Unsuccessful Search

I am using Elastic Search (and Kibana) v 6.3.0. I am unable to obtain search results through the RestHighLevelClient that appear using Kibana. Here are the steps to reproduce by issue, please let me know what you think.

Using the transport client I submit

CreateIndexRequest createIndexRequest = new 
CreateIndexRequest("phenotype");
Settings settings = Settings.builder()
        .put("index.number_of_replicas", 2)
        .put("index.number_of_shards", 3)
        .build();
createIndexRequest.settings(settings);
CreateIndexResponse createIndexResponse = 
transportClient.admin().indices().create(createIndexRequest).actionGet();

Then I submit a mapping update for a field name called key1 giving it the field type keyword. Using the Kibana Dev Tools tab and the command GET /phenotype/_mappings I can verify that both steps are successful.

I save a document to Elastic Search with the command

IndexResponse indexResponse = elasticSearchRepository.save(document1); 

which contains only the information key1: value1.

Executing the command, from Kibana,

GET /phenotype/_search
{
  "query": {
    "term" : {
      "key1" : {
        "value" : "value1",
        "boost" : 1.0
      }
    }
  }
}

I see that the correct data is returned, being

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "phenotype",
        "_type": "phenotype",
        "_id": "685c3d59-4315-4f63-bf6a-17ad8a20aede",
        "_score": 0.2876821,
        "_source": {
          "key1": "value1"
        }
      }
    ]
  }
}

But when I execute the search command through the Java REST API I receive zero search hits. This is how I do it.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
    .query(
          QueryBuilders.termQuery("key1", "value1")
    );
SearchRequest searchRequest = new SearchRequest("phenotype");
searchRequest.source(searchSourceBuilder);
return restHighLevelClient.search(searchRequest);

WHY?!

The solution was to sleep for 1 second after saving the document to ES, before searching for it.

Why does the save method receive Rest.CREATED when sleep is still required?

You can add the refresh=wait_for option in the context of tests or refresh=true to make your documents immediately visible for search.

See https://www.elastic.co/guide/en/elasticsearch/reference/6.3/docs-refresh.html

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.