Understanding of Elasticsearch Query statements

Source: Internet
Author: User

Reprint Source https://blog.csdn.net/hololens/article/details/78932628

①{"Query": {"Match_all": {}}} The query section tells us what our definition is, and the Match_all section simply specifies the type of query we want to execute, meaning to search all the documents in the index.

In addition to the query parameter, we can also influence search results with other parameters. Specify size to specify the number of results returned

{"Query": {"Match_all": {}}, "Size": 1}} Note If size is not specified, it defaults to 10.


POST  /testxufeifei/_search
{"
   query": {"Match_all": {}},
   "from": 2,
   "size": 2
}
The From parameter (starting at 2) specifies the document index from which to start, and the size parameter specifies how many documents to return from the index specified in the From. This feature is useful when you are implementing a paged search. Note If the from parameter is not specified, it defaults to 0.

The following example uses Match_all and returns the first 10 documents in reverse order by the balance value of the account:

{"Query": {"Match_all": {}}, "sort": {"balance": {"order": "Desc"}}

First, let's look at the document properties that are returned. By default, the document returns all property values as part of the search results. The JSON content of this document is called source (the value of the _source property that returns the hits in the result). If we don't need to return all the source document properties, we can add the property names we need to return in the request body.

The following example shows how to return two properties, age and balance (in _source): Post/testxufeifei/_search

{"Query": {"Match_all": {}}, "_source": ["balance", "age"], "Size": 2}


Note that the above example only reduces the properties in the _source. It will still return the _source property, except that the _source property contains Account_number and balance two properties.

Now, let's go to the query section. We've seen how to use Match_all to match all of the documents before. Now let's introduce a new query called a match query, which can be considered a basic property search query (that is, search by a specific attribute or attributes).

The following example returns a document with a account_number of 20:

post/testxufeifei/_search{"Query": {"match": {"Account_number": 13}}


The following example returns the account document for all address fields that contain the word "Street" (case insensitive):

{"Query": {"match": {"Address": "Street"}}, "_source": ["Address", "City"]} record displays only the value of the Address City field


The returned result is

{"hits": { 
"total": 3,
"Max_score": 0.70273256,
"hits": [ 
{ 
    "_index": "Testxufeifei",
     "_ Type ":" Doc ",
     " _id ":" 2 ",
     " _score ": 0.70273256,
     " _source ": { 
         " address ":" 671 Bristol Street ",
         " City ":" Dante "
      }
,
 { 
      " _index ":" Testxufeifei ",
      " _type ":" Example ",
      " _id ":" 7 " ,
      "_score": 0.5,
      "_source": { 
            "address": "702 Quentin Street",
            "City": "Veguita"
       }
      }
,
{ 
     "_index": "Testxufeifei",
     "_type": "Doc",
     "_id": "3",
     "_score": 0.15342641,
     "_source 
     ": { "Address": "789 Madison Street",
      "City": "Nogal"}}
]
}

The following example returns all account documents that contain "671" or "702" in the Address field:

{"Query": {"match": {"Address": "671 702"}}, "_source": ["Address", "City", "FirstName"]}


Return data as

{"hits": [ 
{ 
    "_index": "Testxufeifei",
    "_type": "Doc",
    "_id": "2",
    "_score": 0.19551794,
    "_ Source ": { 
       " address ":" 671 Bristol Street ",
       " FirstName ":" Hattie ",
       " City ":" Dante "
     }
,
{ 
    "_index": "Testxufeifei",
    "_type": "Example",
    "_id": "7",
    "_score": 0.12713557,
    " _source ": { 
      " address ":" 702 Quentin Street ",
      " FirstName ":" Dillard ",
      " City ":" Veguita "
}
}
]}

The following example is a variant of match (Match_phrase), which returns all account documents that contain the phrase "171 Putnam" in Address:

{"Query": {"match_phrase": {"Address": "171 Putnam"}}, "_source": ["Address", "City", "FirstName"]}


Query results are

{"hits": {"Total": 2, "Max_score": 1.4054651, "hits": [{"_index": "Testxufeifei",
      "_type": "Doc", "_id": "6", "_score": 1.4054651, "_source": {"Address": "171 Putnam Avenue", "FirstName": "Virginia", "City": "Nicholson"}, {"_index": "Testxufeifei", "_type": "Doc "," _id ":" 5 "," _score ": 0.30685282," _sour 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.