# Spaces in terms in request body make the query return no results

**URL:** <https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166>\
**Category:** Elasticsearch\
**Created:** [April 24, 2014, 6:42am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166 "2014-04-24T06:42:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexey\_Kotlyarov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexey_kotlyarov/32/1386_2.png) [@Alexey\_Kotlyarov](https://discuss.elastic.co/u/Alexey_Kotlyarov)\
**Post date:** [April 24, 2014, 6:42am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/1 "2014-04-24T06:42:38Z")

</div>

Given a simple index:

```
curl -XPUT http://localhost:9200/twitter/tweet/1 -d '{"message": "test 

```

message"}'

A query for "test" returns the tweet:

```
curl -XPOST 'http://localhost:9200/twitter/tweet/_search?pretty' -d 

```

'{"query": {"prefix": {"message": "test"}}}'  
curl -XGET '[http://localhost:9200/twitter/tweet/\_search?pretty](http://localhost:9200/twitter/tweet/_search?pretty)' -d  
'{"query": {"prefix": {"message": "test"}}}'

However, if I search for "test ", there are no results:

```
curl -XPOST 'http://localhost:9200/twitter/tweet/_search?pretty' -d 

```

'{"query": {"prefix": {"message": "test "}}}'  
curl -XGET '[http://localhost:9200/twitter/tweet/\_search?pretty](http://localhost:9200/twitter/tweet/_search?pretty)' -d  
'{"query": {"prefix": {"message": "test "}}}'

However again, the same query works fine if put into the URL (using wget  
and not curl because curl tries to expand the braces):

```
wget -O - 

```

'[http://localhost:9200/twitter/tweet/\_search?{"query":{"prefix":{"message":"test%20"}](http://localhost:9200/twitter/tweet/_search?%7B%22query%22:%7B%22prefix%22:%7B%22message%22:%22test%20%22%7D)}}'

How do I make the queries with "test " work when they are supplied in the  
request body?

My Elasticsearch version is 1.1.1.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e17173c2-7e9d-429f-b36e-e897a695b56e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e17173c2-7e9d-429f-b36e-e897a695b56e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [April 24, 2014, 6:53am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/2 "2014-04-24T06:53:59Z")

</div>

Hi Alexey,

Your message field is analyzed by default using the Standard Analyzer:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

This means your "test message" will become ["test", "message"].

On the other hand, the prefix query isn't analyzed. Which means "test" will  
match but "test " won't, because you have no term that begins with that  
string.

One solution for this is to index your message field as not\_analyzed. This  
will only generate the term "test message" which will match both "test" and  
"test " prefixes. However, if you search for the "test" term, it won't  
match because you have no such term.

You can have the best of both worlds by indexing the same text multiple  
times with multiple settings:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

## Best regards, Radu

Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

On Thu, Apr 24, 2014 at 9:42 AM, Alexey Kotlyarov [koterpillar@gmail.com](mailto:koterpillar@gmail.com)wrote:

> Given a simple index:
> 
> ```
> curl -XPUT http://localhost:9200/twitter/tweet/1 -d '{"message":
> 
> ```
> 
> "test message"}'
> 
> A query for "test" returns the tweet:
> 
> ```
> curl -XPOST 'http://localhost:9200/twitter/tweet/_search?pretty' -d
> 
> ```
> 
> '{"query": {"prefix": {"message": "test"}}}'  
> curl -XGET '[http://localhost:9200/twitter/tweet/\_search?pretty](http://localhost:9200/twitter/tweet/_search?pretty)' -d  
> '{"query": {"prefix": {"message": "test"}}}'
> 
> However, if I search for "test ", there are no results:
> 
> ```
> curl -XPOST 'http://localhost:9200/twitter/tweet/_search?pretty' -d
> 
> ```
> 
> '{"query": {"prefix": {"message": "test "}}}'  
> curl -XGET '[http://localhost:9200/twitter/tweet/\_search?pretty](http://localhost:9200/twitter/tweet/_search?pretty)' -d  
> '{"query": {"prefix": {"message": "test "}}}'
> 
> However again, the same query works fine if put into the URL (using wget  
> and not curl because curl tries to expand the braces):
> 
> ```
> wget -O - '
> 
> ```
> 
> [http://localhost:9200/twitter/tweet/\_search?{"query":{"prefix":{"message":"test%20"}](http://localhost:9200/twitter/tweet/_search?%7B%22query%22:%7B%22prefix%22:%7B%22message%22:%22test%20%22%7D)}}[http://localhost:9200/twitter/tweet/\_search?{"query":{"prefix":{"message":"test%20"}}}](http://localhost:9200/twitter/tweet/_search?%7B%22query%22:%7B%22prefix%22:%7B%22message%22:%22test%20%22%7D%7D%7D)  
> '
> 
> How do I make the queries with "test " work when they are supplied in the  
> request body?
> 
> My Elasticsearch version is 1.1.1.
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/e17173c2-7e9d-429f-b36e-e897a695b56e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e17173c2-7e9d-429f-b36e-e897a695b56e%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/e17173c2-7e9d-429f-b36e-e897a695b56e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/e17173c2-7e9d-429f-b36e-e897a695b56e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_1xAjxqGHTcT-7dQqEQRWoqLEXwkG5czbOUWNpasFpjdg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_1xAjxqGHTcT-7dQqEQRWoqLEXwkG5czbOUWNpasFpjdg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Alexey\_Kotlyarov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexey_kotlyarov/32/1386_2.png) [@Alexey\_Kotlyarov](https://discuss.elastic.co/u/Alexey_Kotlyarov)\
**Post date:** [April 24, 2014, 6:57am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/3 "2014-04-24T06:57:38Z")

</div>

> Your message field is analyzed by default using the Standard Analyzer:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html)
> 
> This means your "test message" will become ["test", "message"].
> 
> On the other hand, the prefix query isn't analyzed. Which means "test"  
> will match but "test " won't, because you have no term that begins with  
> that string.
> 
> One solution for this is to index your message field as not\_analyzed. This  
> will only generate the term "test message" which will match both "test" and  
> "test " prefixes. However, if you search for the "test" term, it won't  
> match because you have no such term.
> 
> You can have the best of both worlds by indexing the same text multiple  
> times with multiple settings:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_multi_fields.html)

