0

I am trying basic commands in elasticsearch and I am stuck with basic search.

My script:

from elasticsearch import Elasticsearch

INDEX_NAME = 'person'

es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

data = {
    'name': 'John'
}
response = es.index(index=INDEX_NAME, body=data)
print(response['result']) #=> created
id = response['_id']

response = es.get(index=INDEX_NAME, id=id)
print(response['_source']) #=> {'name': 'John'}

query = {
    'query': {
        'match_all': {}
    }
}
response = es.search(index=INDEX_NAME, body=query)
print(response) #=> {'took': 0, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 0, 'relation': 'eq'}, 'max_score': None, 'hits': []}}
print(response['hits']['hits']) #=> []

According to debug logs, a document is created, es.get can find it in the index, but es.search cannot. Does anyone know where the problem is?

I also tried update and delete and both worked. Docs was not really helpful.

EDIT:

Search works in Kibana:

GET person/_search
{
  "query": {
    "match_all": {}
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "person",
        "_type" : "_doc",
        "_id" : "3EZ1s3gBsIJkcgJzc150",
        "_score" : 1.0,
        "_source" : {
          "name" : "John"
        }
      }
    ]
  }
}

So it must be something Python specific.

dom
  • 414
  • 3
  • 12

1 Answers1

0

It was a bug in PyCharm apparently. I restarted the IDE and it works.

dom
  • 414
  • 3
  • 12