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.