# Error when searching multiple fields with different types

**URL:** <https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275>\
**Category:** Elasticsearch\
**Created:** [April 9, 2012, 5:25pm UTC](https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275 "2012-04-09T17:25:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chimingc](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@chimingc](https://discuss.elastic.co/u/chimingc)\
**Post date:** [April 9, 2012, 5:25pm UTC](https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275/1 "2012-04-09T17:25:04Z")

</div>

I have been seeing strange error in my application and I was able to narrow it down and reproduce it in my simple test.

1. A new index with 2 fields, one type long, another type string.  
curl -XPOST localhost:9200/test -d '{  
"mappings" : {  
"Cases" : {  
"properties" : {  
"case\_number" : { "type" : "long"},  
"desc" : { "type" : "string"}  
}  
}  
}  
}'

2. Index a record  
curl -XPUT '[http://localhost:9200/test/Cases/1](http://localhost:9200/test/Cases/1)' -d '{  
"case\_number" : "123",  
"desc" : "abc"  
}'

3. Search  
curl -XPOST "localhost:9200/test/Cases/\_search?pretty=true" -d '{  
"query": {  
"query\_string": {  
"fields" : ["case\_number", "desc"],  
"query":"abc"  
}  
}  
}'

The search returns status 500 and produces this error:  
"error" : "SearchPhaseExecutionException[Failed to execute phase [query\_fetch], total failure; shardFailures {[tFucKjGMTjeW2EoWEcXDww][test][0]: SearchParseException[[test][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query": {\n "query\_string": {\n "fields" : ["case\_number", "desc"],\n "query":"abc"\n }\n }\n}]]]; nested: NumberFormatException[For input string: "abc"]; }]",

But if I replace my query "abc" with "123", the error goes away. But as you can see I have both string and long fields in my index so I would expect both from users.

Is this a bug or something I have to do to make ES handle both long and string types?

I am using the latest ES (0.19.2).

Thanks.

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 11, 2012, 2:54am UTC](https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275/2 "2012-04-11T02:54:17Z")

</div>

I don't think it's elasticsearch bug. Elasticsearch is just trying to  
convert your query into a DisMax query (case\_number:abc | desc:abc) and  
case\_number:abc just doesn't make sense here because case\_number has type  
long.

For your scenario to work, you need to index case\_number as a string, or if  
you need case\_number as a long somewhere else (for sorting, for example),  
index it as a multifield and include a string version into the user's query.

On Monday, April 9, 2012 1:25:04 PM UTC-4, chimingc wrote:

> I have been seeing strange error in my application and I was able to narrow  
> it down and reproduce it in my simple test.
> 
> 1. A new index with 2 fields, one type long, another type string.  
> curl -XPOST localhost:9200/test -d '{  
> "mappings" : {  
> "Cases" : {  
> "properties" : {  
> "case\_number" : { "type" : "long"},  
> "desc" : { "type" : "string"}  
> }  
> }  
> }  
> }'
> 
> 2. Index a record  
> curl -XPUT '[http://localhost:9200/test/Cases/1](http://localhost:9200/test/Cases/1)' -d '{  
> "case\_number" : "123",  
> "desc" : "abc"  
> }'
> 
> 3. Search  
> curl -XPOST "localhost:9200/test/Cases/\_search?pretty=true" -d '{  
> "query": {  
> "query\_string": {  
> "fields" : ["case\_number", "desc"],  
> "query":"abc"  
> }  
> }  
> }'
> 
> The search returns status 500 and produces this error:  
> "error" : "SearchPhaseExecutionException[Failed to execute phase  
> [query\_fetch], total failure; shardFailures  
> {[tFucKjGMTjeW2EoWEcXDww][test][0]: SearchParseException[[test][0]:  
> from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query":  
> {\n "query\_string": {\n "fields" : ["case\_number",  
> "desc"],\n "query":"abc"\n }\n }\n}]]]; nested:  
> NumberFormatException[For input string: "abc"]; }]",
> 
> But if I replace my query "abc" with "123", the error goes away. But as you  
> can see I have both string and long fields in my index so I would expect  
> both from users.
> 
> Is this a bug or something I have to do to make ES handle both long and  
> string types?
> 
> I am using the latest ES (0.19.2).
> 
> Thanks.
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3897459.html](http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3897459.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![chimingc](https://avatars.discourse-cdn.com/v4/letter/c/ba9def/32.png) [@chimingc](https://discuss.elastic.co/u/chimingc)\
**Post date:** [April 12, 2012, 5:14pm UTC](https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275/3 "2012-04-12T17:14:09Z")

</div>

Thanks Igor.

Yes I agree case\_number:abc does not make sense. But desc:abc does and has a hit so I would expect it to ignore case\_number and return a hit instead of throwing an error.

I'd imagine that it's quite common to have multiple field types in one index. If so, how do people usually do? Or do people simply use strings for everything?

From: "Igor Motov-3 [via ElasticSearch Users]" \<[ml-node+s115913n3901461h95@n3.nabble.com](mailto:ml-node+s115913n3901461h95@n3.nabble.com)[mailto:ml-node+s115913n3901461h95@n3.nabble.com](mailto:ml-node+s115913n3901461h95@n3.nabble.com)\>  
Date: Tue, 10 Apr 2012 21:54:41 -0500  
To: Jimmy Chen \<[jchen@sugarcrm.com](mailto:jchen@sugarcrm.com)[mailto:jchen@sugarcrm.com](mailto:jchen@sugarcrm.com)\>  
Subject: Re: Error when searching multiple fields with different types

I don't think it's elasticsearch bug. Elasticsearch is just trying to convert your query into a DisMax query (case\_number:abc | desc:abc) and case\_number:abc just doesn't make sense here because case\_number has type long.

For your scenario to work, you need to index case\_number as a string, or if you need case\_number as a long somewhere else (for sorting, for example), index it as a multifield and include a string version into the user's query.

On Monday, April 9, 2012 1:25:04 PM UTC-4, chimingc wrote:  
I have been seeing strange error in my application and I was able to narrow  
it down and reproduce it in my simple test.

1. A new index with 2 fields, one type long, another type string.  
curl -XPOST localhost:9200/test -d '{  
"mappings" : {  
"Cases" : {  
"properties" : {  
"case\_number" : { "type" : "long"},  
"desc" : { "type" : "string"}  
}  
}  
}  
}'

2. Index a record  
curl -XPUT '[http://localhost:9200/test/Cases/1](http://localhost:9200/test/Cases/1)' -d '{  
"case\_number" : "123",  
"desc" : "abc"  
}'

