I am trying to replicate a in
SQL query in elastic search... something similar to select * from products where id in ('123, '345');
I have something like the below -
Set<String> searchIds = new HashSet<String>();
searchIds.add("123");
searchIds.add("456");
response = client.prepareSearch("id_index")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termsQuery("product_id", searchIds))
.setFrom(0).setSize(50).setExplain(false)
.execute().actionGet();
hits = response.getHits();
hits.forEach((h) -> {
h.getSource().entrySet().stream().forEach((e)->{
System.out.println(e.getKey()+" : "+String.valueOf(e.getValue()).replace("\n", "").replace("\t", ""));
});
});
System.out.println("Response : "+response.toString());
The response I am getting from the last System.out.println
is as follows,
Response : {
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
I know for sure there are two products with product id's 123
and 345
as I was able to see them via Kibana. But the above code returns no hits. Am I replicating the SQL IN
query in the right way?