Beispiel #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
}
Beispiel #2
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")
	}
}
Beispiel #3
0
func (suite *EsTester) Test11Pagination() {
	t := suite.T()
	assert := assert.New(t)

	var err error

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

	type Obj3 struct {
		ID3   string `json:"id3" binding:"required"`
		Data3 int    `json:"data3" binding:"required"`
	}
	obj3Mapping :=
		`{
			"Obj3":{
				"properties":{
				"id3": {
					"type":"string",
					"store":true
				},
				"data3": {
					"type":"integer",
					"store": true
				}
			}
		}
	}`

	err = esi.SetMapping("Obj3", piazza.JsonString(obj3Mapping))
	assert.NoError(err)

	p := uniq()

	for i := 0; i <= 9; i++ {
		id := fmt.Sprintf("id%d_%s", i, p)
		obj := Obj3{ID3: id, Data3: i * i}
		indexResult, err := esi.PostData("Obj3", id, obj)
		assert.NoError(err)
		assert.NotNil(indexResult)
		assert.EqualValues(id, indexResult.ID)
	}

	{
		realFormat := &piazza.JsonPagination{
			PerPage: 4,
			Page:    0,
			Order:   piazza.SortOrderAscending,
			SortBy:  "id3",
		}
		pollingFn := elasticsearch.GetData(func() (bool, error) {
			getResult, err := esi.FilterByMatchAll("Obj3", realFormat)
			if err != nil {
				return false, err
			}
			if getResult != nil && getResult.TotalHits() == 10 {
				return true, nil
			}
			return false, nil
		})

		_, err := elasticsearch.PollFunction(pollingFn)
		getResult, err := esi.FilterByMatchAll("Obj3", realFormat)
		assert.NoError(err)
		assert.Len(*getResult.GetHits(), 4)
		assert.Equal("id0_"+p, getResult.GetHit(0).ID)
		assert.Equal("id1_"+p, getResult.GetHit(1).ID)
		assert.Equal("id2_"+p, getResult.GetHit(2).ID)
		assert.Equal("id3_"+p, getResult.GetHit(3).ID)
	}

	{
		realFormat := &piazza.JsonPagination{
			PerPage: 3,
			Page:    1,
			Order:   piazza.SortOrderAscending,
			SortBy:  "id3",
		}
		getResult, err := esi.FilterByMatchAll("Obj3", realFormat)
		assert.NoError(err)
		assert.Len(*getResult.GetHits(), 3)
		assert.Equal("id3_"+p, getResult.GetHit(0).ID)
		assert.Equal("id4_"+p, getResult.GetHit(1).ID)
		assert.Equal("id5_"+p, getResult.GetHit(2).ID)
	}
}