Search API

Other topics

Routing

When executing a search, it will be broadcast to all the index/indices shards (round robin between replicas). Which shards will be searched on can be controlled by providing the routing parameter. For example, when indexing tweets, the routing value can be the user name:

curl -XPOST 'localhost:9200/twitter/tweet?routing=kimchy&pretty' -d'
{
    "user" : "kimchy",
    "postDate" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}'

Search using request body

Searches can also be done on elasticsearch using a search DSL.The query element within the search request body allows to define a query using the Query DSL.

GET /my_index/type/_search
{
    "query" : {
        "term" : { "field_to_search" : "search_item" }
    }
}

Multi search

The multi_search option allows us to search for a query in multiple fields at once.

GET /_search
{
  "query": {
    "multi_match" : {
      "query":    "text to search", 
      "fields": [ "field_1", "field_2" ] 
    }
  }
}

We can also boost the score of certain fields using the boost operator(^), and use wild cards in the field name (*)

GET /_search
    {
      "query": {
        "multi_match" : {
          "query":    "text to search", 
          "fields": [ "field_1^2", "field_2*" ] 
        }
      }
    }

URI search, and Highlighting

A search request can be executed purely using a URI by providing request parameters. Not all search options are exposed when executing a search using this mode, but it can be handy for quick "curl tests".

GET Index/type/_search?q=field:value

Another useful feature provided is highlighting the match hits in the documents.

GET /_search
{
    "query" : {
        "match": { "field": "value" }
    },
    "highlight" : {
        "fields" : {
            "content" : {}
        }
    }
}

In the above case, the particular field will be highlighted for each search hit

Contributors

Topic Id: 8625

Example Ids: 26936,31919,31984,32050

This site is not affiliated with any of the contributors.