elasticsearch - 简单查询与一个特定字段不匹配

标签 elasticsearch elasticsearch-5

我将一些任意数据扔进elasticsearch(日志)。写入进行得很好,大多数查询都能正常工作,但是我有一个“reqId”字段,该字段永不匹配。

$ curl localhost:9200/log_general/log/c9811a1a-6710-424a-b67d-d02d6ad75c89 | jq .
{
  "_index": "log_general",
  "_type": "log",
  "_id": "c9811a1a-6710-424a-b67d-d02d6ad75c89",
  "_version": 1,
  "found": true,
  "_source": {
    "body": {
      "body": {
        "media": [],
        "parentId": "5a695c7bda3c26391649e332",
        "text": "Super bulk comment 25"
      },
      "method": "post",
      "url": "/addComment",
      "xuserid": "5a695c30da3c26391649e17f"
    },
    "logType": "request_start",
    "reqId": "5T42Q1AUmd9LS1E8Q",
    "reqUrl": "/addComment"
  }
}

我可以尝试通过 reqId 搜索
curl -XPOST localhost:9200/_search -H 'content-type: application/json' --data-binary @sample-query

样本查询:
{
  "query": {
    "bool": {
      "must": [
      {
        "term": {
          "reqId": "5T42Q1AUmd9LS1E8Q"
        }
      }
      ],
      "filter": [],
      "should": []
    }
  }
}

没有命中,也没有错误。

如果我尝试其他字段,它将返回结果。其中两个结果具有相同的reqId。
{
  "query": {
    "bool": {
      "must": [
      {
        "term": {
          "logType": "request_start"
        }
      }
      ],
      "filter": [],
      "should": []
    }
  }
}

这是为两个字段生成的映射elasticsearch
      "logType": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "reqId": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },

真的不知道问题可能在这里。

最佳答案

在 flex 搜索6中,为每个字符串字段生成两种类型的映射。
一个是文本,另一个是关键字。

  • 这里的reqId字段是文本类型->并且defualt分析器是标准的,因此实际生成的 token 为5t42q1aumd9ls1e8q。
  • 您正在执行术语查询的查询,它查找确切的术语并且不分析搜索。
    所以发生的是被索引的 token 是 5t42q1aumd9ls1e8q ,您正在搜索5T42Q1AUmd9LS1E8Q

  • 可以有两种解决方案
  • 您可以在字段 reqId 上使用匹配查询。这将分析搜索字符串,并且与确切的索引标记匹配。
  • 否则,您将搜索未分析的 reqId.keyword字段
  • 关于elasticsearch - 简单查询与一个特定字段不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48474710/

    相关文章:

    Python Elasticsearch : BulkIndexError: 'not_x_content_exception' while trying to use helpers. 批量

    python - 在 Django 模板中访问带有前导下划线的字典元素

    java - Elasticsearch 5 卡住从磁盘读取

    elasticsearch - 如何在 Elasticsearch 中调整文本字段的max_gram?

    elasticsearch - 将多类型索引迁移到单类型索引

    sorting - ElasticSearch-排序不起作用

    elasticsearch - 将 PCAP 导入 Elasticsearch

    ElasticSearch 嵌套排序GeoDistance

    java - 为什么在 Spring Boot 中无法加载 Elasticsearch 节点会出现此错误?

    elasticsearch - 为什么需要过滤器聚合?