0

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"
}]
Lily
  • 605
  • 3
  • 15
  • 31

2 Answers2

0

I need it to search only Name field and return the value of that field from all documents that returned in this search.

Not a straight-forward one. You need to change query string as below to handle only name field.

JAVA way:

QueryBuilder matchQueryBuilder = 
               QueryBuilders.queryStringQuery("elon").defaultField("name");

String[] includeFields = new String[] {"name"};
String[] excludeFields = new String[] {"user"};
sourceBuilder.fetchSource(includeFields, excludeFields);

PLAIN:

  {
    "_source":
      ["name"],
    "query": {
        "query_string" : {
            "query" : "name:elon"

        }
    }

}

And you can do source controlling in code and Hope you are interested in query_string way only. If you are open to other ways, you can use this

Then to convert to List<String>, you need to iterate the response and add it to a List

SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
List<String> names = new ArrayList<String>();
for (SearchHit hit : searchHits) {
   Map<String, Object> sourceAsMap = hit.getSourceAsMap();
   names.add(sourceAsMap.get("name"));
}
Gibbs
  • 21,904
  • 13
  • 74
  • 138
0

It works with

        String[] includeFields = new String[]{"students.name"};
        sourceBuilder.fetchSource(includeFields, null);

Lily
  • 605
  • 3
  • 15
  • 31