Elasticsearch-dsl-py: How to search from .raw field?

Created on 31 May 2017  路  1Comment  路  Source: elastic/elasticsearch-dsl-py

Hi,

I don't see documentation for searching from .raw fields.

I'd like to search following:

{
   "match": {
        "content.raw": "Northern Ireland"
   }
}

DSL alternative:

s.query = Q('bool', must=[Q('match', content.raw='python'), Q('match', body='best')])

It gives me error on content.raw part. Is there any way? or shall I use low-level client (elasticsearch-py) in this case?

Most helpful comment

Hi,

The query you want to execute is, perfectly valid; the only problem is that python doesn't allow . in the keyword argument name. To get around that the dsl library allows you to use __ (double underscore) instead:

s.query = Q('bool', must=[Q('match', content__raw='python'), Q('match', body='best')])

which should work.

Not, however, that the .raw field is typically not_analyzed (or keyword in elasticsearch 5.x) which means it doesn't support many queries - as in you won't get back any results unless you are doing an exact match. To make this explicit I like to use term query instead of match (explicit is better than implicit), also you don't have to construct the bool query yourself as the library can do that for you:

s = s.query('term', content__raw='python').query('match', body='test')

Hope this helps.

>All comments

Hi,

The query you want to execute is, perfectly valid; the only problem is that python doesn't allow . in the keyword argument name. To get around that the dsl library allows you to use __ (double underscore) instead:

s.query = Q('bool', must=[Q('match', content__raw='python'), Q('match', body='best')])

which should work.

Not, however, that the .raw field is typically not_analyzed (or keyword in elasticsearch 5.x) which means it doesn't support many queries - as in you won't get back any results unless you are doing an exact match. To make this explicit I like to use term query instead of match (explicit is better than implicit), also you don't have to construct the bool query yourself as the library can do that for you:

s = s.query('term', content__raw='python').query('match', body='test')

Hope this helps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

njoannin picture njoannin  路  3Comments

takaomag picture takaomag  路  3Comments

gabrielpjordao picture gabrielpjordao  路  3Comments

quasiben picture quasiben  路  4Comments

ypkkhatri picture ypkkhatri  路  4Comments