How do I convert this kind of query.
{
"query": {
"nested": {
"path": "consultations",
"query": {
"bool": {
"must": [
{
"match": {
"consultations.prescriptions": "alfuorism"
}
},
{
"match": {
"consultations.Diagnosis": "Fever"
}
}
]
}
}
}
}
}
To a Java Client query using QueryBuilders
You can search nested fields using dot notation that includes the complete path, such as obj1.name . Multi-level nesting is automatically supported, and detected, resulting in an inner nested query to automatically match the relevant nesting level, rather than root, if it exists within another nested query.
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.
When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .
The folowing Java code will generate your query
public NestedQueryBuilder nestedBoolQuery(final Map<String, String> propertyValues, final String nestedPath) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
Iterator<String> iterator = propertyValues.keySet().iterator();
while (iterator.hasNext()) {
String propertyName = iterator.next();
String propertValue = propertyValues.get(propertyName);
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(propertyName, propertValue);
boolQueryBuilder.must(matchQuery);
}
return QueryBuilders.nestedQuery(nestedPath, boolQueryBuilder);
}
The parameter propertyValues
is:
Map<String, String> propertyValues = new HashMap<String, String>();
propertyValues.put("consultations.prescriptions", "alfuorism");
propertyValues.put("consultations.Diagnosis", "Fever");
The parameter nestedPath
is:
consultations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With