elasticsearch - 为什么这个Elasticsearch查询不完全匹配字符串?

标签 elasticsearch

我有一个Elasticsearch查询,该查询与它应该匹配的文档不匹配。

这是查询:

POST _search
{
   "query": {
      "filtered": {
         "filter": {
            "bool": {
               "must": {
                  "term": {
                     "signupCompany": "NewBay Media"
                  }
               }
            }
         }
      }
   }
}

但是,如果我将术语更改为:
"signupCompanyID": 643

然后找到正确的文档。这是我的映射:
"properties": {
      "body": {
        "type": "string"
      },
      "datetimeIndexed": {
        "type": "date",
        "format": "epoch_millis||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
      },
      "datetimeReceived": {
        "type": "date",
        "format": "epoch_millis||yyyy/MM/dd HH:mm:ss||yyyy/MM/dd"
      },
      "duplicateOfEmailID": {
        "type": "long"
      },
      "enabled": {
        "type": "boolean"
      },
      "htmlBodyPath": {
        "type": "string"
      },
      "plainBodyPath": {
        "type": "string"
      },
      "secondsAgoReceived": {
        "type": "long"
      },
      "senderName": {
        "type": "string",
        "fields": {
          "raw": {
            "type": "string",
            "index": "not_analyzed"
          }
        }
      },
      "signupCompany": {
        "type": "string"
      },
      "signupCompanyEnabled": {
        "type": "boolean"
      },
      "signupCompanyID": {
        "type": "long"
      },
      "signupCompanyInitial": {
        "type": "string"
      },
      "subject": {
        "type": "string"
      }
    }

我还有一些其他查询已经在使用中并且正在使用datetimeReceived字段和enabled字段进行生产,但无法使此特定字符串匹配正常工作。

它不适用于任何signupCompany,不仅限于此。

最佳答案

您需要将signupCompany的映射类型设置为not_analyzed,以便它不尝试匹配部分字符串,仅匹配确切的值。 -文档:https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-intro.html

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

关于elasticsearch - 为什么这个Elasticsearch查询不完全匹配字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712845/

相关文章:

elasticsearch - ElasticSearch-如何不存储未在静态索引中定义的字段?

json - 将JSON文件索引到elasticsearch命令/映射错误中

elasticsearch - 在 ELK 中配置 number_of_shards 和 number_of_replicas

java - 使用 Spring Boot 从 Elastic Search 查询结果中返回 List<Student>

docker - 非生产模式下的 Elasticsearch docker 容器以消除 vm.max_map_count=262144 要求

symfony - Asciifolding 不适用于 FOSElasticabundle

java - 在elasticsearch中触发多个搜索请求的问题

java - 在 Elasticsearch 中添加多个求和聚合

c# - 通用 Elasticsearch 方法 c#

elasticsearch - 带有过滤条件和字符串搜索的Elasticsearch Bool查询返回不一致的结果