ES 基本使用《三》--terms

1.查找多个精确值

 term 查询对于查找单个值非常有用,但通常我们可能想搜索多个值。 如果我们想要查找价格字段值为 $20 或 $30 的文档该如何处理呢?

不需要使用多个 term 查询,我们只要用单个 terms 查询(注意末尾的 s ), terms 查询好比是 term 查询的复数形式(以英语名词的单复数做比)。

它几乎与 term 的使用方式一模一样,与指定单个价格不同,我们只要将 term 字段的值改为数组即可:

{
    "terms" : {
        "price" : [20, 30]
    }
}

带评分的DSL:

GET /my_store/products/_search
{
  "query": {
    "terms": {
       "price": [
          "20",
          "30"
       ]
    }   
  }
  }


不带评分:

GET /my_store/products/_search
{
   "query": {
      "constant_score": {
         "filter": {
            "terms": {
               "price": [
                  "20",
                  "30"
               ]
            }
         }
      }
   }
}



猜你喜欢

转载自blog.csdn.net/jjshouji/article/details/78287411
今日推荐