Skip to content

1. 一个完整的查询语句应该如何写

官方文档

2. 全文查询 - 分词

1. match查询(匹配查询)

文档

match:模糊匹配,需要指定字段名,但是输入会进行分词,比如"hello world"会进行拆分为hello和world,然后匹配,如果字段中包含hello或者world,或者都包含的结果都会被查询出来,也就是说match是一个部分匹配的模糊查询。查询条件相对来说比较宽松。

shell
GET user/_search
{
  "query": {
    "match": {
      "address": "street"
    }
  }
}

2. match_phrase查询 短语查询

官方文档match_phase:会对输入做分词,但是需要结果中也包含所有的分词,而且顺序要求一样。以"hello world"为例,要求结果中必须包含hello和world,而且还要求他们是连着的,顺序也是固定的,hello that word不满足,world hello也不满足条件。

shell
GET user/_search
{
  "query": {
    "match_phrase": {
      "address": "Madison street"
    }
  }
}

3. multi_match查询

官方文档multi_match查询提供了一个简便的方法用来对多个字段执行相同的查询,即对指定的多个字段进行match查询

shell
POST resume/_doc/12
{
  "title": "后端工程师",
  "desc": "多年go语言开发经验, 熟悉go的基本语法, 熟悉常用的go语言库",
  "want_learn":"python语言"
}

POST resume/_doc/13
{
  "title": "go工程师",
  "desc": "多年开发经验",
  "want_learn":"java语言"
}

POST resume/_doc/14
{
  "title": "后端工程师",
  "desc": "多年开发经验",
  "want_learn":"rust语言"
}

GET account/_search
{
  "query": {
    "multi_match": {
      "query": "go",
      "fields": ["title", "desc"]
    }
  }
}

3. match all查询

shell
GET user/_search
{
  "query": {
    "match_all": {}
  }
}

4. term 级别查询

官方文档

1. term查询

term: 这种查询和match在有些时候是等价的,比如我们查询单个的词hello,那么会和match查询结果一样,但是如果查询"hello world",结果就相差很大,因为这个输入不会进行分词,就是说查询的时候,是查询字段分词结果中是否有"hello world"的字样,而不是查询字段中包含"hello world"的字样,elasticsearch会对字段内容进行分词,“hello world"会被分成hello和world,不存在"hello world”,因此这里的查询结果会为空。这也是term查询和match的区别。

shell
GET user/_search
{
  "query": {
    "term": {
      "address": "madison street"
    }
  }
}

2. range查询 - 范围查询

shell
GET user/_search
{
  "query":{
    "range": {
      "age": {
        "gte": 20,
        "lte": 30
      }
    }
  }
}

3. exists查询

shell
GET user/_search
{
  "query": {
    "exists": {
      "field": "school"
    }
  }
}

4. fuzzy模糊查询

编辑距离

shell
GET user/_search
{
  "query": {
    "match": {
      "address": {
        "query": "Midison streat",
        "fuzziness": 1
      }
    }
  }
}
shell
GET /_search
{
  "query": {
    "fuzzy": {
      "user.id": {
        "value": "ki"
      }
    }
  }
}

5. 复合查询

官方文档 Elasticsearch bool查询对应Lucene BooleanQuery, 格式如下

shell
{
    "query":{
        "bool":{
            "must":[
            ],
            "should":[
            ],
            "must_not":[
            ],
            "filter":[
            ],
        }
    }
}

/

must: 必须匹配,查询上下文,加分
should: 应该匹配,查询上下文,加分
must_not: 必须不匹配,过滤上下文,过滤
filter: 必须匹配,过滤上下文,过滤

bool查询采用了一种匹配越多越好的方法,因此每个匹配的must或should子句的分数将被加在一起,以提供每个文档的最终得分

shell
GET user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "state": "tn"
          }
        },
        {
          "range": {
            "age": {
              "gte": 20,
              "lte": 30
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "gender": "m"
          }
        }
      ],
      "should": [
        {
          "match": {
            "firstname": "Decker"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 25,
              "lte": 30
            }
          }
        }
      ]
    }
  }
}