条款过滤器不适用于bool查询

时间:2015-08-06 08:53:46

标签: elasticsearch

我已在elasticsearch上映射了一个名为measure的类型,其中包含variableNamesensorId字符串。

映射。

{
    "measures": {
        "mappings": {
            "summarizedmeasure": {
                "properties": {
                    "id": {
                        "type": "long"
                    },
                    "location": {
                        "type": "boolean"
                    },
                    "rawValue": {
                        "type": "string"
                    },
                    "sensorId": {
                        "type": "string"
                    },
                    "summaryTimeUnit": {
                        "type": "string"
                    },
                    "timestamp": {
                        "type": "date",
                        "format": "dateOptionalTime"
                    },
                    "value": {
                        "type": "string"
                    },
                    "variableName": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

当我执行此查询时,它可以正常运行并返回variableNameFORWARD_FLOW的所有传感器的所有度量。

{
  "query" : { 
     "bool": {
        "must": {
           "match": { "variableName":  "FORWARD_FLOW" }
        }
      }
   }
}

查询结果。

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 294409,
        "max_score": 1.0059962,
        "hits": [
            {
                "_index": "measures",
                "_type": "summarizedmeasure",
                "_id": "AU4fKBWcMpd7AZtvgiuS",
                "_score": 1.0059962,
                "_source": {
                    "id": null,
                    "sensorId": "13LA062753",
                    "variableName": "FORWARD_FLOW",
                    "rawValue": "0.01",
                    "value": "0.01",
                    "timestamp": 1434060000000,
                    "summaryTimeUnit": "DAYS"
                }
            },
            {
                "_index": "measures",
                "_type": "summarizedmeasure",
                "_id": "AU4fKAtqMpd7AZtvgitp",
                "_score": 1.0059962,
                "_source": {
                    "id": null,
                    "sensorId": "13LA062744",
                    "variableName": "FORWARD_FLOW",
                    "rawValue": "0.44",
                    "value": "0.44",
                    "timestamp": 1433973600000,
                    "summaryTimeUnit": "DAYS"
                }
            }.....

但是,当我使用terms过滤器仅搜索与特定传感器相关的度量时,它不返回任何度量。

{
   "query":{            
     "filtered":{
        "query" : { 
          "bool": {
            "must": {
               "match": { "variableName":  "FORWARD_FLOW" }
            }
          }
        }, 
        "filter": {"terms":{ "sensorId":["13LA062753","14VD021810"]}}
      }        
   }
}

我做错了什么?

谢谢你。

1 个答案:

答案 0 :(得分:1)

您需要像这样设置sensorId "index": "not_analyzed"

"sensorId": {
    "type": "string",
    "index": "not_analyzed"
}

此值默认为analyzed,术语过滤器会进行精确检查(这些术语不会通过分析器运行)。

有关您需要执行此操作的详细说明,请参阅finding exact values

或者您可以更改分析仪或小写您的术语。问题是默认分析器正在处理该字段,这会降低值。这就是为什么这个查询可以与您的映射一起使用的原因:

{
   "query":{            
     "filtered":{
        "query" : { 
          "bool": {
            "must": {
               "match": { "variableName":  "FORWARD_FLOW" }
            }
          }
        }, 
        "filter": {"terms":{ "sensorId":["13la062753","14vd021810"]}}
      }        
   }
}