简体   繁体   中英

How to store key-value pairs in an elasticsearch index using python

I want to store the contents of the dict into an elasticsearch index as follows . Is it correct or is there a better way to do it .

    def process(self, inputDict):
       for k, v in inputDict.items():
        # for each key-value pair, store it as a field and string inside the specified index of elastic search.
          key1=k
          value1=v
          doc={
            "key1" : "value" ,
            }
          self.es.index(index='test-index2',doc_type='exdoc', id=1, body=doc)
    pass;

First of all: Did you try your code? I tried it and it ran without errors. That means that there must be some documents in the index test-index2 . Running it inside idle I got the the following output:

{'_index': 'test-index2', '_type': 'exdoc', '_id': '1', '_version': 1, '_shards': {'failed': 0, 'total': 2, 'successful': 1}, 'created': True}
{'_index': 'test-index2', '_type': 'exdoc', '_id': '1', '_version': 2, '_shards': {'failed': 0, 'total': 2, 'successful': 1}, 'created': False}
{'_index': 'test-index2', '_type': 'exdoc', '_id': '1', '_version': 3, '_shards': {'failed': 0, 'total': 2, 'successful': 1}, 'created': False}

See that _version field in there? That looks suspicious. Querying elasticsearch from sense with

GET test-index2/exdoc/_search
{
  "query": {
    "match_all": {}
  }
}

will give you the following output:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "test-index2",
        "_type": "exdoc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "key1": "value"
        }
      }
    ]
  }
}

There is only one document there: {"key1": "value"} . So you are always sending the same document - ignoring the keys and values from inputDict - for the same id ( id=1 ) to elasticsearch. I think you wanted something like this instead:

def process(self, inputDict):
       i = 1 
       for k, v in inputDict.items():
        # for each key-value pair, store it as a field and string inside the specified index of elastic search.
          doc={
            k: v
            }
          self.es.index(index='test-index2',doc_type='exdoc', id=i, body=doc)
          i += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM