예제 #1
0
func (suite *EsTester) Test04JsonOperations() {
	t := suite.T()
	assert := assert.New(t)

	var tmp1, tmp2 Obj
	var err error
	var src *json.RawMessage

	var searchResult *elasticsearch.SearchResult

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

	// SEARCH for everything
	{
		str :=
			`{
				"query": {
					"match_all": {}
				}
			}`

		searchResult, err = esi.SearchByJSON(objType, str)
		assert.NoError(err)
		assert.NotNil(searchResult)

		for _, hit := range *searchResult.GetHits() {
			err = json.Unmarshal(*hit.Source, &tmp1)
			assert.NoError(err)
		}
		assert.EqualValues(3, searchResult.TotalHits())
	}

	// SEARCH for a specific one
	{
		str :=
			`{
				"query": {
					"term": {"id":"id1"}
				}
			}`

		searchResult, err = esi.SearchByJSON(objType, str)
		assert.NoError(err)
		assert.NotNil(searchResult)

		assert.EqualValues(1, searchResult.TotalHits())
		src = searchResult.GetHit(0).Source
		assert.NotNil(src)
		err = json.Unmarshal(*src, &tmp1)
		assert.NoError(err)
		assert.EqualValues("data1", tmp1.Data)
	}

	// SEARCH fuzzily
	{
		str :=
			`{
				"query": {
					"term": {"tags":"foo"}
				}
			}`

		searchResult, err = esi.SearchByJSON(objType, str)
		assert.NoError(err)
		assert.NotNil(searchResult)

		assert.EqualValues(2, searchResult.TotalHits())
		hit0 := searchResult.GetHit(0)
		assert.NotNil(hit0)

		src = hit0.Source
		assert.NotNil(src)
		err = json.Unmarshal(*src, &tmp1)
		assert.NoError(err)

		hit1 := searchResult.GetHit(1)
		assert.NotNil(hit1)
		src = hit1.Source
		assert.NotNil(src)
		err = json.Unmarshal(*src, &tmp2)
		assert.NoError(err)

		ok1 := ("id0" == tmp1.ID && "id2" == tmp2.ID)
		ok2 := ("id0" == tmp2.ID && "id2" == tmp1.ID)
		assert.True((ok1 || ok2) && !(ok1 && ok2))
	}
}
예제 #2
0
func (service *Service) GetMessage(params *piazza.HttpQueryParams) *piazza.JsonResponse {
	var err error

	pagination, err := piazza.NewJsonPagination(params)
	if err != nil {
		return service.newBadRequestResponse(err)
	}

	dsl, err := createQueryDslAsString(pagination, params)
	if err != nil {
		return service.newBadRequestResponse(err)
	}

	var searchResult *elasticsearch.SearchResult

	if dsl == "" {
		searchResult, err = service.esIndex.FilterByMatchAll(schema, pagination)
	} else {
		searchResult, err = service.esIndex.SearchByJSON(schema, dsl)
	}

	if err != nil {
		return service.newInternalErrorResponse(err)
	}

	var lines = make([]Message, 0)

	if searchResult != nil && searchResult.GetHits() != nil {
		for _, hit := range *searchResult.GetHits() {
			if hit.Source == nil {
				log.Printf("null source hit")
				continue
			}

			var msg Message
			err = json.Unmarshal(*hit.Source, &msg)
			if err != nil {
				log.Printf("UNABLE TO PARSE: %s", string(*hit.Source))
				return service.newInternalErrorResponse(err)
			}

			// just in case
			err = msg.Validate()
			if err != nil {
				log.Printf("UNABLE TO VALIDATE: %s", string(*hit.Source))
				continue
			}

			lines = append(lines, msg)
		}
	}

	pagination.Count = int(searchResult.TotalHits())
	resp := &piazza.JsonResponse{
		StatusCode: http.StatusOK,
		Data:       lines,
		Pagination: pagination,
	}

	err = resp.SetType()
	if err != nil {
		return service.newInternalErrorResponse(err)
	}
	return resp
}