Esempio n. 1
0
func closer(t *testing.T, esi elasticsearch.IIndex) {
	if esi == nil {
		return
	}
	err := esi.Close()
	assert.NoError(t, err)
	err = esi.Delete()
	assert.NoError(t, err)
}
Esempio n. 2
0
func (suite *EsTester) Test09FullPercolation() {
	t := suite.T()
	assert := assert.New(t)

	var esi elasticsearch.IIndex

	// create index
	{
		var err error

		esi, err = elasticsearch.NewIndex2(suite.url, "estest09$"+uniq(), "")
		assert.NoError(err)

		defer closer(t, esi)

		// make the index
		err = esi.Create("")
		assert.NoError(err)

		ok, err := esi.IndexExists()
		assert.NoError(err)
		assert.True(ok)
	}

	//-----------------------------------------------------------------------

	addMappings := func(maps map[string](map[string]elasticsearch.MappingElementTypeName)) {
		for k, v := range maps {
			jsn, err := elasticsearch.ConstructMappingSchema(k, v)
			assert.NoError(err)
			assert.NotEmpty(jsn)
			err = esi.SetMapping(k, jsn)
			assert.NoError(err)
		}
	}

	addQueries := func(queries map[string]piazza.JsonString) {
		for k, v := range queries {
			_, err := esi.AddPercolationQuery(k, v)
			assert.NoError(err)
		}
	}

	type TestCase struct {
		typeName string
		event    interface{}
		expected []string
	}

	addEvents := func(tests []TestCase) {
		for i, t := range tests {

			percolateResponse, err := esi.AddPercolationDocument(t.typeName, t.event)
			assert.NoError(err)

			assert.EqualValues(len(t.expected), percolateResponse.Total, fmt.Sprintf("for test #%d", i))

			matches := sortMatches(percolateResponse.Matches)

			for i, expected := range t.expected {
				assert.Equal(esi.IndexName(), matches[i].Index)
				assert.Equal(expected, matches[i].Id)
			}
		}
	}

	//-----------------------------------------------------------------------

	type EventType1 struct {
		ID  string `json:"id" binding:"required"`
		Str string `json:"str" binding:"required"`
		Num int    `json:"num" binding:"required"`
	}

	type EventType2 struct {
		ID  string `json:"id" binding:"required"`
		Boo bool   `json:"boo" binding:"required"`
		Num int    `json:"num" binding:"required"`
	}

	maps := map[string](map[string]elasticsearch.MappingElementTypeName){
		"EventType1": map[string]elasticsearch.MappingElementTypeName{
			"id":  elasticsearch.MappingElementTypeString,
			"str": elasticsearch.MappingElementTypeString,
			"num": elasticsearch.MappingElementTypeInteger,
		},
		"EventType2": map[string]elasticsearch.MappingElementTypeName{
			"id":  elasticsearch.MappingElementTypeString,
			"boo": elasticsearch.MappingElementTypeBool,
			"num": elasticsearch.MappingElementTypeInteger,
		},
	}
	addMappings(maps)

	//-----------------------------------------------------------------------

	q1 :=
		`{
			"query": {
				"match": {
					"str": {
						"query": "kitten"
					}
				}
			}
		}`
	q2 :=
		`{
			"query" : {
				"match" : {
					"boo" : true
				}
			}
		}`
	q3 :=
		`{
			"query" : {
				"match" : {
					"num" : 17
				}
			}
		}`
	q4 :=
		`{
			"query" : {
				"range" : {
					"num" : {
						"lt": 10.0
					}
				}
			}
		}`
	q5 :=
		`{
			"query" : {
				"filtered": {
					"query": {
						"match": {
							"num": 17
						}
					},
					"filter": {
						"term": {
							"_type": "EventType2"
						}
					}
				}
			}
		}`
	q6 :=
		`{
		"query" : {
			"bool": {
				"must": [
					{
						"match" : {
							"num" : 17
						}
					},
					{
						"match" : {
							"_type" : "EventType1"
						}
					}
				]
			}
		}
	}`
	queries := map[string]piazza.JsonString{
		"Q1": piazza.JsonString(q1),
		"Q2": piazza.JsonString(q2),
		"Q3": piazza.JsonString(q3),
		"Q4": piazza.JsonString(q4),
		"Q5": piazza.JsonString(q5),
		"Q6": piazza.JsonString(q6),
	}
	addQueries(queries)

	//-----------------------------------------------------------------------

	tests := []TestCase{
		TestCase{
			typeName: "EventType1",
			event:    EventType1{ID: "E1", Str: "kitten", Num: 17},
			expected: []string{"Q1", "Q3", "Q6"},
		},
		TestCase{
			typeName: "EventType2",
			event:    EventType2{ID: "E2", Boo: true, Num: 17},
			expected: []string{"Q2", "Q3", "Q5"},
		},
		TestCase{
			typeName: "EventType1",
			event:    EventType1{ID: "E3", Str: "lemur", Num: -31},
			expected: []string{"Q4"},
		},
	}

	addEvents(tests)
}
Esempio n. 3
0
func (service *Service) Init(sys *piazza.SystemConfig, esIndex elasticsearch.IIndex) error {
	var err error

	service.stats.CreatedOn = time.Now()
	/***
	err = esIndex.Delete()
	if err != nil {
		log.Fatal(err)
	}
	if esIndex.IndexExists() {
		log.Fatal("index still exists")
	}
	err = esIndex.Create()
	if err != nil {
		log.Fatal(err)
	}
	***/

	ok, err := esIndex.IndexExists()
	if err != nil {
		return err
	}
	if !ok {
		log.Printf("Creating index: %s", esIndex.IndexName())
		err = esIndex.Create("")
		if err != nil {
			log.Fatal(err)
		}
	}

	ok, err = esIndex.TypeExists(schema)
	if err != nil {
		return err
	}
	if !ok {
		log.Printf("Creating type: %s", schema)

		mapping :=
			`{
			"LogData7":{
				"dynamic": "strict",
				"properties": {
					"service": {
						"type": "string",
						"store": true,
						"index": "not_analyzed"
					},
					"address": {
						"type": "string",
						"store": true,
						"index": "not_analyzed"
					},
					"createdOn": {
						"type": "date",
						"store": true,
						"index": "not_analyzed"
					},
					"severity": {
						"type": "string",
						"store": true,
						"index": "not_analyzed"
					},
					"message": {
						"type": "string",
						"store": true,
						"index": "analyzed"
					}
				}
			}
		}`

		err = esIndex.SetMapping(schema, piazza.JsonString(mapping))
		if err != nil {
			log.Printf("LoggerService.Init: %s", err.Error())
			return err
		}
	}

	service.esIndex = esIndex

	service.origin = string(sys.Name)

	return nil
}