3. Search  
curl -XPOST "localhost:9200/test/Cases/\_search?pretty=true" -d '{  
"query": {  
"query\_string": {  
"fields" : ["case\_number", "desc"],  
"query":"abc"  
}  
}  
}'

The search returns status 500 and produces this error:  
"error" : "SearchPhaseExecutionException[Failed to execute phase  
[query\_fetch], total failure; shardFailures  
{[tFucKjGMTjeW2EoWEcXDww][test][0]: SearchParseException[[test][0]:  
from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query":  
{\n "query\_string": {\n "fields" : ["case\_number",  
"desc"],\n "query":"abc"\n }\n }\n}]]]; nested:  
NumberFormatException[For input string: "abc"]; }]",

But if I replace my query "abc" with "123", the error goes away. But as you  
can see I have both string and long fields in my index so I would expect  
both from users.

Is this a bug or something I have to do to make ES handle both long and  
string types?

I am using the latest ES (0.19.2).

Thanks.

--  
View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3897459.html](http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3897459.html)  
Sent from the ElasticSearch Users mailing list archive at [Nabble.com](http://Nabble.com).

* * *

If you reply to this email, your message will be added to the discussion below:  
[http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3901461.html](http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3901461.html)  
To unsubscribe from Error when searching multiple fields with different types, click here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=3897459&code=amNoZW5Ac3VnYXJjcm0uY29tfDM4OTc0NTl8LTcxNDM5MzQ0Nw==](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3897459&code=amNoZW5Ac3VnYXJjcm0uY29tfDM4OTc0NTl8LTcxNDM5MzQ0Nw==).  
NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 12, 2012, 7:28pm UTC](https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275/4 "2012-04-12T19:28:52Z")

</div>

I think this is part of a larger discussion about what should search engine  
do - ignore invalid user input or return an error. I can argue it both  
ways. In most projects I worked on, we separated user query language from  
search engine query language. It provided us a lot of flexibility with some  
limited overhead. And when I translate user queries into search engine  
queries, I prefer search engine returning error on invalid queries instead  
of ignoring them, since it makes debugging process much easier.

