I have the following code working fine and returning the documents. I want to search based on only one field
from the document and shall return the value of that field from all the documents.
RestHighLevelClient client;
QueryBuilder matchQueryBuilder = QueryBuilders.queryStringQuery("elon");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest(ELASTIC_SEARCH_INDEX);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
I need it to search only Name
field and return the value of that field from all documents that returned in this search.
let say, I query Elon
and it should response a List<String>
"lily Elon musk", "parker Elon ",Elon musk 77" and ignore other fields.
Mapping
{
students: {
mappings: {
properties: {
students: {
properties: {
courseTitle: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
},
courseCode: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
},
name: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
},
courseGrade: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
}
}
}
}
}
}
}
Sample document
students: [
{
courseCode: " HM101",
courseTitle: "ICP",
courseGrade: "A"
},
{
courseCode: " CS101",
courseTitle: "electronice",
courseGrade: "B+"
},
{
name: "elon musk"
}]
It works with
String[] includeFields = new String[]{"students.name"};
sourceBuilder.fetchSource(includeFields, null);