Beispiel #1
0
func ESQueryString(e *State, T miniprofiler.Timer, key string, query string) (*Results, error) {
	var r Results
	qs := elastic.NewQueryStringQuery(query)
	if key != "" {
		qs.Field(key)
	}
	q := ESQuery{Query: qs}
	r.Results = append(r.Results, &Result{Value: q})
	return &r, nil
}
func TestQueryString(t *testing.T) {

	queryInfo := &config.QueryInfo{
		Type: "query_string",
		Clauses: map[string]interface{}{
			"query": "type:MySQL AND Timestamp [2012-01-01 TO 2012-12-31]",
		},
	}
	myQuery, err := computeQuery(queryInfo)
	assert.Equal(t, err, nil)
	realQuery := elastic.NewQueryStringQuery("type:MySQL AND Timestamp [2012-01-01 TO 2012-12-31]").AnalyzeWildcard(false)
	assert.Equal(t, realQuery, myQuery)

	queryInfo = &config.QueryInfo{
		Type: "querystring",
		Clauses: map[string]interface{}{
			"query":             "this OR (that OR thi*)",
			"analyze_wildcards": true,
		},
	}
	myQuery, err = computeQuery(queryInfo)
	assert.Equal(t, err, nil)
	realQuery = elastic.NewQueryStringQuery("this OR (that OR thi*)").AnalyzeWildcard(true)
	assert.Equal(t, realQuery, myQuery)

	queryInfo = &config.QueryInfo{
		Type: "querystring",
		Clauses: map[string]interface{}{
			"query":             "this OR (that OR this)",
			"analyze_wildcards": 42,
		},
	}
	myQuery, err = computeQuery(queryInfo)
	assert.Equal(t, err, nil)
	realQuery = elastic.NewQueryStringQuery("this OR (that OR this)").AnalyzeWildcard(false)
	assert.Equal(t, realQuery, myQuery)

	//ERRORS
	queryInfo = &config.QueryInfo{
		Type: "querystring",
		Clauses: map[string]interface{}{
			"quer": "type:Error AND method:GET",
		},
	}
	myQuery, err = computeQuery(queryInfo)
	assert.NotNil(t, err)

	queryInfo = &config.QueryInfo{
		Type: "plop",
		Clauses: map[string]interface{}{
			"query": "type:Error AND method:GET",
		},
	}
	myQuery, err = computeQuery(queryInfo)
	assert.NotNil(t, err)

	queryInfo = &config.QueryInfo{
		Type: "querystring",
		Clauses: map[string]interface{}{
			"query": "",
		},
	}
	myQuery, err = computeQuery(queryInfo)
	assert.NotNil(t, err)
}
Beispiel #3
0
// Searches for mssages
func (e *Elastic) GetMessages(q *storage.SearchQuery) (result *storage.SearchResult, err error) {
	var (
		filters []es.Query    = []es.Query{}
		query   *es.BoolQuery = es.NewBoolQuery()
		tsRange *es.RangeQuery
		msg     *storage.Message
	)

	if len(q.Query) > 0 {
		filters = append(filters, es.NewQueryStringQuery(q.Query))
	} else {
		filters = append(filters, es.NewMatchAllQuery())
	}

	if q.From.Second() > 0 && q.To.Second() > 0 {
		tsRange = es.NewRangeQuery("timestamp")
		tsRange = tsRange.From(q.From)
		tsRange = tsRange.To(q.To)
		filters = append(filters, tsRange)
	} else if q.From.Second() > 0 && q.To.Second() <= 0 {
		tsRange = es.NewRangeQuery("timestamp")
		tsRange = tsRange.From(q.From)
		filters = append(filters, tsRange)
	} else if q.To.Second() > 0 && q.From.Second() <= 0 {
		tsRange = es.NewRangeQuery("timestamp")
		tsRange = tsRange.To(q.To)
		filters = append(filters, tsRange)
	}

	rs, err := e.client.
		Search(e.indexName).
		Type(e.typeName).
		Query(query.Filter(filters...)).
		Sort("timestamp", false).
		From(q.Offset).
		Size(q.Limit).
		Do()

	if err != nil {
		return nil, err
	}

	result = &storage.SearchResult{
		Total:    rs.TotalHits(),
		TookMs:   rs.TookInMillis,
		Limit:    q.Limit,
		Offset:   q.Offset,
		Messages: []storage.Message{},
	}

	// rs.Each() is not used because we need to add message id manually
	if rs.Hits != nil && rs.Hits.Hits != nil && len(rs.Hits.Hits) > 0 {
		for _, hit := range rs.Hits.Hits {
			msg = new(storage.Message)
			err = json.Unmarshal(*hit.Source, msg)

			if err != nil {
				continue
			}

			msg.Id = hit.Id

			result.Messages = append(result.Messages, *msg)
		}
	}

	return result, nil
}