1
votes

I am trying to apply term filter elastic search index data using elasticsearch_dsl python client but it's not working with string field.

This is my ES index data:

s = Search(using=es, index='idx_object', doc_type='ip').source(include=['id', 'status', 'username'])

[{u'_score': 1.0, u'_type': u'ip', u'_id': u'79', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 79}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'84', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 84}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'73', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 73}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'82', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 82}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'65', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 65}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'78', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 78}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'62', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 62}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'96', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 96}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'70', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 70}, u'_index': u'idx_object'}, {u'_score': 1.0, u'_type': u'ip', u'_id': u'80', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 80}, u'_index': u'idx_object'}]

Now if I apply a filter in id integer field it's working.

s = Search(using=es, index='idx_object', doc_type='ip').source(include=['id', 'status', 'username']).filter("term", id=79)

Output:

[{u'_score': 0.0, u'_type': u'ip', u'_id': u'79', u'_source': {u'status': u'PUBLISHED', u'username': u'[email protected]', u'id': 79}, u'_index': u'idx_object'}]

Now if I apply filter on username string field its not working:

s = Search(using=es, index='idx_object', doc_type='ip').source(include=['id', 'status', 'username']).filter("term", username='[email protected]')

Output:

[]

1

1 Answers

6
votes

Chances are username field is a text field. Term query looks for exact match i.e it will look for a token with [email protected] in reverse index

In case you are using ES 5.x, you can try changing your query to

s = Search(using=es, index='idx_object', doc_type='ip').source(include=['id', 'status', 'username']).filter("term", username.keyword='[email protected]')

In case ES version < 5.x.

  • If you want exact match You can use multi-field for creating a raw version of your field username and then apply term query on that field.

Hope this helps!