# Query on multi value field and only return hits Elasticsearch in Java

**URL:** <https://discuss.elastic.co/t/query-on-multi-value-field-and-only-return-hits-elasticsearch-in-java/22693>\
**Category:** Elasticsearch\
**Created:** [March 16, 2015, 4:56pm UTC](https://discuss.elastic.co/t/query-on-multi-value-field-and-only-return-hits-elasticsearch-in-java/22693 "2015-03-16T16:56:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![djbuckle91](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/djbuckle91/32/795_2.png) [@djbuckle91](https://discuss.elastic.co/u/djbuckle91)\
**Post date:** [March 16, 2015, 4:56pm UTC](https://discuss.elastic.co/t/query-on-multi-value-field-and-only-return-hits-elasticsearch-in-java/22693/1 "2015-03-16T16:56:53Z")

</div>

I have a document set out like this:

{"thread\_id":"1",  
"thread\_name":"Apple",  
"created":"Wed Mar 11 2015",  
"messages": [  
{ "message\_id": "1", "message": "Banana", "message\_nick": "AdminTech"},  
{ "message\_id": "2", "message": "iPhone", "message\_nick": "AdminTech"},  
{ "message\_id": "3", "message": "Mac", "message\_nick": "AdminTech"},  
{ "message\_id": "4", "message": "Laptop", "message\_nick": "AdminTech"}],  
"thread\_url":"1-Apple" }  
I want to query on the thread\_name field, which works, but I also want to see if the keyword I am querying appears within the messages multi value fields within message.

So for example, I want to query the word "Laptop". This document should appear, but I only want the one field where the keyword matched, e.g. message\_id 4.

Here is the code I am using the query in Java:

QueryBuilder query = QueryBuilders.matchQuery(keyword, "thread\_name");  
QueryBuilder nestedQuery = QueryBuilders.matchQuery("messages.message", keyword);

```
SearchRequestBuilder srb = client.prepareSearch("thread_and_messages")
        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
srb.setQuery(QueryBuilders.boolQuery()
        .should(query)
        .should(nestedQuery)
        .minimumShouldMatch("1"));

SearchResponse threadResponse = srb.execute().actionGet();

SearchHit[] threadHits = threadResponse.getHits().getHits();

```

The code to retrieve the hits and place them within an object is here:

ArrayList messageIdAndMessage = (ArrayList) hit.get("messages");

```
ArrayList<TalkMessage> messages = new ArrayList<TalkMessage>();
FoundThreadAndMessage ftm = new FoundThreadAndMessage();
ftm.setId(Integer.parseInt(String.valueOf(hit.get("thread_id"))));
ftm.setThreadName(String.valueOf(hit.get("thread_name")));
ftm.setCreated(String.valueOf(hit.get("created")));

```

Does anyone know if this query is correct, because at the moment I am getting every single field back from the query.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:26am UTC](https://discuss.elastic.co/t/query-on-multi-value-field-and-only-return-hits-elasticsearch-in-java/22693/2 "2017-07-06T00:26:27Z")

</div>


