在Elasticsearch中使用"AND"运算符使用数组进行多字段搜索 [英] Multi-field Search with Array Using 'AND' Operator in elasticsearch

查看:77
本文介绍了在Elasticsearch中使用"AND"运算符使用数组进行多字段搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to query the values of a multi-value field as separate 'fields' in the same way I'm querying the other fields. I have a data structure like so:

{
  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"不会返回匹配项.

The 'type' and 'operator' work perfectly for the single value fields in only matching when the value contains my entire query. For example, querying 'foo two' doesn't return a match.

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

I'd like the tags field to behave the same way. Right now, querying 'stuff that' will return a match when it shouldn't because no fields or tag values contain both words in a single value. Is there a way to achieve this?

编辑Val的评估当场.我已将映射更新为以下内容(使用 elasticsearch-rails /

EDIT Val's assessment was spot on. I've updated my mapping to the following (using 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

推荐答案

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

Please show your mapping type, but I suspect your tags field is a simple string field like this:

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

在这种情况下,ES会在索引编制时在 tags 字段中将引擎盖下的所有标签压平",如下所示:

In this case ES will "flatten" all your tags under the hood in the tags field at indexing time like this:

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

即这就是为什么在查询东西"时得到结果的原因,因为 tags 字段包含两个单词.

i.e. this is why you get results when querying "stuff that", because the tags field contains both words.

前进的方法是将 tags 设为

The way forward would be to make tags a nested object type, like this

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

您需要重新索引数据,但至少要查询 tags:东西" 将不再返回任何内容.您的标记令牌将按照您的期望保持在一起".试试看.

You'll need to reindex your data but at least querying for tags: "stuff that" will not return anything anymore. Your tag tokens will be "kept together" as you expect. Give it a try.

这篇关于在Elasticsearch中使用"AND"运算符使用数组进行多字段搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