ElasticSearch Search
Search can be executed across indices and types, with query string as a parameter, or using a request body.
Search is broadcasted to all the index/indices shards; to limit the scope, you can use routing parameter - e.g. using user ID when searching through tweets by given user. The routing parameter may be multivalued, CSV.
Contents
Request Body
Request body uses Query DSL.
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{ "query" : { "term" : { "user" : "kimchy" } } }'
{ "_shards":{ "total" : 5, "successful" : 5, "failed" : 0 }, "hits":{ "total" : 1, "hits" : [ { "_index" : "twitter", "_type" : "tweet", "_id" : "1", "_source" : { "user" : "kimchy", "postDate" : "2009-11-15T14:12:12", "message" : "trying out Elastic Search" } } ] } }
Parameters
- timeout - search timeout, bounding the search request to be executed within the specified time; default no timeout,
- from - the starting from index of the hits to return; default 0,
- size - the number of hits to return; default 10,
- search_type - the type of the search operation to perform - dfs_query_then_fetch, dfs_query_and_fetch, query_then_fetch, query_and_fetch; defaults query_then_fetch; see Search Type for more details on the different types of search that can be performed.
URI Request
A search request can be executed purely using a URI by providing request parameters.
$ curl -XGET 'http://localhost:9200/twitter/tweet/_search?q=user:kimchy'
Parameters
- q - query string (maps to the query_string query, see Query String Query for more details),
- df - default field to use when no field prefix is defined,
- default_operator - default operator to be used - AND or OR, default OR,
- explain - include explanation of how scoring of the each hits was computed,
- fields - selective fields of the document to return, CSV; default internal _source field; empty value will cause no fields to return,
- sort - sorting to perform - fieldName, fieldName:asc, or fieldName:desc; fieldName can either be an actual field, or _score; there can be several sort parameters (CSV, order is important),
- track_scores - when sorting, set to true in order to return score as part of each hit,
- timeout - search timeout, limiting the execution time; all results accumulated up to timeout are returned; defaults to no timeout,
- from - starting from index of the hits to return; defaults 0,
- size - number of hits to return; defaults 10,
- lowercase_expanded_terms - should terms be automatically lowercased or not; default true,
- analyze_wildcard - should wildcard and prefix queries be analyzed or not; default false.
Query Element
See Query DSL for details.
{ "query" : { "term" : { "user" : "kimchy" } } }
Filter Element
When doing things like facet navigation, sometimes only the hits are needed to be filtered by the chosen facet, and all the facets should continue to be calculated based on the original query. The filter element within the search request can be used to accomplish it.
Note, this is different compared to creating a filtered query with the filter, since this will cause the facets to only process the filtered results.
In other words, using
{ "query" : { "term" : { "message" : "something" } }, "filter" : { "term" : { "tag" : "green" } }, "facets" : { "tag" : { "terms" : { "field" : "tag" } } } }
the filter will not change the facets (the results of facets will be the same as without the filter element), while the results set will be different.
But using filtered query:
{ "filtered" : { "query" : { "message" : "something" }, "filter" : { "term" : { "tag" : "green" } } } }, "facets" : { "tag" : { "terms" : { "field" : "tag" } } } }
the filter field within the filtered query element will change the facets, influencing both the results set and the facets.
To filter the facets, you can use facet_filter element.