Why does the query work if given in the request query string then?

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0d9a28b3-5a61-4be2-9549-d6688e1715c1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0d9a28b3-5a61-4be2-9549-d6688e1715c1%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [April 24, 2014, 7:06am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/4 "2014-04-24T07:06:03Z")

</div>

On Thu, Apr 24, 2014 at 9:57 AM, Alexey Kotlyarov [koterpillar@gmail.com](mailto:koterpillar@gmail.com)wrote:

> > Your message field is analyzed by default using the Standard Analyzer:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > current/analysis-standard-analyzer.html
> > 
> > This means your "test message" will become ["test", "message"].
> > 
> > On the other hand, the prefix query isn't analyzed. Which means "test"  
> > will match but "test " won't, because you have no term that begins with  
> > that string.
> > 
> > One solution for this is to index your message field as not\_analyzed.  
> > This will only generate the term "test message" which will match both  
> > "test" and "test " prefixes. However, if you search for the "test" term, it  
> > won't match because you have no such term.
> > 
> > You can have the best of both worlds by indexing the same text multiple  
> > times with multiple settings: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/)  
> > elasticsearch/reference/current/\_multi\_fields.html
> 
> Why does the query work if given in the request query string then?

I think that isn't a valid query. I've just tried it and if I put "bla" in  
there I still get the result back. Basically, it will run a match\_all  
query. It's like doing this:

curl [http://localhost:9200/twitter/tweet/\_search?bla](http://localhost:9200/twitter/tweet/_search?bla)

If you want to do an URI search, you need to put things in the "q"  
parameter:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

But not that a URI search will run a query\_string query, which is analyzed:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_2bi33sFpjtTXB7BDQZM-bm78YP96SOrRuJnwD5oJOm8Q%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_2bi33sFpjtTXB7BDQZM-bm78YP96SOrRuJnwD5oJOm8Q%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Alexey\_Kotlyarov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexey_kotlyarov/32/1386_2.png) [@Alexey\_Kotlyarov](https://discuss.elastic.co/u/Alexey_Kotlyarov)\
**Post date:** [April 25, 2014, 7:30am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/5 "2014-04-25T07:30:05Z")

</div>

> I think that isn't a valid query. I've just tried it and if I put "bla" in  
> there I still get the result back. Basically, it will run a match\_all  
> query. It's like doing this:
> 
> curl [http://localhost:9200/twitter/tweet/\_search?bla](http://localhost:9200/twitter/tweet/_search?bla)
> 
> If you want to do an URI search, you need to put things in the "q"  
> parameter:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-uri-request.html)
> 
> But not that a URI search will run a query\_string query, which is  
> analyzed:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html)

I have tested this; you're right, the query submitted in this way is  
equivalent to a "match\_all". Thank you for the explanations!

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/79c28ade-2830-481b-8841-be9d55cc9022%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/79c28ade-2830-481b-8841-be9d55cc9022%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [April 25, 2014, 2:19pm UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/6 "2014-04-25T14:19:34Z")

</div>

Great! You're welcome

Best regards,  
Radu

Performance Monitoring \* Log Analytics \* Search Analytics  
Solr & Elasticsearch Support \* [http://sematext.com/](http://sematext.com/)

On Fri, Apr 25, 2014 at 10:30 AM, Alexey Kotlyarov [koterpillar@gmail.com](mailto:koterpillar@gmail.com)wrote:

> I think that isn't a valid query. I've just tried it and if I put "bla" in
> 
> > there I still get the result back. Basically, it will run a match\_all  
> > query. It's like doing this:
> > 
> > curl [http://localhost:9200/twitter/tweet/\_search?bla](http://localhost:9200/twitter/tweet/_search?bla)
> > 
> > If you want to do an URI search, you need to put things in the "q"  
> > parameter: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > current/search-uri-request.html
> > 
> > But not that a URI search will run a query\_string query, which is  
> > analyzed: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/)  
> > current/query-dsl-query-string-query.html
> 
> I have tested this; you're right, the query submitted in this way is  
> equivalent to a "match\_all". Thank you for the explanations!
> 
> --  
> 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).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/79c28ade-2830-481b-8841-be9d55cc9022%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/79c28ade-2830-481b-8841-be9d55cc9022%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/79c28ade-2830-481b-8841-be9d55cc9022%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/79c28ade-2830-481b-8841-be9d55cc9022%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAHXA0\_1cix\_eY7L2RY-1wz6Hs6g8Jm06d6d1tf9QsoBcE%3Dg8Tg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHXA0_1cix_eY7L2RY-1wz6Hs6g8Jm06d6d1tf9QsoBcE%3Dg8Tg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:33am UTC](https://discuss.elastic.co/t/spaces-in-terms-in-request-body-make-the-query-return-no-results/17166/7 "2017-07-06T01:33:31Z")

</div>


