This commit implements a Search() function, which allow for running search requests with both a query and aggregations. This function is meant to more accurately implement the structure of search requests accepted by ElasticSearch's Search API. The Query() and Aggregate() functions are still included by the library, but now simply call Search() internally, making them simple shortcuts. Two new aggregations are also added: "terms" and "top_hits". These are implemented a bit differently than previously implemented ones. The structs and methods for ElasticSearch queries and aggregations will eventually be auto-generated from a specification file, and will look more like the new implementations of these new aggregations.
98 lines
2.1 KiB
Go
98 lines
2.1 KiB
Go
package esquery
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSearchMaps(t *testing.T) {
|
|
runMapTests(t, []mapTest{
|
|
{
|
|
"a simple match_all query with a size and no aggs",
|
|
Search().Query(MatchAll()).Size(20),
|
|
map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"match_all": map[string]interface{}{},
|
|
},
|
|
"size": 20,
|
|
},
|
|
},
|
|
{
|
|
"a complex query with an aggregation and various other options",
|
|
Search().
|
|
Query(
|
|
Bool().
|
|
Must(
|
|
Range("date").
|
|
Gt("some time in the past").
|
|
Lte("now").
|
|
Relation(RangeContains).
|
|
TimeZone("Asia/Jerusalem").
|
|
Boost(2.3),
|
|
|
|
Match("author").
|
|
Query("some guy").
|
|
Analyzer("analyzer?").
|
|
Fuzziness("fuzz"),
|
|
).
|
|
Boost(3.1),
|
|
).
|
|
Aggs(
|
|
Sum("total_score", "score"),
|
|
StringStats("tag_stats", "tags").
|
|
ShowDistribution(true),
|
|
).
|
|
Size(30).
|
|
From(5).
|
|
Explain(true).
|
|
Timeout(time.Duration(20000000000)),
|
|
map[string]interface{}{
|
|
"query": map[string]interface{}{
|
|
"bool": map[string]interface{}{
|
|
"must": []map[string]interface{}{
|
|
{
|
|
"range": map[string]interface{}{
|
|
"date": map[string]interface{}{
|
|
"gt": "some time in the past",
|
|
"lte": "now",
|
|
"relation": "CONTAINS",
|
|
"time_zone": "Asia/Jerusalem",
|
|
"boost": 2.3,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
"match": map[string]interface{}{
|
|
"author": map[string]interface{}{
|
|
"query": "some guy",
|
|
"analyzer": "analyzer?",
|
|
"fuzziness": "fuzz",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
"boost": 3.1,
|
|
},
|
|
},
|
|
"aggs": map[string]interface{}{
|
|
"total_score": map[string]interface{}{
|
|
"sum": map[string]interface{}{
|
|
"field": "score",
|
|
},
|
|
},
|
|
"tag_stats": map[string]interface{}{
|
|
"string_stats": map[string]interface{}{
|
|
"field": "tags",
|
|
"show_distribution": true,
|
|
},
|
|
},
|
|
},
|
|
"size": 30,
|
|
"from": 5,
|
|
"explain": true,
|
|
"timeout": "20s",
|
|
},
|
|
},
|
|
})
|
|
}
|