Ejemplo n.º 1
0
func (suite *EsTester) SetUpIndex() elasticsearch.IIndex {
	t := suite.T()
	assert := assert.New(t)

	var esi *elasticsearch.Index
	var err error
	var ok bool

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

		err = esi.Delete()
		//assert.NoError(err)

		ok, err = esi.IndexExists()
		assert.NoError(err)
		assert.False(ok)

		// make the index
		err = esi.Create("")
		assert.NoError(err)
		ok, err = esi.IndexExists()
		assert.NoError(err)
		assert.True(ok)

		err = esi.SetMapping(objType, objMapping)
		assert.NoError(err)
	}

	// populate the index
	for _, o := range objs {
		indexResult, err := esi.PostData(objType, o.ID, o)
		assert.NoError(err)
		assert.NotNil(indexResult)
	}

	// allow the database time to settle
	pollingFn := elasticsearch.GetData(func() (bool, error) {
		getResult, err := esi.FilterByMatchAll(objType, nil)
		if err != nil {
			return false, err
		}
		if getResult != nil && len(*getResult.GetHits()) == len(objs) {
			return true, nil
		}
		return false, nil
	})

	{
		_, err := elasticsearch.PollFunction(pollingFn)
		assert.NoError(err)
	}

	return esi
}
Ejemplo n.º 2
0
func (suite *EsTester) Test01Client() {
	t := suite.T()
	assert := assert.New(t)

	esi, err := elasticsearch.NewIndex2(suite.url, "estest01$", "")
	assert.NoError(err)

	version := esi.GetVersion()
	assert.NoError(err)
	assert.True(strings.HasPrefix(version, "2.2") ||
		strings.HasPrefix(version, "2.3") ||
		strings.HasPrefix(version, "2.4"))
}
Ejemplo n.º 3
0
func (suite *EsTester) Test10GetAll() {
	t := suite.T()
	assert := assert.New(t)

	esi, err := elasticsearch.NewIndex2(suite.url, "getall$", "")
	assert.NoError(err)
	defer closer(t, esi)

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

	type T1 struct {
		Data1  string `json:"data1" binding:"required"`
		Extra1 string `json:"extra1" binding:"required"`
	}

	type T2 struct {
		Data2  int    `json:"data2" binding:"required"`
		Extra2 string `json:"extra2" binding:"required"`
	}

	schema1 :=
		`{
			"schema1":{
				"properties":{
					"data1":{
						"type":"string",
						"store":true
					},
					"extra1":{
						"type":"string",
						"store":true
					}
				}
			}
		}`

	schema2 :=
		`{
			"schema2":{
				"properties":{
					"data2":{
						"type":"integer",
						"store":true
					},
					"extra2":{
						"type":"string",
						"store":true
					}
				}
			}
		}`

	err = esi.SetMapping("schema1", piazza.JsonString(schema1))
	assert.NoError(err)
	err = esi.SetMapping("schema2", piazza.JsonString(schema2))
	assert.NoError(err)

	obj1 := T1{Data1: "obj", Extra1: "extra1"}
	obj2 := T2{Data2: 123, Extra2: "extra2"}
	indexResult, err := esi.PostData("schema1", "id1", obj1)
	assert.NoError(err)
	assert.NotNil(indexResult)
	indexResult, err = esi.PostData("schema2", "id2", obj2)
	assert.NoError(err)
	assert.NotNil(indexResult)

	{
		// GET a specific one
		getResult, err := esi.GetByID("schema1", "id1")
		assert.NoError(err)
		assert.NotNil(getResult)
		src := getResult.Source
		assert.NotNil(src)
		var tmp T1
		err = json.Unmarshal(*src, &tmp)
		assert.NoError(err)
		assert.EqualValues("obj", tmp.Data1)
	}

	{
		// GET a specific one
		getResult, err := esi.GetByID("schema2", "id2")
		assert.NoError(err)
		assert.NotNil(getResult)
		src := getResult.Source
		assert.NotNil(src)
		var tmp T2
		err = json.Unmarshal(*src, &tmp)
		assert.NoError(err)
		assert.Equal(123, tmp.Data2)
	}

	{
		// GET the types
		strs, err := esi.GetTypes()
		assert.NoError(err)
		assert.Len(strs, 2)
		if strs[0] == "schema1" {
			assert.EqualValues("schema2", strs[1])
		} else if strs[0] == "schema2" {
			assert.EqualValues("schema1", strs[1])
		} else {
			assert.True(false)
		}
	}

	{
		pollingFn := elasticsearch.GetData(func() (bool, error) {
			getResult, err := esi.FilterByMatchAll("", nil)
			if err != nil {
				return false, err
			}
			if getResult != nil && len(*getResult.GetHits()) == 2 {
				return true, nil
			}
			return false, nil
		})

		_, err := elasticsearch.PollFunction(pollingFn)
		getResult, err := esi.FilterByMatchAll("", nil)
		assert.NoError(err)
		assert.NotNil(getResult)
		assert.Len(*getResult.GetHits(), 2)
		src1 := getResult.GetHit(0).Source
		assert.NotNil(src1)
		src2 := getResult.GetHit(1).Source
		assert.NotNil(src2)

		var tmp1 T1
		var tmp2 T2
		err1 := json.Unmarshal(*src1, &tmp1)
		err2 := json.Unmarshal(*src2, &tmp2)
		assert.True((err1 == nil && err2 == nil) || (err1 != nil && err2 != nil))

		if err1 != nil {
			err = json.Unmarshal(*src1, &tmp1)
			assert.NoError(err)
			err = json.Unmarshal(*src2, &tmp2)
			assert.NoError(err)
		} else {
			err = json.Unmarshal(*src1, &tmp2)
			assert.NoError(err)
			err = json.Unmarshal(*src2, &tmp1)
			assert.NoError(err)
		}

		assert.Equal(tmp1.Data1, "obj")
		assert.Equal(tmp1.Extra1, "extra1")
		assert.Equal(tmp2.Data2, 123)
		assert.Equal(tmp2.Extra2, "extra2")
	}
}
Ejemplo n.º 4
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)
}