部分匹配弹性搜索查询中的要求

时间:2017-08-13 02:35:27

标签: elasticsearch dsl

我试图根据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,它将返回其dependentClassName中包含java或lang或String的任何类型。我认为这是因为“/”。我该如何纠正这个?

修改

我使用此查询进行映射,

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

1 个答案:

答案 0 :(得分:1)

您可以将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"
                }
              }
            }
          ]
        }
    }
}