Search details of elastic search (2): request body search

Posted by frabble on Wed, 25 Dec 2019 16:15:46 +0100

The previous article introduced the search based on url. This time I want to talk about a more advanced search method - Request Body Search. The search parameters are not written on the url, but sent as the requested data. The syntax of Query DSL can be used to combine more flexible searches.

A simple example

GET /customer/_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

The corresponding curl format is:

curl -X GET "localhost:9200/customer/_search" -H 'Content-Type: application/json' -d'
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
'

Results returned by query

{
    "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" : "_doc",
                "_id" : "0",
                "_score": 1.3862944,
                "_source" : {
                    "user" : "kimchy",
                    "message": "trying out Elasticsearch",
                    "date" : "2009-11-15T14:12:12",
                    "likes" : 0
                }
            }
        ]
    }
}

After the statistics of this query, and the documents found.

from and size

Similar to a relational database, ES can specify from and size, for example

GET /customer/_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    },
    from: 0,
    size: 20
}

from 0, the maximum size cannot be greater than the configuration of index.max'result'window. The default is 10000.

Sort sort

You can specify one or more fields as sort fields. There are two special sorting fields, i.e., "score" and "doc". The "score" is the score matching the document, and "doc" is the order of document indexing.

Before you can use sort, you must specify the type for the sorted fields (which must be completed before indexing), for example

PUT /my_index
{
    "mappings": {
        "_doc": {
            "properties": {
                "post_date": { "type": "date" },
                "user": {
                    "type": "keyword"
                },
                "name": {
                    "type": "keyword"
                },
                "age": { "type": "integer" }
            }
        }
    }
}

Then you can specify the field sorting, asc and desc

GET /my_index/_search
{
    "sort" : [
        { "post_date" : {"order" : "asc"}},
        "user",
        { "name" : "desc" },
        { "age" : "desc" },
        "_score"
    ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

sort mode

When the sorted field is an array, how to tell ES to sort? Sort can also specify the mode attribute, min, max, sum, avg, or median, with the same meaning as literally understood.

Give an example

PUT /my_index/_doc/1?refresh
{
   "product": "chocolate",
   "price": [20, 4]
}

GET /my_index/_search
{
   "query" : {
      "term" : { "product" : "chocolate" }
   },
   "sort" : [
      {"price" : {"order" : "asc", "mode" : "avg"}}
   ]
}

sort nexted

What to do when the sorted field is an attribute in an object? For example, offer.price,

POST /_search
{
   "query" : {
      "term" : { "product" : "chocolate" }
   },
   "sort" : [
       {
          "offer.price" : {
             "mode" :  "avg",
             "order" : "asc",
             "nested": {
                "path": "offer",
                "filter": {
                   "term" : { "offer.color" : "blue" }
                }
             }
          }
       }
    ]
}

offer.price is a user-defined sorting name. The necessary keywords are nested and path. Path indicates which object's attribute the sorting attribute is. filter is optional. It has the same query criteria as query.

Topics: Attribute curl JSON ElasticSearch