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

时间:2017-11-07 14:42:23

标签: elasticsearch

映射包含不应分析的嵌套字段(不确定&not -analyzed'值是否准确)。是否可以在嵌套字段上进行精确匹配?在下面的查询中," metadata.value":" 2014.NWJSD.47"仍然得到分析。 Elasticsearch将字符串拆分为多个术语(" 2014"," NWJSD"," 47")。我尝试使用" term"而不是"匹配"但这并没有带来任何结果。

"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"
                      }
                    }
                  ]
                }
              }
            }
         }
      ]
    }
}

2 个答案:

答案 0 :(得分:4)

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

2](System.Collections.Generic.IEnumerable

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

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

答案 1 :(得分:0)

I think you are looking for a query string query. You can freely disable "analyze" option for that field in mapping option and reindex everything again but you could also check this query out:

as written here:

GET /_search
{
    "query": {
        "query_string" : {
            "query" : "your string"
        }
    }
}