Search code examples
elasticsearchelasticsearch-dslelasticsearch-py

How to query on unanalyzed fields?


I have a few things in my elasticsearch_dsl class that i want to query for an exact match:

class Profile(DocType):
    name = String(fields={'raw': String(index='not_analyzed')})

While that does work, i always need to add a .raw to the query and cannot query name exactly:

# Matches "foo" and "foo-1"
Profile.search().filter('term', name='foo'})
# Matches nothing
Profile.search().filter('term', name='foo-1'})
# Matches what i want (only "foo-1")
Profile.search().filter('term', **{'name.raw': 'foo-1'})

This feels kinda wrong, as i should be able to just use name and not raw, because it should be the same.

What's the right way?


Solution

  • No, the correct way to use it is with name.raw because that's the field which is not_analyzed. If you use only name you are not using the not_analyzed version, you use the analyzed version with the standard analyzer.

    That's why filter('term', name='foo'}) matches both foo and foo-1.