salvador
salvador

Reputation: 1089

Retrieve a document from elastic search by matching two fields

My data are stored in elastic search as shown below

{  
   "identifier":{  
      "source":"source 1",
      "id":"22081070"
   },
   "title":"Book 1",
   "published":2011,
   "types":[  
      "type1",
      "type2, 
      "type3"
   ]
}

Is there a way to retrieve a document with specific "identifier.id" and "identifier.source" parameters? For example I am retrieving the above document with its id as an input with the following:

QueryBuilder queryBuilder = QueryBuilders.matchQuery("identifier.id", "22081070");
SearchResponse searchResponse = client.prepareSearch("test-index")
                .setTypes("type").setQuery(queryBuilder).execute().actionGet();

but I know know how to add the "identifier.source" as a match parameter.

Upvotes: 1

Views: 127

Answers (1)

Andrei Stefan
Andrei Stefan

Reputation: 52368

Try this:

BoolQueryBuilder boolQuery = new BoolQueryBuilder();
QueryBuilder queryBuilder1 = QueryBuilders.matchQuery("identifier.id", "22081070");
QueryBuilder queryBuilder2 = QueryBuilders.matchQuery("identifier.source", "source 1");

boolQuery.must(queryBuilder1).must(queryBuilder2);

SearchResponse searchResponse = client.prepareSearch("test-index")
                .setTypes("type").setQuery(boolQuery).execute().actionGet();

Upvotes: 2

Related Questions