ES基于Request Body的match、multi_match、query_string、match_phrase、range查询

in ELK with 0 comment

Request Body查询

query

  • match 单条件查询
  • query_string 多字段 AND、OR条件查询
  • multi_match 多字段查询
  • match_phrase 短语匹配查询

_source

查询部分列

sort

排序

分页

from:从第n个开始
size:数量

查询包含beautiful,mind的电影

GET movies/_search
{
  "query": {
    "match": {
      "title": "beautiful mind"
    }
  }
}

查询名字中包含beautiful mind短语的电影

GET movies/_search
{
  "query": {
    "match_phrase": {
      "title": "beautiful mind"
    }
  }
}

查询2016-2018年的电影,并按照年份排序

GET movies/_search
{
  "query": {
    "range": {
      "year": {
        "gte": 2016,
        "lte": 2018
      }
    }
  },
  "sort": [
    {
      "year": {
        "order": "desc"
      }
    }
  ]
}
  • lt:less than
  • lte:less than or equals
  • gt:greater than
  • gte:greater than or equals
  • eq:equals
  • desc 降序
  • asc 升序

只查询部分列

GET movies/_search
{
  "_source": ["title","year"]  
} 

多字段同时匹配某些字符串

GET movies/_search
{
    "query": {
        "multi_match": {
        "query": "beautiful mind Romance",
        "fields": ["title", "genre"],
        "type": "best_fields"
        }
    }
}

其中type的值有三个:

  • most_fields:在多字段中匹配的越多排名越靠前
  • best_fields: 能完全匹配的文档,排名越靠前。
  • cross_fields: 查询越分散,排名越靠前。

查询既包含beautiful又包含mind的电影,使用query_string

GET movies/_search
{
    "query": {
    "query_string": {
    "default_field": "title",
    "query": "Beautiful Mind",
    "default_operator": "AND" #不加默认是OR
        }
    }
}
GET movies/_search
{
    "query": {
        "query_string": {
        "fields": ["title", "genre"],
        "query": "Beautiful Mind",
        "default_operator": "AND"
        }
    }
}
Responses