ElasticSearch搜索term和terms的區別

今天同事使用ES查詢印地語的文章。發現查詢報錯,查詢語句和錯誤信息以下:sql

查詢語句:
{
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "update_time":{
                            "gt":"1488556800000"
                        }
                    }
                },
                {
                    "terms":{
                        "lang":1
                    }
                },
                {
                    "terms":{
                        "domain":[
                            "dailymasala.co",
                            "goldenmob.com"
                        ]
                    }
                },
                {
                    "prefix":{
                        "user_id":"errVideo_setInterval_"
                    }
                }
            ]
        }
    },
    "from":0,
    "size":10
}


錯誤信息:
{
    "error":{
        "root_cause":[
            {
                "type":"parsing_exception",
                "reason":"[terms] query does not support [lang]",
                "line":1,
                "col":93
            }
        ],
        "type":"parsing_exception",
        "reason":"[terms] query does not support [lang]",
        "line":1,
        "col":93
    },
    "status":400
}

其實這麼看上去好像並無什麼問題,可是就是查詢不成功。json

問題出在查詢lang這個字段上。數組

在查詢的字段只有一個值的時候,應該使用term而不是terms,在查詢字段包含多個的時候才使用terms(相似於sql中的in、or),使用terms語法,JSON中必須包含數組。dom

正確的寫法以下:ide

第一種(單個值,term):
{
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "update_time":{
                            "gt":"1488556800000"
                        }
                    }
                },
                {
                    "term":{
                        "lang":1
                    }
                },
                {
                    "terms":{
                        "domain":[
                            "dailymasala.co",
                            "goldenmob.com"
                        ]
                    }
                },
                {
                    "prefix":{
                        "user_id":"errVideo_setInterval_"
                    }
                }
            ]
        }
    },
    "from":0,
    "size":10
}



第二種(數組形式,terms):
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "update_time": {
              "gt": "1488556800000"
            }
          }
        },
        {
          "terms": {
            "lang": [1]
          }
        },
        {
          "terms": {
            "domain": [
              "dailymasala.co",
              "goldenmob.com"
            ]
          }
        },
        {
          "prefix": {
            "user_id": "errVideo_setInterval_"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 10
}
相關文章
相關標籤/搜索