【问题标题】:Elasticsearch match nested field against array of valuesElasticsearch 将嵌套字段与值数组匹配
【发布时间】:2015-11-24 04:18:26
【问题描述】:

我正在尝试使用 mongoid-elasticsearch 和 ElasticSearch 2.0 对嵌套字段应用术语查询。这已经变得非常令人沮丧,因为试错并没有带来太多回报,而且该主题上的docs 相当稀疏。 这是我的查询:

  {
    "query": {
      "nested": {
        "path": "awards",
        "query": {
          "bool": {
            "must": [
              { "match": { "awards.year":  "2010"}}
            ]
          }
        }
      },
      "nested":{
        "path": "procuring_entity",
        "query": {
          "bool": {
            "must": [
              { "terms": { "procuring_entity.country": ["ES", "PL"]}}
            ]
          }
        }
      }
    }
  }

虽然“匹配”和“术语”工作得很好,但当与“术语”查询结合使用时,它不会返回任何结果,甚至认为它应该返回。我的映射如下所示:

  elasticsearch!({
    prefix_name: false,
    index_name: 'documents',
    index_options: {
      mappings: {
        document: {
          properties: {
            procuring_entity: {
              type: "nested"
            },
            awards: {
              type: "nested"
            }
          }
        }
      }
    },
    wrapper: :load
  })

如果“嵌套”不算作分析器(据我所知不算),那么这没有问题。至于second example,我认为不是这种情况,因为它匹配的值数组来自外部。 是否可以在嵌套字段上进行术语查询?难道我做错了什么? 还有其他方法可以将嵌套字段与多个值匹配吗?

任何想法将不胜感激。

【问题讨论】:

  • 在黑暗中拍摄你能在术语查询示例中使用小写吗:` ["es", "pl"]` 因为术语查询没有被分析,可能是 procuring_entity 在编制索引时作为标准进行分析
  • 不幸的是它也不是那样工作的。

标签: elasticsearch mongoid elasticsearch-2.0


【解决方案1】:

我认为您需要为此更改 nested types 的映射 - terms query 仅适用于 not_analyzed 字段。如果您将映射更新为:

elasticsearch!({
    prefix_name: false,
    index_name: 'documents',
    index_options: {
    mappings: {
        document: {
            properties: {
                procuring_entity: {
                    type: 'nested',
                    properties: {
                        country: {
                            'type': 'string',
                            'index': 'not_analyzed'
                        }
                    }
                },
                awards: {
                    type: 'nested'
                }
            }
        }
    }
    },
    wrapper: :load
})

如果你这样做,我认为查询应该可以工作。

【讨论】:

  • 成功了。因此,必须将字段显式映射为not_analyzed,否则将对其进行分析。谢谢!
  • 没问题!这种行为可能令人惊讶。不要忘记选择这个作为答案,所以它会显示为其他人解决。
猜你喜欢
  • 2021-07-12
  • 1970-01-01
  • 2018-04-08
  • 2019-08-21
  • 2017-12-03
  • 1970-01-01
  • 2015-09-18
  • 2017-09-17
  • 1970-01-01
相关资源
最近更新 更多