# Query fails via Java client, successful with elasticsearch-head

**URL:** <https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890>\
**Category:** Elasticsearch\
**Created:** [November 16, 2011, 8:01pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890 "2011-11-16T20:01:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [November 16, 2011, 8:01pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/1 "2011-11-16T20:01:56Z")

</div>

Using ES 0.17.9, the following query is successful if I execute using the  
elasticsearch-head web ui, but fails when using the Java client:

> <https://gist.github.com/digitalsanctum/1371153>

I get the following ShardSearchFailure:

sort[\<custom:"owner.username":  
org.elasticsearch.index.field.data.strings.StringFieldDataType$1@173aa68\>!]:  
Query Failed [Failed to execute main query]]; nested: IOException[Can't  
sort on string types with more than one value per doc, or more than one  
token per field];

The field in question (owner.username) should only have a single value.

Any suggestions on why I'm getting inconsistent results and what I can do  
to fix?

---

<div class="post-metadata">

**Author:** ![avasilenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/avasilenko/32/2901_2.png) [@avasilenko](https://discuss.elastic.co/u/avasilenko)\
**Post date:** [November 16, 2011, 9:02pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/2 "2011-11-16T21:02:17Z")

</div>

Hello Shane,

Please show your mapping for this index type. Are you sure "owner.username"  
is not tokenized?

Regards,  
Alexandr Vasilenko

2011/11/16 Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)

> Using ES 0.17.9, the following query is successful if I execute using the  
> elasticsearch-head web ui, but fails when using the Java client:
> 
> [ES query failure · GitHub](https://gist.github.com/1371153)
> 
> I get the following ShardSearchFailure:
> 
> sort[\<custom:"owner.username":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@173aa68\>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort on string types with more than one value per doc, or more than one  
> token per field];
> 
> The field in question (owner.username) should only have a single value.
> 
> Any suggestions on why I'm getting inconsistent results and what I can do  
> to fix?

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [November 16, 2011, 9:30pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/3 "2011-11-16T21:30:44Z")

</div>

I don't have a mapping per se, since I'm letting ES do it at the time of  
index. Here's the gist of the content builder:

> <https://gist.github.com/anonymous/1371479>

Thanks

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 17, 2011, 1:10pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/4 "2011-11-17T13:10:06Z")

</div>

You probably need to set the username to be not\_analyzed in the mapping for  
it. There isn't a different between using the Java API and the REST API.

On Wed, Nov 16, 2011 at 11:30 PM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:

> I don't have a mapping per se, since I'm letting ES do it at the time of  
> index. Here's the gist of the content builder:
> 
> [content builder for thread · GitHub](https://gist.github.com/1371479)
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [November 17, 2011, 1:57pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/5 "2011-11-17T13:57:33Z")

</div>

Thanks. How would I set username to be not\_analyzed using the Java API? I  
assume this needs to be done at the time of indexing.

---

<div class="post-metadata">

**Author:** ![mkleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkleen/32/1694_2.png) [@mkleen](https://discuss.elastic.co/u/mkleen)\
**Post date:** [November 17, 2011, 6:10pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/6 "2011-11-17T18:10:30Z")

</div>

Hello,

you need to put a mapping before the indexing and this would look in  
java something like:

```
JsonXContent
    .contentBuilder
    .startObject
      .startObject("Foo")
        .startObject("properties")
          .startObject("username")
             .field("type", "string")
             .field("index", "not_analyzed")
           .endObject
         .endObject
       .endObject
     .endObject

```

and then you can use  
org.elasticsearch.client.Requests.putMappingRequest to send the  
xcontent to the server.

Michael

On Nov 17, 2:57 pm, Shane Witbeck [sh...@digitalsanctum.com](mailto:sh...@digitalsanctum.com) wrote:

> Thanks. How would I set username to be not\_analyzed using the Java API? I  
> assume this needs to be done at the time of indexing.

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [November 22, 2011, 3:30pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/7 "2011-11-22T15:30:32Z")

</div>

Thanks for the replies. Unfortunately I'm still not able to resolve this.  
Shay's suggestion of adding "not\_analyzed" to the mapping for username  
works for the sort but doesn't allow the field to be searchable. Since my  
last post I'm now creating an explicit mapping shown here:

> <https://gist.github.com/digitalsanctum/1385933>

As before, if I try to sort on the owner.ownerUsername field, I get the  
following error:

SEARCH FAILURE: status=INTERNAL\_SERVER\_ERROR,  
reason=QueryPhaseExecutionException[[threads][1]:  
query[ConstantScore(org.elasticsearch.common.lucene.search.OrFilter@3d0d96f)],from[0],size[25],sort[\<custom:"owner.ownerUsername":  
org.elasticsearch.index.field.data.strings.StringFieldDataType$1@709d70b4\>!]:  
Query Failed [Failed to execute main query]]; nested: IOException[Can't  
sort on string types with more than one value per doc, or more than one  
token per field];

What do I need to do in order to have this field searchable AND sortable?

Thanks again.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 22, 2011, 3:33pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/8 "2011-11-22T15:33:05Z")

</div>

Use mutli field mapping:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html).

On Tue, Nov 22, 2011 at 5:30 PM, Shane Witbeck [shane@digitalsanctum.com](mailto:shane@digitalsanctum.com)wrote:

> Thanks for the replies. Unfortunately I'm still not able to resolve this.  
> Shay's suggestion of adding "not\_analyzed" to the mapping for username  
> works for the sort but doesn't allow the field to be searchable. Since my  
> last post I'm now creating an explicit mapping shown here:
> 
> [ES thread mapping · GitHub](https://gist.github.com/1385933)
> 
> As before, if I try to sort on the owner.ownerUsername field, I get the  
> following error:
> 
> SEARCH FAILURE: status=INTERNAL\_SERVER\_ERROR,  
> reason=QueryPhaseExecutionException[[threads][1]:  
> query[ConstantScore(org.elasticsearch.common.lucene.search.OrFilter@3d0d96f)],from[0],size[25],sort[\<custom:"owner.ownerUsername":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@709d70b4\>!]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort on string types with more than one value per doc, or more than one  
> token per field];
> 
> What do I need to do in order to have this field searchable AND sortable?
> 
> Thanks again.

---

<div class="post-metadata">

**Author:** ![Shane\_Witbeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shane_witbeck/32/2803_2.png) [@Shane\_Witbeck](https://discuss.elastic.co/u/Shane_Witbeck)\
**Post date:** [November 22, 2011, 4:34pm UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/9 "2011-11-22T16:34:56Z")

</div>

Shay, that worked! Thanks for the quick response.

---

<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, 3:47am UTC](https://discuss.elastic.co/t/query-fails-via-java-client-successful-with-elasticsearch-head/5890/10 "2017-07-06T03:47:49Z")

</div>


