0

this is my previous question - how to insert data in elastic search index

index mapping is as follows

{
 "test" : {
   "mappings" : {
     "properties" : {
        "name" : {
          "type" : "keyword"
        },
        "info" : {
          "type" : "nested"
        },
        "joining" : {
           "type" : "date"
        }
    }
}

how can i check the data of field is already present or not before uploading a data to the index

Note :- I dont have id field maintained in index. need to check name in each document if it is already present then dont insert document into index

thanks in advance

1 Answer 1

1

As you don't have a id field in your mapping, you have to search on name field and you can use below code to search on it.

public List<SearchResult> search(String searchTerm) throws IOException {
        SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder multiMatchQueryBuilder = new 
        MatchQueryBuilder(searchTerm, "firstName");
        searchSourceBuilder.query(matchQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = esclient.search(searchRequest, RequestOptions.DEFAULT);
        return getSearchResults(searchResponse);

    }

Note, as you have keyword field instead of match you can use termquerybuilder

And it uses the utility method to parse the searchResponse of ES, code of which is below:

private List<yourpojo> getSearchResults(SearchResponse searchResponse) {
        RestStatus status = searchResponse.status();
        TimeValue took = searchResponse.getTook();
        Boolean terminatedEarly = searchResponse.isTerminatedEarly();
        boolean timedOut = searchResponse.isTimedOut();

        // Start fetching the documents matching the search results.
        //https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search
        // .html#java-rest-high-search-response-search-hits
        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
        List<sr> sr = new ArrayList<>();
        for (SearchHit hit : searchHits) {
            // do something with the SearchHit
            String index = hit.getIndex();
            String id = hit.getId();
            float score = hit.getScore();

            //String sourceAsString = hit.getSourceAsString();
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            String firstName = (String) sourceAsMap.get("firstName");
            
            sr.add(userSearchResultBuilder.build());
        }
Sign up to request clarification or add additional context in comments.

2 Comments

class SearchResult is from which package?
@Jenny its my own class, which parses the field from SearchResponse class of Elasticsearch, so in your case, you can use your POJO which defines your index in ES

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.