search results handle no-id case poorly #59

willkg opened this Issue Aug 24, 2012 · 2 comments

1 participant

Mozilla member
# This errors out.

import elasticutils

elasticutils.get_es().index({'object_id': 'id2'}, 'some_object_index', 'object', id='query_hash_1')
elasticutils.get_es().index({'object_id': 'some_object_id'}, 'some_object_index', 'object', id='query_hash_2')

[a for a in elasticutils.S().filter(object_id='some_object_id').indexes('some_object_index')]

# This works.

import elasticutils

elasticutils.get_es().index({'id': 1, 'object_id': 'id2'}, 'some_object_index', 'object', id='query_hash_1')
elasticutils.get_es().index({'id': 2, 'object_id': 'some_object_id'}, 'some_object_index', 'object', id='query_hash_2')

[a for a in elasticutils.S().filter(object_id='some_object_id').indexes('some_object_index')]

What's going on is that we default to asking for at least the id field, but there is no id field. In the case where the list of fields don't exist, ES doesn't return a fields key in the result dict. Need to handle that case better.

Mozilla member

The pyelasticsearch bulk_index method has an id_field argument. I'm thinking having something like that could work here, too. Perhaps:


And we could add get_id_field to MappingType for typed Ss.

Mozilla member

Here's some updated code that should error out:

import elasticutils

es = elasticutils.get_es(urls=['http://localhost:9200'])

es.index('testindex', 'testtype', {'object_id': 'id2'}, id=1)
es.index('testindex', 'testtype', {'object_id': 'foo'}, id=2)

s = elasticutils.S().es(urls=['http://localhost:9200']).indexes('testindex').doctypes('testtype')
print s

That works now. It returns a list of DefaultMappingType objects which are dict-like and don't have anything to do with the id field.

On the Django side of things, it uses self._id which is the id field that comes back from ElasticSearch--it's not part of the source document.

I think this is all set now. Closing it out.

@willkg willkg closed this Jan 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment