My Document is like:
class Foo{
private Integer idDl;
private String Name;
private String Add;
@Field(type = FieldType.Nested)
private List< Bar> Bar;
}
class Bar{
private Integer barId;
private List<String> barData
}
and Foo sample data is like:
{
"idDl": 123,
"Name": "ABCD",
"Add": "FL",
"Bar": [
{
"barId": 456,
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": 985,
"barData": [
"Bar4",
"Bar5"
]
}
]
}
I want to return all Fooobjects where Bar.barId is matching but as Bar is list object in Foo, Foo must only contain only one Bar object who's id is matching with the id provided by user.I am using NativeSearchQueryBuilder provided by spring-data-elasticsearch as:
String[] includeFields = new String[]{"idDl", "Name"};
String[] excludeFields = new String[]{"Add"}; // to exclude Add field of Foo
Query searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchQuery("Bar.barId", 456))
//.withQuery(termQuery("Bar.barId", 456))
.withSourceFilter(new FetchSourceFilter(includeFields, excludeFields))
.build();
return elasticsearchRestTemplate.queryForList( searchQuery, Foo.class);
The response I am getting consist of all Bar objects irrespective of Id, here is the sample response:
[
{
"idDl": 123,
"Name": "ABCD",
"Add": "FL",
"Bar": [
{
"barId": 456,
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": 985,
"barData": [
"Bar4",
"Bar5"
]
}
]
},
{
"idDl": 758,
"Name": "PQR",
"Add": "NY",
"Bar": [
{
"barId": 456,
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": 671,
"barData": [
"Bar24",
"Bar25"
]
}
]
}
]
I tried using termQuery as commented in the snippet but i ain't getting response for it and for matchQuery I am getting response as above. In the response the Bar must contain only objects having the id 456 i.e. Id being sent in query. Any suggestions will be helpful