term级别的查询不会对search term作任何分词。

exists query

  1. GET /_search
  2. {
  3. "query": {
  4. "exists": {
  5. "field": "user"
  6. }
  7. }
  8. }
  9. 返回存在user字段的doc
  10. GET /_search
  11. {
  12. "query": {
  13. "bool": {
  14. "must_not": {
  15. "exists": {
  16. "field": "user.id"
  17. }
  18. }
  19. }
  20. }
  21. }
  22. 查找没有user.id字段的文档

fuzzy query

返回包含与查询term近似的term的文档。近似程度用 Levnshtein edit distance来衡量

GET /_search
{
  "query": {
    "fuzzy": {
      "user.id": {
        "value": "ki",
        "fuzziness": "AUTO",
        "max_expansions": 50,
        "prefix_length": 0,
        "transpositions": true,
        "rewrite": "constant_score"
      }
    }
  }
}

IDs query

查找对应id的文档,实际上查找文档的_id字段

GET /_search
{
  "query": {
    "ids" : {
      "values" : ["1", "4", "100"]
    }
  }
}

prefix query

前缀查询

GET /_search
{
  "query": {
    "prefix": {
      "user.id": {
        "value": "ki"
      }
    }
  }
}

通过设置field 参数index_prefixes来加速prefix查询效率,如果设置为enable, es在索引“term”时还会额外帮你索引“t”, “te”, “ter”

range query

regexp query

正则表达式查询

term query

精确查询
返回指定字段中包含精确term的文档(不对term查询的条件进行任何形式的分词修改)

查找user.id字段中包含kimchy的文档
GET /_search
{
  "query": {
    "term": {
      "user.id": {
        "value": "kimchy",
        "boost": 1.0
      }
    }
  }
}

可以简写成
GET /_search
{
  "query": {
    "term": {
      "user.id":  "kimchy"
    }
  }
}

不用对text字段做term查询,text字段 index中保存的是分词后的term,不能进行精确查找
尽量只对keyword字段做term查询

terms query

GET /_search
{
  "query": {
    "terms": {
    #匹配任意一个term都行
      "user.id": [ "kimchy", "elkbee" ],
      "boost": 1.0
    }
  }
}

可以指定一个文档的一个字段的terms用来做terms查询

GET my-index-000001/_search?pretty
{
  "query": {
    "terms": {
        "color" : {
        //用my-index-000001索引中id为2文档的color字段的值做terms查询
            "index" : "my-index-000001",
            "id" : "2",
            "path" : "color"
        }
    }
  }
}

terms set query

与terms查询差不多,但可以指定最少要包含的term数量。返回的文档不仅需要包含search term set中的term而且还是满足一定的数量要求

GET /job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "c++", "java", "php" ], //programming_languages不需要至少包含required_matches个term.
        "minimum_should_match_field": "required_matches" //required_matches是文档中指定的数量限制字段
      }
    }
  }
}
GET /job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "c++", "java", "php" ],
        "minimum_should_match_script": {
        //为了防止required_mathes比"terms"指定的term列表的长度还大,可以用script指定必须匹配的最小数量
        //params.num_terms指的是terms.length
          "source": "Math.min(params.num_terms, doc['required_matches'].value)" 
        },
        "boost": 1.0
      }
    }
  }
}

wildcard query

The following search returns documents where the user.id field contains a term that begins with ki and ends with y. These matching terms can include kiy, kity, or kimchy.

GET /_search
{
  "query": {
    "wildcard": {
      "user.id": {
        "value": "ki*y",
        "boost": 1.0,
        "rewrite": "constant_score"
      }
    }
  }
}