【问题标题】:Multi-field Search with Array Using 'AND' Operator in elasticsearch在 elasticsearch 中使用“AND”运算符使用数组进行多字段搜索
【发布时间】:2015-05-22 06:33:43
【问题描述】:

我想以与查询其他字段相同的方式将多值字段的值查询为单独的“字段”。 我有一个像这样的数据结构:

{
  name: 'foo one',
  alternate_name: 'bar two',
  lay_name: 'baz three',
  tags: ['stuff like', 'this that']
}

我的查询如下所示:

{
  query:
    query: stuff
    type: 'best_fields',
    fields: ['name', 'alternate_name', 'lay_name', 'tags'],
    operator: 'and'
}

“类型”和“运算符”仅在值包含我的整个查询时才匹配单值字段。例如,查询 'foo two' 不会返回匹配项。

我希望标签字段的行为方式相同。现在,查询“stuff that”将在不应该返回匹配项时返回匹配项,因为没有字段或标记值在单个值中包含两个单词。有没有办法做到这一点?

编辑 瓦尔的评估是正确的。我已将映射更新为以下内容(使用 elasticsearch-rails/elasticsearch-model):

mapping dynamic: false, include_in_all: true do
  ... other fields ...
  indexes :tags, type: 'nested' do
    indexes :tag, type: 'string', include_in_parent: true
  end
end

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    请显示您的映射类型,但我怀疑您的 tags 字段是一个简单的 string 字段,如下所示:

    {
        "your_type" : {
            "properties" : {
                "tags" : {
                    "type" : "string"
                }
            }
        }
    }
    

    在这种情况下,ES 将在索引时“展平”tags 字段中的所有标签,如下所示:

    tags: "stuff", "like", "this", "that"
    

    即这就是您在查询“stuff that”时得到结果的原因,因为tags 字段包含这两个词。

    前进的方向是让tags 成为nested object type,像这样

    {
        "your_type" : {
            "properties" : {
                "tags" : {
                    "type" : "nested",
                    "properties": {
                        "tag" : {"type": "string" }
                    }
                }
            }
        }
    }
    

    您需要重新索引您的数据,但至少查询tags: "stuff that" 将不再返回任何内容。您的标签令牌将按照您的预期“保存在一起”。试试看吧。

    【讨论】:

    • 你是正确的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2015-12-14
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多