# Search Filter for nested json array

**URL:** <https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110>\
**Category:** Elasticsearch\
**Created:** [March 12, 2013, 12:21pm UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110 "2013-03-12T12:21:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamar\_Fraenkel](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tamar\_Fraenkel](https://discuss.elastic.co/u/Tamar_Fraenkel)\
**Post date:** [March 12, 2013, 12:21pm UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110/1 "2013-03-12T12:21:27Z")

</div>

Hi!  
I am a newbie to elasticsearch.  
I have a json document with the following structure  
{

- id: "45f9f13c-5234-4a72-8a08-3863deed3755",
- title: "My Title",
- text: "My Text",
- article:  
{
  - id: "http://.[http://www.thehindu.com/arts/theatre/article3810675.ece](http://www.thehindu.com/arts/theatre/article3810675.ece)  
..",
  - url: null,
  - title: "Article Title",
  - excerpt: "...",
  - imageUrl: "http:[http://www.thehindu.com/multimedia/dynamic/01186/A\_sequence\_from\_Ma\_1186698g.jpg](http://www.thehindu.com/multimedia/dynamic/01186/A_sequence_from_Ma_1186698g.jpg)  
",
  - sourceId: 110,
  - sourceName: "Source",
  - sourceIconUrl: "http://[http://www.thehindubusinessline.com/template/1-0-1/gfx/favicon.ico](http://www.thehindubusinessline.com/template/1-0-1/gfx/favicon.ico)  
",
  - time: "2012-08-23 17:49:00 +0300",
  - topics: null,  
},

- 
## poll: [

## { - text: "Option 1", - count: 0, - index: 0 },

## { - text: "Option 2", - count: 0, - index: 1 },

## { - text: "Option 3", - count: 0, - index: 2 },
{  
- text: "Option 14",  
- count: 0,  
- index: 3  
}

I am using the Java API  
I am indexing this object like this

IndexResponse response = client.prepareIndex(  
ES\_INDEX, "discussions",  
id).setSource(json\_string).execute().actionGet();

When I query it like this it works fine, but it will search only in  
title and text fields. It will also find only cases where searchText is  
the prefix of text or title. Why?

client.prepareSearch(ES\_INDEX).setTypes(type.name())  
.setQuery(filteredQuery(matchAllQuery(),  
andFilter(boolFilter().must(termFilter("test",  
"false")),  
orFilter(inFilter("title", searchText),  
inFilter("text", searchText)))))  
.setSearchType(  
SearchType.DFS\_QUERY\_THEN\_FETCH).setFrom(0).setSize(  
limit).execute().actionGet();

I can't search in the poll text. Tried nested filter but I am getting  
exception below

Exception

nested: QueryParsingException[[tokrest] [nested] nested object under  
path [poll] is not of nested type]; }  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:260)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:213)  
at  
org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteDfs(SearchServiceTransportAction.java:110)  
at  
org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchDfsQueryThenFetchAction.java:86)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
at  
org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
at  
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
at  
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
at java.lang.Thread.run(Thread.java:722)

Any help and code examples will be appreciated!  
Tamar Fraenkel

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 12, 2013, 1:59pm UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110/2 "2013-03-12T13:59:06Z")

</div>

You can't use nested filters if you did not define nested objects using a mapping first.  
See: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)

That said, what are you trying to achieve here?  
What are you looking for?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 12 mars 2013 à 13:21, Tamar Fraenkel [tamar.fraenkel@gmail.com](mailto:tamar.fraenkel@gmail.com) a écrit :