In my opinion, when you use the same term to search multiple fields, it's  
better to search fields with the same type. Otherwise, you might run into  
issues like "why 12\* finds 123 in description but doesn't find 123 in the  
case number?" If you have a defined set of fields that you always want to  
search by default, you can use \_all field[http://www.elasticsearch.org/guide/reference/mapping/all-field.html](http://www.elasticsearch.org/guide/reference/mapping/all-field.html).  
Elasticsearch provides a mechanism that allows you to control which fields  
should be included there. If you have multiple sets of default fields that  
you want to search in different situations, you can use multi field type[http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html)to index case number as a number (for sorting) and as a string (for  
searching as part of a default field set).

On Thursday, April 12, 2012 1:14:18 PM UTC-4, chimingc wrote:

> Thanks Igor.
> 
> Yes I agree case\_number:abc does not make sense. But desc:abc does and has  
> a hit so I would expect it to ignore case\_number and return a hit instead  
> of throwing an error.
> 
> I'd imagine that it's quite common to have multiple field types in one  
> index. If so, how do people usually do? Or do people simply use strings for  
> everything?
> 
> From: "Igor Motov-3 [via Elasticsearch Users]" \<[hidden email][http://user/SendEmail.jtp?type=node&node=3905931&i=0](http://user/SendEmail.jtp?type=node&node=3905931&i=0)
> 
> > 
> 
> Date: Tue, 10 Apr 2012 21:54:41 -0500  
> To: Jimmy Chen \<[hidden email][http://user/SendEmail.jtp?type=node&node=3905931&i=1](http://user/SendEmail.jtp?type=node&node=3905931&i=1)
> 
> > 
> 
> Subject: Re: Error when searching multiple fields with different types
> 
> I don't think it's elasticsearch bug. Elasticsearch is just trying to  
> convert your query into a DisMax query (case\_number:abc | desc:abc) and  
> case\_number:abc just doesn't make sense here because case\_number has type  
> long.
> 
> For your scenario to work, you need to index case\_number as a string, or  
> if you need case\_number as a long somewhere else (for sorting, for  
> example), index it as a multifield and include a string version into the  
> user's query.
> 
> On Monday, April 9, 2012 1:25:04 PM UTC-4, chimingc wrote:
> 
> > I have been seeing strange error in my application and I was able to  
> > narrow  
> > it down and reproduce it in my simple test.
> > 
> > 1. A new index with 2 fields, one type long, another type string.  
> > curl -XPOST localhost:9200/test -d '{  
> > "mappings" : {  
> > "Cases" : {  
> > "properties" : {  
> > "case\_number" : { "type" : "long"},  
> > "desc" : { "type" : "string"}  
> > }  
> > }  
> > }  
> > }'
> > 
> > 2. Index a record  
> > curl -XPUT '[http://localhost:9200/test/Cases/1](http://localhost:9200/test/Cases/1)' -d '{  
> > "case\_number" : "123",  
> > "desc" : "abc"  
> > }'
> > 
> > 3. Search  
> > curl -XPOST "localhost:9200/test/Cases/\_search?pretty=true" -d '{  
> > "query": {  
> > "query\_string": {  
> > "fields" : ["case\_number", "desc"],  
> > "query":"abc"  
> > }  
> > }  
> > }'
> > 
> > The search returns status 500 and produces this error:  
> > "error" : "SearchPhaseExecutionException[Failed to execute phase  
> > [query\_fetch], total failure; shardFailures  
> > {[tFucKjGMTjeW2EoWEcXDww][test][0]: SearchParseException[[test][0]:  
> > from[-1],size[-1]: Parse Failure [Failed to parse source [{\n "query":  
> > {\n "query\_string": {\n "fields" : ["case\_number",  
> > "desc"],\n "query":"abc"\n }\n }\n}]]]; nested:  
> > NumberFormatException[For input string: "abc"]; }]",
> > 
> > But if I replace my query "abc" with "123", the error goes away. But as  
> > you  
> > can see I have both string and long fields in my index so I would expect  
> > both from users.
> > 
> > Is this a bug or something I have to do to make ES handle both long and  
> > string types?
> > 
> > I am using the latest ES (0.19.2).
> > 
> > Thanks.
> > 
> > --  
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3897459.html](http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3897459.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3901461.html](http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3901461.html)  
> To unsubscribe from Error when searching multiple fields with different  
> types, click here.  
> NAML[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro\_viewer&id=instant\_html!nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify\_subscribers!nabble%3Aemail.naml-instant\_emails!nabble%3Aemail.naml-send\_instant\_email!nabble%3Aemail.naml](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)
> 
> * * *
> 
> View this message in context: Re: Error when searching multiple fields  
> with different types[http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3905931.html](http://elasticsearch-users.115913.n3.nabble.com/Error-when-searching-multiple-fields-with-different-types-tp3897459p3905931.html)  
> Sent from the Elasticsearch Users mailing list archive[http://elasticsearch-users.115913.n3.nabble.com/](http://elasticsearch-users.115913.n3.nabble.com/)at [Nabble.com](http://Nabble.com).

---

<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:32am UTC](https://discuss.elastic.co/t/error-when-searching-multiple-fields-with-different-types/7275/5 "2017-07-06T03:32:47Z")

</div>


