2

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']
}

My query looks like this:

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

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?

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

1 Answer 1

2

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

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

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"

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

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

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

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You were spot on. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.