Elasticsearch匹配或术语始终失败

时间:2018-03-02 11:12:49

标签: elasticsearch

我遇到了一个没有返回结果的查询问题。当我使用matchterm执行以下查询时:

{
    "size": 1, 
    "query": {
        "bool": {
            "must": [
                { "term": { "ALERT_TYPE.raw": "ERROR" }}
            ],
            "filter": [
                { "range": {
                    "@timestamp": {
                        "gte": "2018-02-01T00:00:01.000Z",
                        "lte": "2018-02-28T23:55:55.000Z"
                    }
                }}
            ]
        }
    }
}

我总是收到以下回复:

{
    "took": 92,
    "timed_out": false,
    "_shards": {
        "total": 215,
        "successful": 215,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

但我确定该元素存在,因为当我执行match_all查询时,第一个命中如下:

{
    "took": 269,
    "timed_out": false,
    "_shards": {
        "total": 210,
        "successful": 210,
        "failed": 0
    },
    "hits": {
        "total": 68292,
        "max_score": 1,
        "hits": [
        {
            "_index": "logstash-2018.02.22",
            "_type": "alert",
            "_id": "AWEdVphtJjppDZ0FiAz-",
            "_score": 1,
            "_source": {
            "@version": "1",
            "@timestamp": "2018-02-22T10:07:41.549Z",
            "path": "/something",
            "host": "host.host",
            "type": "alert",
            "SERVER_TYPE": "STANDALONE",
            "LOG_FILE": "log.log",
            "DATE": "2018-02-22 11:02:02,367",
            "ALERT_TYPE": "ERROR",
            "MESSAGE": "There is an error"
            }
        }
        ]
    }
}

在这里,我可以看到该字段是我期待的价值。从映射中我知道该字段由默认分析器进行分析,并且未对原始字段进行分析(感谢Glenn Van Schil的回答)。映射是由logstash动态生成的,但对于我正在研究的类型,它看起来像这样:

  "alert": {
    "_all": {
      "enabled": true,
      "omit_norms": true
    },
    "dynamic_templates": [
      {
        "message_field": {
          "mapping": {
            "index": "analyzed",
            "omit_norms": true,
            "fielddata": { "format": "disabled" },
            "type": "string"
          },
          "match": "message",
          "match_mapping_type": "string"
        }
      },
      {
        "string_fields": {
          "mapping": {
            "index": "analyzed",
            "omit_norms": true,
            "fielddata": { "format": "disabled" },
            "type": "string",
            "fields": {
              "raw": {
                "index": "not_analyzed",
                "ignore_above": 256,
                "type": "string"
              }
            }
          },
          "match": "*",
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "@timestamp": { "type": "date", "format": "strict_date_optional_time||epoch_millis" },
      "@version": { "type": "string", "index": "not_analyzed" },
      "ALERT_TYPE": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "DATE": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "LOG_FILE": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "MESSAGE": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "SERVER_TYPE": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "geoip": {
        "dynamic": "true",
        "properties": {
          "ip": { "type": "ip" },
          "latitude": { "type": "float" },
          "location": { "type": "geo_point" },
          "longitude": { "type": "float" }
        }
      },
      "host": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "path": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      },
      "type": {
        "type": "string",
        "norms": { "enabled": false },
        "fielddata": { "format": "disabled" },
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed",
            "ignore_above": 256
          }
        }
      }

有没有人知道为什么这个查询一直没有返回?也许我错过了映射中的某些内容,这解释了matchterm查询失败的原因?我已经不知道发生了什么,我对elasticsearch和logstash很新。

工具和环境的版本:

  • 操作系统:RHEL Server 6.5(圣地亚哥)
  • Java: 1.7.0_91
  • Elasticsearch: 2.4.6
    • Lucene: 5.5.4
  • Logstash: 2.4.1

1 个答案:

答案 0 :(得分:1)

这不是一个真正的答案,但将此作为评论写得很复杂。

  

从映射我知道该字段未被分析。

您正在搜索ALERT_TYPE,但实际上已使用default analyzer对此进行了分析,因为您未在ALERT_TYPE映射下直接指定任何分析器。

但是,您的ALERT_TYPE有一个名为raw内部字段,未进行分析。如果您想使用原始字段搜索文档,则需要从

更改查询
"must": [
    { "term": { "ALERT_TYPE": "ERROR" }}
]

"must": [
    { "term": { "ALERT_TYPE.raw": "ERROR" }}
]