0

I have the following documents inserted in an ElasticSearch domain.

{"depID:": "5656", "employeeID": "7777", "jobID": "2345"}
{"depID:": "6767", "employeeID": "9999", "jobID": "2345"}
{"depID:": "7676", "employeeID": "8888", "jobID": "2345"}

When I perform a SearchRequest on depID, I get 0 hits. But when I perform it on employeeIDs or jobIDs I'm getting the correct hits in response. I'm not sure what I'm doing wrong here. Below is how I'm creating these documents.

public IndexResponse indexDocumentRequest(final String indexName, final String jsonDocument) throws IOException {
    IndexRequest indexRequest = new IndexRequest(indexName);
    indexRequest.source(jsonDocument, XContentType.JSON);
    return restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}

IndexResponse indexResponse =
            esDao.indexDocumentRequest("employees", "{\"depID:\": \"5656\", \"employeeID\": \"7777\", \"jobID\": \"2345\"}");

Below is the method in which I'm running the SearchRequests.

public SearchResponse searchDocumentRequest(final String indexName, final String jsonDocument) throws IOException {
        QueryBuilder queryBuilder = QueryBuilders.wrapperQuery(jsonDocument);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(queryBuilder);
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices(indexName);
        searchRequest.source(searchSourceBuilder);
        return restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    }

Below is how I'm calling the above Search method.

 SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"depID\":\"5656\"}}]}}");
 SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"depID\":\"6767\"}}]}}");

None of the above calls return any hits! But the below calls on other fields return the correct hits.

SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"employeeID\":\"7777\"}}]}}");
SearchResponse searchResponse = esDao.searchDocumentRequest("employees",
            "{\"bool\":{\"must\":[{\"match\":{\"jobID\":\"2345\"}}]}}");

What am I doing wrong here? Why won't the depID searches not return any hits? Any advice would be much appreciated

UPDATE: The index mapping I have for the documents seem to be as follows.

{
  "properties": {
    "jobID": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "depID:": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "employeeID": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    }
  }
}
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118

2 Answers2

1

Can you share your index mapping that is created when you inserted the data? That may help in understanding the query results in both scenarios.

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-mapping.html

Update:

The issue is with the field name, There is a : missing in the depID in the query.

  • Sure, this is what I'm getting when I fetch the index mapping. { "properties": { "jobID": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "depID:": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } }, "employeeID": { "type": "text", "fields": { "keyword": { "ignore_above": 256, "type": "keyword" } } } } } – AnOldSoul Apr 09 '21 at 20:14
  • I tried using your documents and figured out the issue. It is in the document you are indexing. It is not depID, it is depID: with a ':' at the end. So your query should be `SearchResponse searchResponse = esDao.searchDocumentRequest("employees", "{\"bool\":{\"must\":[{\"match\":{\"depID:\":\"5656\"}}]}}");` – deepak mahapatra Apr 11 '21 at 18:53
  • A silly mistake it is. Can you update the answer with this? I'll accept it as the answer – AnOldSoul Apr 13 '21 at 21:08
0

You are indexing the documents using the indexDocumentRequest method, in the employees index and you are running search requests using the searchDocumentRequest2 method, in the candidates index.

Since both the index are different, maybe due to this, you are not getting accurate search results.

ESCoder
  • 15,431
  • 2
  • 19
  • 42