部分符合弹性搜索查询中的要求

Partially matches the requirement in elastic-search query

我正在尝试根据 2 个条件从 elasticsearch 检索数据,它应该匹配 jarFileName 和 dependentClassName。该查询使用 jarFileName 运行良好,但它部分匹配 dependendentClassName。

这是我使用的查询。

{
"query": {
"bool": {
  "must": [
    {
      "match": {
        "dependencies.dependedntClass": "java/lang/String"
      }
    },
    {
      "match": {
        "JarFileName": {
          "query": "Client.jar"
         }
       }
     }
     ]
   }
  }
}

查询完全匹配 jarFileName 但对于 dependentClassName 它甚至匹配并返回所提到的值的任何部分。例如,如果我使用 java/lang/String,它会 returns 任何在其 dependentClassName 中具有 java 或 lang 或 String 的类型。我认为这是因为“/”。我该如何更正这个?

编辑

我使用此查询进行映射,

{
"classdata": {
"properties": {
  "dependencies": {
    "type": "object",
    "properties": {
      "dependedntClass": {
        "type": "string",
        "index": "not_analyzed"
      }
     }
    }
   }
  }
}

您可以将 dependencies.dependedntClass 的索引设置为 not_analyzed 这样您给定的字符串就不会被 standard analyzer 分析。如果您使用 ES 2.x 那么下面的映射应该可以正常工作。

PUT /your_index
{
    "mappings": {
        "your_type":{
            "properties": {
                "dependencies":{
                    "type": "string",
                    "fields": {
                        "dependedntClass":{
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }
    }
}

那么,您的查询应该也能正常工作。

编辑(如果dependencies字段是nested类型)

如果您的 dependencies 字段是 nested 或数组类型,则将映射更改为:

POST /your_index
{
    "mappings": {
        "your_type":{
            "properties": {
                "dependencies":{
                    "type": "nested",
                    "properties": {
                        "dependedntClass":{
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }
    }
}

并且查询应该如下更改:

GET /your_index/_search
{
    "query": {
        "bool": {
          "must": [
            {
              "nested": {
                   "path": "dependencies",
                   "query": {
                       "match": {
                          "dependencies.dependedntClass": "java/lang/String"
                       }
                   }
                }
            },
            {
              "match": {
                "JarFileName": {
                  "query": "Client.jar"
                }
              }
            }
          ]
        }
    }
}