ES权威指南_01_get start_07 Full-Body Search

来源:互联网 发布:c语言计算闰年循环 编辑:程序博客网 时间:2024/06/13 02:01

https://www.elastic.co/guide/en/elasticsearch/guide/current/full-body-search.html

1 Empty Search

returns all documents in all indices:

GET /_search{}

you can search on one, many, or _all indices, and one, many, or all types:

GET /index_2014*/type1,type2/_search{}

use the from and size parameters for pagination:

GET /_search{  "from": 30,  "size": 10}

A GET Request with a Body?
The authors of Elasticsearch prefer using GET for a search request because they feel that it describes the action—retrieving information—better than the POST verb. However, because GET with a request body is not universally supported, the search API also accepts POST requests:

POST /_search{  // query DSL  "from": 30,  "size": 10}

2 Query DSL

flexible, expressive search language

GET /_search{    "query": YOUR_QUERY_HERE  //query _all}

The empty search—{}—is functionally equivalent to using the match_all query clause

GET /_search{    "query": {        "match_all": {}    }}
GET /_search{    "query": {        "match": {            "tweet": "elasticsearch"        }    }}

Combining Multiple Clauses

{    "bool": {        "must":     { "match": { "tweet": "elasticsearch" }},        "must_not": { "match": { "name":  "mary" }},        "should":   { "match": { "tweet": "full text" }},        "filter":   { "range": { "age" : { "gt" : 30 }} }    }}

It is important to note that a compound clause can combine any other query clauses, including other compound clauses.

{    "bool": {        "must": { "match":   { "email": "business opportunity" }},        "should": [            { "match":       { "starred": true }},            { "bool": {                "must":      { "match": { "folder": "inbox" }},                "must_not":  { "match": { "spam": true }}            }}        ],        "minimum_should_match": 1    }}

3 Queries and Filters

two contexts: filtering context and query context.
filtering context, the query is said to be a “non-scoring” or “filtering” query,yes|no.
querying context, the query becomes a “scoring” query , if a document matches and how well the document matches.

注意:
Historically, queries and filters were separate components in Elasticsearch. Starting in Elasticsearch 2.0, filters were technically eliminated, and all queries gained the ability to become non-scoring.

However, for clarity and simplicity, we will use the term “filter” to mean a query which is used in a non-scoring, filtering context. You can think of the terms “filter”, “filtering query” and “non-scoring query” as being identical.

Similarly, if the term “query” is used in isolation without a qualifier, we are referring to a “scoring query”.


Performance Differences
Filtering queries are simple checks for set inclusion/exclusion, which make them very fast to compute. There are various optimizations that can be leveraged when at least one of your filtering query is “sparse” (few matching documents), and frequently used non-scoring queries can be cached in memory for faster access.

In contrast, scoring queries have to not only find matching documents, but also calculate how relevant each document is, which typically makes them heavier

Thanks to the inverted index, a simple scoring query that matches just a few documents may perform as well or better than a filter that spans millions of documents. In general, however, a filter will outperform a scoring query.

The goal of filtering is to reduce the number of documents that have to be examined by the scoring queries.

4 Most Important Queries

4.1 match_all

{ "match_all": {}}

4.2 match

exact value 或 full-text

{ "match": { "age":    26           }}{ "match": { "tweet": "About Search" }}

提示:For exact-value searches, you probably want to use a filter clause instead of a query, as a filter will be cached.

4.3 multi_match

run the same match query on multiple fields:

{    "multi_match": {        "query":    "full text search",        "fields":   [ "title", "body" ]    }}

4.4 rang

gt,gte,lt,lte

{    "range": {        "age": {            "gte":  20,            "lt":   30        }    }}

4.5 term

used to search by exact values,, be they numbers, dates, Booleans, or not_analyzed exact-value string fields

{ "term": { "age":    26           }}

The term query performs no analysis on the input text, so it will look for exactly the value that is supplied.

4.6 terms 【任一】

specify multiple values to match.
If the field contains any of the specified values, the document matches:

{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}

4.7 exists and missing

{    "exists":   {        "field":    "title"    }}

5 Combining queries together

must
must_not
should,If these clauses match, they increase the _score; otherwise, they have no effect.
filter ,must match, but are run in non-scoring, filtering mode.

提示:
If there are no must clauses, at least one should clause has to match. However, if there is at least one must clause, no should clauses are required to match.

Adding a filtering query

{    "bool": {        "must":     { "match": { "title": "how to make millions" }},        "must_not": { "match": { "tag":   "spam" }},        "should": [            { "match": { "tag": "starred" }}        ],        "filter": {          "range": { "date": { "gte": "2014-01-01" }}         }    }}

If you need to filter on many different criteria, the bool query itself can be used as a non-scoring query.

{    "bool": {        "must":     { "match": { "title": "how to make millions" }},        "must_not": { "match": { "tag":   "spam" }},        "should": [            { "match": { "tag": "starred" }}        ],        "filter": {          "bool": {               "must": [                  { "range": { "date": { "gte": "2014-01-01" }}},                  { "range": { "price": { "lte": 29.99 }}}              ],              "must_not": [                  { "term": { "category": "ebooks" }}              ]          }        }    }}

constant_score Query
The query applies a static, constant score to all matching documents.

You can use this instead of a bool that only has filter clauses. Performance will be identical, but it may aid in query simplicity/clarity.

{    "constant_score":   {        "filter": {            "term": { "category": "ebooks" }         }    }}

A term query is placed inside the constant_score, converting it to a non-scoring filter.

6 Validating Queries

check whether a query is valid.

GET /gb/tweet/_validate/query{   "query": {      "tweet" : { // 和match颠倒了         "match" : "really powerful"      }   }}{  "valid" :         false,  "_shards" : {    "total" :       1,    "successful" :  1,    "failed" :      0  }}

Understanding Errors

GET /gb/tweet/_validate/query?explain {...}{  "valid" :     false,  "_shards" :   { ... },  "explanations" : [ {    "index" :   "gb",    "valid" :   false,    "error" :   "org.elasticsearch.index.query.QueryParsingException:                 [gb] No query registered for [tweet]"  } ]}

Understanding Queries

GET /_validate/query?explain{   "query": {      "match" : {         "tweet" : "really powerful"      }   }}{  "valid" :         true,  "_shards" :       { ... },  "explanations" : [ {    "index" :       "us",    "valid" :       true,    "explanation" : "tweet:really tweet:powerful"  }, {    "index" :       "gb",    "valid" :       true,    "explanation" : "tweet:realli tweet:power"  } ]}
0 0
原创粉丝点击