> Hi!  
> I am a newbie to elasticsearch.  
> I have a json document with the following structure  
> {  
> id: "45f9f13c-5234-4a72-8a08-3863deed3755",  
> title: "My Title",  
> text: "My Text",  
> article: {  
> id: "http://...",  
> url: null,  
> title: "Article Title",  
> excerpt: "...",  
> imageUrl: "http:",  
> sourceId: 110,  
> sourceName: "Source",  
> sourceIconUrl: "http://",  
> time: "2012-08-23 17:49:00 +0300",  
> topics: null,  
> },  
> poll: [  
> {  
> text: "Option 1",  
> count: 0,  
> index: 0  
> },  
> {  
> text: "Option 2",  
> count: 0,  
> index: 1  
> },  
> {  
> text: "Option 3",  
> count: 0,  
> index: 2  
> },  
> {  
> text: "Option 14",  
> count: 0,  
> index: 3  
> }
> 
> I am using the Java API  
> I am indexing this object like this
> 
> IndexResponse response = client.prepareIndex(  
> ES\_INDEX, "discussions", id).setSource(json\_string).execute().actionGet();
> 
> When I query it like this it works fine, but it will search only in title and text fields. It will also find only cases where searchText is the prefix of text or title. Why?
> 
> client.prepareSearch(ES\_INDEX).setTypes(type.name())  
> .setQuery(filteredQuery(matchAllQuery(),  
> andFilter(boolFilter().must(termFilter("test", "false")),  
> orFilter(inFilter("title", searchText),  
> inFilter("text", searchText)))))  
> .setSearchType(  
> SearchType.DFS\_QUERY\_THEN\_FETCH).setFrom(0).setSize(  
> limit).execute().actionGet();
> 
> I can't search in the poll text. Tried nested filter but I am getting exception below
> 
> Exception
> 
> nested: QueryParsingException[[tokrest] [nested] nested object under path [poll] is not of nested type]; }  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:260)  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:213)  
> at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteDfs(SearchServiceTransportAction.java:110)  
> at org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchDfsQueryThenFetchAction.java:86)  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
> at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
> at java.lang.Thread.run(Thread.java:722)
> 
> Any help and code examples will be appreciated!  
> Tamar Fraenkel
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tamar\_Fraenkel](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tamar\_Fraenkel](https://discuss.elastic.co/u/Tamar_Fraenkel)\
**Post date:** [March 12, 2013, 5:59pm UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110/3 "2013-03-12T17:59:08Z")

</div>

Hi  
Thanks for the reply.  
I actually saw this but have no clue on how to define such mapping using  
the java API.  
What I am trying to achieve is search for discussion by text match to  
either the title, text, or one of the votes texts.  
I can probably flatten the Json and solve the issue, but I was hoping I  
wouldn't have to.

Also, I am not sure why search works for some words but not others

Tamar  
On Mar 12, 2013 3:59 PM, "David Pilato" [david@pilato.fr](mailto:david@pilato.fr) wrote:

> You can't use nested filters if you did not define nested objects using a  
> mapping first.  
> See: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)
> 
> That said, what are you trying to achieve here?  
> What are you looking for?
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 12 mars 2013 à 13:21, Tamar Fraenkel [tamar.fraenkel@gmail.com](mailto:tamar.fraenkel@gmail.com) a  
> écrit :
> 
> Hi!  
> I am a newbie to elasticsearch.  
> I have a json document with the following structure  
> {
> 
> - id: "45f9f13c-5234-4a72-8a08-3863deed3755",
> - title: "My Title",
> - text: "My Text",
> - article:  
> {
> - id: "http://.[http://www.thehindu.com/arts/theatre/article3810675.ece](http://www.thehindu.com/arts/theatre/article3810675.ece)  
> ..",
> - url: null,
> - title: "Article Title",
> - excerpt: "...",
> - imageUrl: "http:[http://www.thehindu.com/multimedia/dynamic/01186/A\_sequence\_from\_Ma\_1186698g.jpg](http://www.thehindu.com/multimedia/dynamic/01186/A_sequence_from_Ma_1186698g.jpg)  
> ",
> - sourceId: 110,
> - sourceName: "Source",
> - sourceIconUrl: "http://[http://www.thehindubusinessline.com/template/1-0-1/gfx/favicon.ico](http://www.thehindubusinessline.com/template/1-0-1/gfx/favicon.ico)  
> ",
> - time: "2012-08-23 17:49:00 +0300",
> - topics: null,  
> },
> 
> - 
> ## poll: [
> 
> ## { - text: "Option 1", - count: 0, - index: 0 },
> 
> ## { - text: "Option 2", - count: 0, - index: 1 },
> 
> ## { - text: "Option 3", - count: 0, - index: 2 },
> {  
> - text: "Option 14",  
> - count: 0,  
> - index: 3  
> }
> 
> I am using the Java API  
> I am indexing this object like this
> 
> IndexResponse response = client.prepareIndex(  
> ES\_INDEX, "discussions",  
> id).setSource(json\_string).execute().actionGet();
> 
> When I query it like this it works fine, but it will search only in  
> title and text fields. It will also find only cases where searchText  
> is the prefix of text or title. Why?
> 
> client.prepareSearch(ES\_INDEX).setTypes(type.name())  
> .setQuery(filteredQuery(matchAllQuery(),  
> andFilter(boolFilter().must(termFilter("test",  
> "false")),  
> orFilter(inFilter("title", searchText),  
> inFilter("text", searchText)))))  
> .setSearchType(
> 
> SearchType.DFS\_QUERY\_THEN\_FETCH).setFrom(0).setSize(  
> limit).execute().actionGet();
> 
> I can't search in the poll text. Tried nested filter but I am getting  
> exception below
> 
> Exception
> 
> nested: QueryParsingException[[tokrest] [nested] nested object under  
> path [poll] is not of nested type]; }  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:260)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:213)  
> at  
> org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteDfs(SearchServiceTransportAction.java:110)  
> at  
> org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchDfsQueryThenFetchAction.java:86)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
> at  
> org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
> at  
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
> at  
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
> at java.lang.Thread.run(Thread.java:722)
> 
> Any help and code examples will be appreciated!  
> Tamar Fraenkel
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/S\_SQpepmw2M/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/S_SQpepmw2M/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 13, 2013, 8:41am UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110/4 "2013-03-13T08:41:01Z")

