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"
}]
8
  • Provide us your queyString and connection establishment. Commented Jun 21, 2020 at 18:09
  • @Gibbs check updated Qs. Commented Jun 21, 2020 at 18:13
  • Check my answer and let me know. Hope it ll help you Commented Jun 21, 2020 at 18:22
  • Have you tried that? Any luck? Commented Jun 22, 2020 at 6:26
  • @Gibbs the links that you have provided are 5 years old. Now a lot of methods deprecated. Commented Jun 22, 2020 at 7:15

2 Answers 2

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"));
}
Sign up to request clarification or add additional context in comments.

10 Comments

sir this is returning me nothing. 0 hits.
Can you add a sample document and mapping for name field?
If you have mapping like keyword or not analyzed then you may not expect the output as you desired.
I have not set the maping keyword etc . I just simply load data.
Can you run the below request and add it to the question - http://host:9200/indexName/_mapping GET
|
0

It works with

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.