Skip to content

Search API

ignacio-alorre edited this page May 23, 2019 · 6 revisions

The search API allows you to execute a search query and get back search hits that match the query. The query can either be provided using a simple query string as a parameter, or using a request body.

Multi-Index, Multi-Type

All search APIs can be applied across multiple types within an index, and across multiple indices with support for the multi-index syntax.

For example, we can search on all documents across all types within the twitter index:

GET /twitter/_search?q=user:kimchy

We can also search within specific types, for example tweet and user:

GET /twitter/tweet,user/_search?q=user:kimchy

We can also search all tweets with a certain tag across several indices (for example, when each user has his own index):

GET /kimchy,elasticsearch/tweet/_search?q=tag:wow

We can search all tweets across all available indices using _all placeholder:

GET /_all/tweet/_search?q=tag:wow

We can search across all indices and all types like:

GET /_search?q=tag:wow

Request Body Search

GET /twitter/tweet/_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

URI Search

GET twitter/tweet/_search?q=user:kimchy

And here is a sample response:

{
    "took": 1,
    "timed_out": false,
    "_shards":{
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
    },
    "hits":{
        "total" : 1,
        "max_score": 1.3862944,
        "hits" : [
            {
                "_index" : "twitter",
                "_type" : "tweet",
                "_id" : "0",
                "_score": 1.3862944,
                "_source" : {
                    "user" : "kimchy",
                    "message": "trying out Elasticsearch",
                    "date" : "2009-11-15T14:12:12",
                    "likes" : 0
                }
            }
        ]
    }
}

Source: https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-search.html#search-multi-index-type

Clone this wiki locally