Elasticsearch与分析字符串的查询结果不一致

时间:2016-08-13 01:06:37

标签: elasticsearch

我有一个属性 edApp.name 我查询匹配。我已确认映射为" type":" string"所以应该进行分析。

当我使用匹配进行查询时,每次都会得到不同数量的匹配。

无论是使用/ _search查询所有文档还是通过读取别名查询子集,我都会看到相同的行为。

较新的更新:动态映射字段似乎是罪魁祸首。该字段为 generated.edApp.name ,并使用" not_analyzed"进行动态映射。只要对包含此字段的文档编制索引, edApp.name 的分析器就会中断,我开始看到匹配查询的奇怪结果。

文件:

{
  @context: "http://purl.imsglobal.org/ctx/caliper/v1/Context",
  edApp: {
    name: "ReadingRainbow"
  }
}

映射:

"dynamic_templates": [
          {
            "string_theory": {
              "mapping": {
                "index": "not_analyzed",
                "type": "string",
                "doc_values": true
              },
              "match": "*",
              "match_mapping_type": "string"
            }
          },
          {
            "i_dont_know_you": {
              "mapping": {
                "enabled": false
              },
              "match_mapping_type": "object",
              "path_match": "*.extensions.*"
            }
          }
   ],
   "properties": {
        "_all": {
          "enabled": false
        },
        "_timestamp": {
          "enabled": true
        },
...
      "edApp": {
        "properties": {
           "name": {
              "type": "string"
           }
        }
     }
}

查询返回不一致的结果:

{
  "query": {
      "match": {
          "edApp.name": "ReadingRainbow"
      }
   }
}

多次运行查询时的hits.total值:[44,45,57,69]

术语查询返回不一致的结果:

{
    "query": {
        "bool": {
            "should": [
            {
                "term": {
                    "edApp.name": "ReadingWonders2.0"
                }
            }
            ]
        }
    }
}

多次运行术语查询时的hits.total值:[21,33,34,46]

其他术语查询返回不一致的结果(注意小写):

{
    "query": {
        "bool": {
            "should": [
            {
                "term": {
                    "edApp.name": "readingwonders2.0"
                }
            }
            ]
        }
    }
}

多次运行术语查询时的hits.total值:[44,45,57,69] 注意:这些与我们在匹配查询中看到的计数相同!

使用这两个术语进行查询:

{
    "query": {
        "bool": {
            "should": [
            {
                "term": {
                    "edApp.name": "readingwonders2.0"
                }
            },
            {
                "term": {
                    "edApp.name": "ReadingWonders2.0"
                }
            }
            ]
        }
    }
}

hits.total值是一致的:79结果

正如您所看到的,来自小写和camelcase术语搜索的不一致命中最多可添加79个文档。分析仪可能会造成这种不一致吗?

我正在使用AWS Elasticsearch Service ES 1.5.2

1 个答案:

答案 0 :(得分:1)

名为 generated.edApp.name 的动态映射属性与 edApp.name 冲突。

edApp.name 显式映射为"已分析"

generated.edApp.name 动态映射为" not_analyzed"

动态属性存在后, edApp.name 的匹配查询会中断。

我的解决方法是添加一个动态模板来处理与我的显式映射分析字符串共享名称的字段

"dynamic_templates": [
          {
            "analyzed_string_theory": {
              "mapping": {
                "index": "analyzed",
                "type": "string"
              },
              "match_pattern": "regex",
              "match": "^.*name|.*keywords|.*description$"
            }
          },
      {
        "string_theory": {
          "mapping": {
            "index": "not_analyzed",
            "type": "string"
          },
          "match_mapping_type": "string",
          "match": "*"
        }
      },
     ... 
]
相关问题