</div>

Just search in `poll.text`, `title`, `text` fields.  
It should work and you probably don't need nested docs here.

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 12 mars 2013 à 18:59, Tamar Fraenkel [tamar.fraenkel@gmail.com](mailto:tamar.fraenkel@gmail.com) a écrit :

> Hi  
> Thanks for the reply.  
> I actually saw this but have no clue on how to define such mapping using the java API.  
> What I am trying to achieve is search for discussion by text match to either the title, text, or one of the votes texts.  
> I can probably flatten the Json and solve the issue, but I was hoping I wouldn't have to.
> 
> Also, I am not sure why search works for some words but not others
> 
> Tamar
> 
> On Mar 12, 2013 3:59 PM, "David Pilato" [david@pilato.fr](mailto:david@pilato.fr) wrote:  
> You can't use nested filters if you did not define nested objects using a mapping first.  
> See: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)
> 
> That said, what are you trying to achieve here?  
> What are you looking for?
> 
> --  
> David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
> @dadoonet | @elasticsearchfr | @scrutmydocs
> 
> Le 12 mars 2013 à 13:21, Tamar Fraenkel [tamar.fraenkel@gmail.com](mailto:tamar.fraenkel@gmail.com) a écrit :
> 
> > Hi!  
> > I am a newbie to elasticsearch.  
> > I have a json document with the following structure  
> > {  
> > id: "45f9f13c-5234-4a72-8a08-3863deed3755",  
> > title: "My Title",  
> > text: "My Text",  
> > article:  
> > {  
> > id: "http://...",  
> > url: null,  
> > title: "Article Title",  
> > excerpt: "...",  
> > imageUrl: "http:",  
> > sourceId: 110,  
> > sourceName: "Source",  
> > sourceIconUrl: "http://",  
> > time: "2012-08-23 17:49:00 +0300",  
> > topics: null,  
> > },  
> > poll:  
> > [  
> > {  
> > text: "Option 1",  
> > count: 0,  
> > index: 0  
> > },  
> > {  
> > text: "Option 2",  
> > count: 0,  
> > index: 1  
> > },  
> > {  
> > text: "Option 3",  
> > count: 0,  
> > index: 2  
> > },  
> > {  
> > text: "Option 14",  
> > count: 0,  
> > index: 3  
> > }
> > 
> > I am using the Java API  
> > I am indexing this object like this
> > 
> > IndexResponse response = client.prepareIndex(  
> > ES\_INDEX, "discussions", id).setSource(json\_string).execute().actionGet();
> > 
> > When I query it like this it works fine, but it will search only in title and text fields. It will also find only cases where searchText is the prefix of text or title. Why?
> > 
> > client.prepareSearch(ES\_INDEX).setTypes(type.name())  
> > .setQuery(filteredQuery(matchAllQuery(),  
> > andFilter(boolFilter().must(termFilter("test", "false")),  
> > orFilter(inFilter("title", searchText),  
> > inFilter("text", searchText)))))  
> > .setSearchType(  
> > SearchType.DFS\_QUERY\_THEN\_FETCH).setFrom(0).setSize(  
> > limit).execute().actionGet();
> > 
> > I can't search in the poll text. Tried nested filter but I am getting exception below
> > 
> > Exception
> > 
> > nested: QueryParsingException[[tokrest] [nested] nested object under path [poll] is not of nested type]; }  
> > at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:260)  
> > at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:213)  
> > at org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteDfs(SearchServiceTransportAction.java:110)  
> > at org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchDfsQueryThenFetchAction.java:86)  
> > at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
> > at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
> > at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
> > at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
> > at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
> > at java.lang.Thread.run(Thread.java:722)
> > 
> > Any help and code examples will be appreciated!  
> > Tamar Fraenkel
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/S\_SQpepmw2M/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/S_SQpepmw2M/unsubscribe?hl=en-US).  
> To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tamar\_Fraenkel](https://avatars.discourse-cdn.com/v4/letter/t/9e8a1a/32.png) [@Tamar\_Fraenkel](https://discuss.elastic.co/u/Tamar_Fraenkel)\
**Post date:** [March 13, 2013, 1:12pm UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110/5 "2013-03-13T13:12:32Z")

</div>

Hi!  
Managed to make it work:

_Before indexing I defined a mapping:_  
XContentBuilder mappingBuilder = jsonBuilder().  
startObject() //all  
.startObject(IndexType.discussions.name()) //type  
.startObject("properties") //properties  
.startObject("title").field("type", "string").endObject()  
.startObject("text").field("type", "string").endObject()  
.startObject("poll").field("type", "nested").endObject()  
.endObject() //properties  
.endObject() //type  
.endObject(); //all

CreateIndexResponse createIndexResponse =

client.admin().indices().prepareCreate(index\_name).addMapping(  
index\_type, mappingBuilder).execute()  
.actionGet();

_When indexing:_  
XContentBuilder builder = jsonBuilder().startObject()  
.field("title", getTitle())  
.field("text", getText())  
.startArray("poll");  
for (PollOption poll : getPollList()) {  
builder = builder.startObject().field("text", poll.getText()).endObject();  
}  
builder = builder.endArray().endObject();  
IndexResponse response = client.prepareIndex(  
index\_name, index\_type, getId()).setSource(  
builder).execute().actionGet();

_When Searching:_  
SearchRequestBuilder srqb =  
client.prepareSearch(index\_name).setTypes(index\_type).  
setQuery(filteredQuery(matchAllQuery(),  
orFilter(queryFilter(fieldQuery("title", searchText)),  
queryFilter(fieldQuery("text", searchText)),  
nestedFilter("poll",  
fieldQuery("poll.text", searchText)))));

Hope this will help someone.

Tamar

On Wed, Mar 13, 2013 at 10:41 AM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> Just search in `poll.text`, `title`, `text` fields.  
> It should work and you probably don't need nested docs here.
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 12 mars 2013 à 18:59, Tamar Fraenkel [tamar.fraenkel@gmail.com](mailto:tamar.fraenkel@gmail.com) a  
> écrit :
> 
> Hi  
> Thanks for the reply.  
> I actually saw this but have no clue on how to define such mapping using  
> the java API.  
> What I am trying to achieve is search for discussion by text match to  
> either the title, text, or one of the votes texts.  
> I can probably flatten the Json and solve the issue, but I was hoping I  
> wouldn't have to.
> 
> Also, I am not sure why search works for some words but not others
> 
> Tamar  
> On Mar 12, 2013 3:59 PM, "David Pilato" [david@pilato.fr](mailto:david@pilato.fr) wrote:
> 
> > You can't use nested filters if you did not define nested objects using a  
> > mapping first.  
> > See:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/nested-type.html)
> > 
> > That said, what are you trying to achieve here?  
> > What are you looking for?
> > 
> > --  
> > _David Pilato_ | _Technical Advocate_ | \*[Elasticsearch.com](http://Elasticsearch.com)[http://elasticsearch.com/](http://elasticsearch.com/)  
> > \*  
> > @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> > | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> > 
> > Le 12 mars 2013 à 13:21, Tamar Fraenkel [tamar.fraenkel@gmail.com](mailto:tamar.fraenkel@gmail.com) a  
> > écrit :
> > 
> > Hi!  
> > I am a newbie to elasticsearch.  
> > I have a json document with the following structure  
> > {
> > 
> > - id: "45f9f13c-5234-4a72-8a08-3863deed3755",
> > - title: "My Title",
> > - text: "My Text",
> > - article:  
> > {
> > - id: "http://.[http://www.thehindu.com/arts/theatre/article3810675.ece](http://www.thehindu.com/arts/theatre/article3810675.ece)  
> > ..",
> > - url: null,
> > - title: "Article Title",
> > - excerpt: "...",
> > - imageUrl: "http:[http://www.thehindu.com/multimedia/dynamic/01186/A\_sequence\_from\_Ma\_1186698g.jpg](http://www.thehindu.com/multimedia/dynamic/01186/A_sequence_from_Ma_1186698g.jpg)  
> > ",
> > - sourceId: 110,
> > - sourceName: "Source",
> > - sourceIconUrl: "http://[http://www.thehindubusinessline.com/template/1-0-1/gfx/favicon.ico](http://www.thehindubusinessline.com/template/1-0-1/gfx/favicon.ico)  
> > ",
> > - time: "2012-08-23 17:49:00 +0300",
> > - topics: null,  
> > },
> > 
> > - 
> > ## poll: [
> > 
> > ## { - text: "Option 1", - count: 0, - index: 0 },
> > 
> > ## { - text: "Option 2", - count: 0, - index: 1 },
> > 
> > ## { - text: "Option 3", - count: 0, - index: 2 },
> > {  
> > - text: "Option 14",  
> > - count: 0,  
> > - index: 3  
> > }
> > 
> > I am using the Java API  
> > I am indexing this object like this
> > 
> > IndexResponse response = client.prepareIndex(  
> > ES\_INDEX, "discussions",  
> > id).setSource(json\_string).execute().actionGet();
> > 
> > When I query it like this it works fine, but it will search only in  
> > title and text fields. It will also find only cases where searchText  
> > is the prefix of text or title. Why?
> > 
> > client.prepareSearch(ES\_INDEX).setTypes(type.name())  
> > .setQuery(filteredQuery(matchAllQuery(),  
> > andFilter(boolFilter().must(termFilter("test",  
> > "false")),  
> > orFilter(inFilter("title", searchText),  
> > inFilter("text", searchText)))))  
> > .setSearchType(
> > 
> > SearchType.DFS\_QUERY\_THEN\_FETCH).setFrom(0).setSize(  
> > limit).execute().actionGet();
> > 
> > I can't search in the poll text. Tried nested filter but I am getting  
> > exception below
> > 
> > Exception
> > 
> > nested: QueryParsingException[[tokrest] [nested] nested object under  
> > path [poll] is not of nested type]; }  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:260)  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$3.onFailure(TransportSearchTypeAction.java:213)  
> > at  
> > org.elasticsearch.search.action.SearchServiceTransportAction.sendExecuteDfs(SearchServiceTransportAction.java:110)  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchDfsQueryThenFetchAction$AsyncAction.sendExecuteFirstPhase(TransportSearchDfsQueryThenFetchAction.java:86)  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:205)  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.performFirstPhase(TransportSearchTypeAction.java:192)  
> > at  
> > org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction$2.run(TransportSearchTypeAction.java:178)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)  
> > at  
> > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)  
> > at java.lang.Thread.run(Thread.java:722)
> > 
> > Any help and code examples will be appreciated!  
> > Tamar Fraenkel
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/S\_SQpepmw2M/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/S_SQpepmw2M/unsubscribe?hl=en-US)  
> > .  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/S\_SQpepmw2M/unsubscribe?hl=en-US](https://groups.google.com/d/topic/elasticsearch/S_SQpepmw2M/unsubscribe?hl=en-US)  
> .  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Tamar Fraenkel  
8 Yitzhak Shalev St., Apt. 2  
Jerusalem, Israel  
Home: +972-2-6500435  
Cell: +972-54-8356490

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:46am UTC](https://discuss.elastic.co/t/search-filter-for-nested-json-array/11110/6 "2017-07-06T02:46:42Z")

</div>


