Running Queries
editRunning Queries
editRequest structures
editEach endpoint comes with a Request type that represents the body of its request.
For example, a simple search request for a term "Foo" in the name
field could be written like this:
search.Request{ Query: &types.QueryContainer{ Term: map[types.Field]types.TermQuery{ "name": {Value: "Foo"}, }, }, }
Query Builder
editAlthough the structure syntax is lightweight, the types will sometimes lead you to pointers of string or even type aliases which ultimately are interfaces. For all those occasions as well as discoverability, we provide for the entire API a builder for each type; the request above can be written like this:
ss := search.NewRequest(). Query( types.NewQueryContainer(). Term(map[types.Field]types.TermQuery{ "name": types.NewTermQuery().Value("Foo").Build(), }). Build()). Build()
The builders are designed to take value arguments for easier inline use, you can mix at will the structures and the builder as you see fit depending on your usage.
Raw JSON
editLastly if you want to use your own pre-baked JSON queries using templates or even a specific encoder, you can pass the body directly to the Raw
method of the endpoint:
es.Search().Raw([]byte(`{ "query": { "term": { "user.id": { "value": "kimchy", "boost": 1.0 } } } }`))
No further validation or serialization is done on what is sent through this method, setting a payload with this takes precedence over any request structure you may submit before running the query.