0

code is below

r = [{'eid': '1', 'data': 'Health'},
 {'eid': '2', 'data': 'countries'},
 {'eid': '3', 'data': 'countries currency'},
 {'eid': '4', 'data': 'countries language'}]
from elasticsearch import Elasticsearch
es = Elasticsearch()
es.cluster.health()
es.indices.create(index='my-index_1', ignore=400)

for e in enumerate(r):
    es.index(index="my-index_1", body=e[1])
search1 = es.search(index="my-index_1", body={'query': {'term' : {'data.keyword': 'Health'}}})
search1

First time out is below

{'took': 0,
 'timed_out': False,
 '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
 'hits': {'total': {'value': 0, 'relation': 'eq'},
  'max_score': None,
  'hits': []}}

Second time

{'took': 0,
 'timed_out': False,
 '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
 'hits': {'total': {'value': 1, 'relation': 'eq'},
  'max_score': 1.2039728,
  'hits': [{'_index': 'my-index_1',
    '_type': '_doc',
    '_id': 'Rov4UHMBpo0uANDoY2_5',
    '_score': 1.2039728,
    '_source': {'eid': '1', 'data': 'Health'}}]}}

Third time

{'took': 0,
 'timed_out': False,
 '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
 'hits': {'total': {'value': 2, 'relation': 'eq'},
  'max_score': 1.2809337,
  'hits': [{'_index': 'my-index_1',
    '_type': '_doc',
    '_id': 'Rov4UHMBpo0uANDoY2_5',
    '_score': 1.2809337,
    '_source': {'eid': '1', 'data': 'Health'}},
   {'_index': 'my-index_1',
    '_type': '_doc',
    '_id': 'aov4UHMBpo0uANDonm_E',
    '_score': 1.2809337,
    '_source': {'eid': '1', 'data': 'Health'}}]}}

​Below tag are keep on repating while hitting again and again

{'_index': 'my-index_1',
    '_type': '_doc',
    '_id': 'aov4UHMBpo0uANDonm_E',
    '_score': 1.2809337,
    '_source': {'eid': '1', 'data': 'Health'}}

Is it because of enumerate?. My input is list of dictionary then which having multiple keys, otherwise how to parse this?

My expected out is it should show only one time for every hit ?

  • That's the metadata returned by elasticsearch – bigbounty Jul 15 '20 at 05:47
  • @bigbounty, yes, but it should return its matching, why it keeps on inserting the document into es –  Jul 15 '20 at 06:32
  • It's the metadata that tells ES uses to search when you fire a query – bigbounty Jul 15 '20 at 06:34
  • What is your document id? Every time you insert, it will create a new doc unless u specify some upsert condition – Gibbs Jul 15 '20 at 06:36
  • @Gibbs go through the link `https://stackoverflow.com/questions/33226831/how-to-use-python-elasticsearch-client-upsert-api` teied inseting 'doc_as_upsert':True but could not save. any blog or code snippet will help –  Jul 15 '20 at 06:57
  • @bigbounty, agreed but it should only appear once right, but why it keeps on repeating on every hit? –  Jul 15 '20 at 07:06
  • @Nons Every document has the metadata associated with it. Hence, for every doc you index, it will be created – bigbounty Jul 15 '20 at 07:23

0 Answers0