Example #1
0
func (suite *EsTester) Test07ConstructMapping() {
	t := suite.T()
	assert := assert.New(t)

	esi := suite.SetUpIndex()
	assert.NotNil(esi)
	defer closer(t, esi)

	items := make(map[string]elasticsearch.MappingElementTypeName)

	items["integer1"] = elasticsearch.MappingElementTypeInteger
	items["integer2"] = elasticsearch.MappingElementTypeInteger
	items["double1"] = elasticsearch.MappingElementTypeDouble
	items["bool1"] = elasticsearch.MappingElementTypeBool
	items["date1"] = elasticsearch.MappingElementTypeDate

	jsonstr, err := elasticsearch.ConstructMappingSchema("MyTestObj", items)
	assert.NoError(err)
	assert.NotNil(jsonstr)
	assert.NotEmpty(jsonstr)

	var iface interface{}
	err = json.Unmarshal([]byte(jsonstr), &iface)
	assert.NoError(err)

	byts, err := json.Marshal(iface)
	assert.NoError(err)
	assert.NotNil(byts)

	actual := string(byts)

	expected :=
		`{"MyTestObj":{"properties":{"bool1":{"type":"boolean"},"date1":{"type":"date"},"double1":{"type":"double"},"integer1":{"type":"integer"},"integer2":{"type":"integer"}}}}`

	assert.Equal(expected, actual)

	err = esi.SetMapping("MyTestObj", piazza.JsonString(actual))
	assert.NoError(err)
}
Example #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)
}
Example #3
0
func (suite *EsTester) Test08Percolation() {
	t := suite.T()
	assert := assert.New(t)

	esi := suite.SetUpIndex()
	assert.NotNil(esi)
	defer closer(t, esi)

	items := make(map[string]elasticsearch.MappingElementTypeName)
	items["tag"] = elasticsearch.MappingElementTypeString
	jsonstr, err := elasticsearch.ConstructMappingSchema("Event", items)
	assert.NoError(err)
	assert.NotEmpty(jsonstr)

	err = esi.SetMapping("Event", jsonstr)
	assert.NoError(err)

	query1 :=
		`{
		"query": {
			"match": {
				"tag": {
					"query": "kitten"
				}
			}
		}
	}`
	query2 :=
		`{
		"query" : {
			"match" : {
				"tag" : "lemur"
			}
		}
	}`

	_, err = esi.AddPercolationQuery("p1", piazza.JsonString(query1))
	assert.NoError(err)

	type Event struct {
		ID  string `json:"id" binding:"required"`
		Tag string `json:"tag" binding:"required"`
	}
	event1 := Event{ID: "id1", Tag: "kitten"}
	event2 := Event{ID: "id2", Tag: "cat"}
	event3 := Event{ID: "id3", Tag: "lemur"}

	percolateResponse, err := esi.AddPercolationDocument("Event", event1)
	assert.NoError(err)
	assert.EqualValues(1, percolateResponse.Total)
	assert.Equal("p1", percolateResponse.Matches[0].Id)

	percolateResponse, err = esi.AddPercolationDocument("Event", event2)
	assert.NoError(err)
	assert.EqualValues(0, percolateResponse.Total)

	_, err = esi.AddPercolationQuery("p2", piazza.JsonString(query2))
	assert.NoError(err)

	percolateResponse, err = esi.AddPercolationDocument("Event", event3)
	assert.NoError(err)
	assert.EqualValues(1, percolateResponse.Total)
	assert.Equal("p2", percolateResponse.Matches[0].Id)
}