markdown [查询DSL]弹性搜索查询DSL #elasticsearch

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown [查询DSL]弹性搜索查询DSL #elasticsearch相关的知识,希望对你有一定的参考价值。


**Find documents that matches a 'FIELD' with an 'VALUE' (match query)**
**@6.2

``` json
GET /_search
{
    "query": {
        "match" : {
            "FEILD" : {
              
            "query": "VALUE",
          	
            "operator" : "", // (default: ) (and,or)
          
          	// set an analyzer to perform analysis
            "analyzer":"", // (default: search analyzer)
          
          	// if true, ignores exceptions caused by data_type mismatches
          	"lenient":"", // (default: false)
          
            // allows fuzzy matching based on the type of feild
          	"fuzziness":"",  // (default: AUTO:3,6) (0,1,2,AUTO:[low],[high])
          	"prefix_length":"",     // prefix lenght for fuzziness
          	"max_expansions":"",    // max expansion for fuzziness
          
          	// if no document matches, returns all document
          	"zero_terms_query":"", // (default: none) (none,all)
          
            "cutoff_frequency":
            }
        }
    }
}
```

(or)

``` json 
GET /_search
{
    "query": {
        "match" : {
            "message" : "this is a test"
        }
    }
}

```
**Query by matching documents and Filter it's Results** @v6.2

NOTE: Use query clauses in query context for conditions which should affect the score of matching documents (i.e. how well does the document match), and use all other query clauses in filter context.

``` json
GET /_search
{
  //  Query Context (“How well does this document match this query clause?”)
  "query": { 
    "bool": { 
      "must": [
        
        // matches the document having title 'Search' and content 'Elasticsearch'
        { "match": { "title":   "Search"        }},  
        { "match": { "content": "Elasticsearch" }}  
      ],
      
      // Filter Context (“Does this document match this query clause?”)
      "filter": [ 
        
        // Filter out the result which has status 'published' and of range greater than or equal to 
        // "2015-01-01"
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}
```
**mathces and returns all the documents in an 'index'**  
**@6.2 @7.1**

``` json 
GET [index]/_search
{
    "query": { // use any one of the match_all
      
      	// matches all documents (gives _score as 1.0)
      	"match_all": {}
      
       // matches no document
       "match_none":{} 
  
  	   // imporve _score with 'boost' paramerter
  	   "match_all": { "boost" : 1.2 }
    }
}
```

**finds the documents that exactly matches the term in order**

For a document to be considered a match for the phrase “quick brown fox”, the following must be true:

quick, brown, and fox must all appear in the field.
The position of brown must be 1 greater than the position of quick.
The position of fox must be 2 greater than the position of quick.
If any of these conditions is not met, the document is not considered a match.

``` json
GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : {
                "query" : "quick brown fox",
   			
             // set an analyzer to perform analysis
            "analyzer":"", // (default: search analyzer)
            }
        }
    }
}

``` 
(OR)

``` json 
GET /_search
{
    "query": {
        "match_phrase" : {
            "message" : "this is a test"
        }
    }
}

```

**finds documents by matching perfix term**

**It can be used for real time search filtering**

It accepts the same parameters as the phrase type. In addition, it also accepts a max_expansions parameter (default 50) that can control to how many suffixes the last term will be expanded. It is highly recommended to set it to an acceptable value to control the execution time of the query.

``` json 
GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
           
            // contolls how much suffixes the last term need to expand
            "max_expansion":50
        }
    }
}

```

以上是关于markdown [查询DSL]弹性搜索查询DSL #elasticsearch的主要内容,如果未能解决你的问题,请参考以下文章

架构师成长记_第八周_13_dsl搜索-查询所有内容与分页查询

架构师成长记_第八周_13_dsl搜索-查询所有内容与分页查询

架构师成长记_第八周_13_dsl搜索-查询所有内容与分页查询

架构师成长记_第八周_15_dsl搜索- 布尔查询

关于 Elasticsearch json dsl 查询结构的困惑

如何仅查询一种类型的记录的弹性搜索?