Elasticsearch:如何在嵌套字段中获得完全匹配

标签 elasticsearch

映射包含不应分析的嵌套字段(不确定“not_analyzed”值是否准确)。是否可以对嵌套字段进行精确匹配?在下面的查询中,“metadata.value”:“2014.NWJSD.47”仍然被分析。 Elasticsearch 将字符串分解为多个术语(“2014”、“NWJSD”、“47”)。我尝试使用“term”而不是“match”,但这没有返回任何结果。

"mappings" : {
  "metadata" : {
    "type" : "nested",
    "properties" : {
      "name" : {
          "type" : "text",
          "index" : "not_analyzed"
      },
      "value" : {
          "type" : "text",
          "index" : "not_analyzed"
      }
    }
  }
}

查询:

  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "metadata",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "metadata.name": "number"
                    }
                  },
                  {
                    "match": {
                      "metadata.value": "2014.NWJSD.47"
                      }
                    }
                  ]
                }
              }
            }
         }
      ]
    }
}

最佳答案

尝试在映射中使用关键字而不是文本,例如:

{
   "mappings": {
      "your_type_name": {
         "properties": {
            "metadata" : {
               "type" : "nested",
               "properties" : {
                  "name" : {
                     "type" : "keyword"
                   },
                  "value" : {
                     "type" :"keyword"
                  }
               }
             }
           }
        }
     }
  }

不会分析这些字段。然后你应该重新索引你的数据并查询你的数据你应该用 term 替换匹配(这是分析的查询)。

 {
    "query": {
       "bool": {
          "must": [
             {
                "nested": {
                   "path": "metadata",
                   "query": {
                      "bool": {
                        "must": [
                           {
                              "term": {
                                 "metadata.name": "number"
                               }
                            },
                            {
                               "term": {
                                  "metadata.value": "2014.NWJSD.47"
                               }
                            }
                         ]
                      }
                   }
                }
             }
          ]
       }
    }
 }

关于Elasticsearch:如何在嵌套字段中获得完全匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47160653/

相关文章:

elasticsearch - Elasticsearch匹配项或术语始终失败

azure - Logstash 无法向 Elasticsearch Master 发送加入请求

c# - 收到无法从_bulk重试的失败后,ElasticSearch Nest BulkAll停止

ruby-on-rails - Rails和Elasticsearch字符串的未定义方法 `delete_if'

docker - 为什么Kubernetes中的Elasticsearch集群启动如此缓慢

elasticsearch - Elasticsearch中的命名实体提取

PHP ElasticSearch如何在索引记录之前设置映射?

react-native - ReactiveSearch如何在加载时停止初始查询

elasticsearch - Elasticsearch:范围索引中的数据

elasticsearch - Elasticsearch 到许多聚合:无法连接到服务器