# Search query related - REST API

**URL:** <https://discuss.elastic.co/t/search-query-related-rest-api/4030>\
**Category:** Elasticsearch\
**Created:** [March 4, 2011, 7:41am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030 "2011-03-04T07:41:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aditya\_kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@aditya\_kulkarni](https://discuss.elastic.co/u/aditya_kulkarni)\
**Post date:** [March 4, 2011, 7:41am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/1 "2011-03-04T07:41:47Z")

</div>

Hi,

system: ES 0.15.1  
platform: WinVista

I am using REST API. My document is as follows,  
{  
"user": "123", /-\>multiple users are possible  
"file\_name": "gftgf", /-\>multiple file names are possible  
"file\_extension": "xls", /-\>multiple file extensions are possible  
"path":"/temp/test\_dir/) /-\>multiple directories are possible  
}'  
I store it in index storage with type fileinformation  
e.g. curl -XPOST '[http://localhost:9200/storage/fileinformation/](http://localhost:9200/storage/fileinformation/)' -d '

Question1: how do I use REST API to get documents with specific extension say xls, for user 123?

I tried  
curl -XPOST '[http://localhost:9200/storage/fileinformation/\_search?pretty=true](http://localhost:9200/storage/fileinformation/_search?pretty=true)' -d '  
{  
"constantScore" : {  
"filter" : {  
"query" : {  
"query\_string" : {  
"query" : "123 AND xls"  
}  
}  
}  
}  
}'

Results from this are not stable. only once it fetched results, else it is throwing errors:  
{  
"error" : "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[-4OtAA0WRjiRWp4eUD-MHg][storage][3]: SearchParseExcept  
ion[[storage][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n"constantScore" : {\n"filter" : {\n"query" : { \n"query\_string" : { \n"query" : "123 AND xls"\n }\n }\n }\n }\n}]]];  
nested: SearchParseException[[storage][3]: from[-1],size[-1]: Parse Failure [No parser for element [constantScore]]]; }  
{[B7WcfgrhRxCgQw9gb7ifCw][storage][4]: RemoteTransportException[[Uncle Ben Parker][inet[/192.168.1.110:9301]][search/phase/query]];  
nested: SearchParseException[[storage][4]: from[-1],size[-1]: Parse Failure [Failedto parse source [\n{\n "constantScore" : {\n "filter" : {\n "query" : { \n "query\_string" : { \n"query" : "123 AND xls"\n }\n }\n }\n  
}\n}]]]; nested: SearchParseException[[storage][4]: from[-1],size[-1]: Parse Failure [No parser for element [constantScore]]]; }{[-4OtAA0WRjiRWp4eUD-MHg][storage][1]: SearchParseException[[storage][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n "constantScore" : {\n "filter" : {\n  
"query" : { \n "query\_string" : { \n "query" : "123 AND xls"\n }\n }\n }\n }\n}]]]; nested: SearchParseException[[storage][1]: from[-1],size[-1]: Parse Failure [No parser for element [constantScore]]]; }  
{[-4OtAA0WRjiRWp4eUD-MHg][fbdata][2]: SearchParseException[[storage][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n "constantScore" : {\n "filter" : {\n "query" : { \n "query\_string" : { \n "query" : "123 AND xls"\n }\n }\n }\n  
}\n}]]]; nested: SearchParseException[[storage][2]: from[-1],size[-1]: Parse Failure [No parser for element [constantScore]]]; }{[B7WcfgrhRxCgQw9gb7ifCw][fbda  
ta][0]: RemoteTransportException[[Uncle Ben Parker][inet[/192.168.1.110:9301]][search/phase/query]]; nested: SearchParseException[[storage][0]: from[-1],size[-1]  
: Parse Failure [Failed to parse source [\n{\n "constantScore" : {\n "filter" : {\n "query" : { \n "query\_string" :{ \n "query" : "123 AND xls"\n }\n }\n }\n }\n}]]]; nested: SearchParseException[[storage][0]: from[-1],size[-1]: Parse Failure [No parser for element [constantScore]]]; }]",  
"status" : 500}

Question2: how do I use REST API to get documents with specific extension say xls OR xlsx, for user 123?  
but

Best Regards,  
a

---

<div class="post-metadata">

**Author:** ![aditya\_kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@aditya\_kulkarni](https://discuss.elastic.co/u/aditya_kulkarni)\
**Post date:** [March 4, 2011, 7:55am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/2 "2011-03-04T07:55:48Z")

</div>

Okay, (probably my bad) if REST API usage was wrong in previous example.

I am finally getting results with

curl -XPOST '[http://localhost:9200/storage/fileinformation/\_search?pretty=true](http://localhost:9200/storage/fileinformation/_search?pretty=true)' -d '  
{  
"query" : {  
"query\_string" : { "query" : "xlsx OR xlx OR xls"}  
}  
}'

This solves my one problem, but problem still stays for finding results only for user id 123 (system may have another users 100, 102... and so on.

Any thoughts please?

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [March 4, 2011, 8:03am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/3 "2011-03-04T08:03:21Z")

</div>

Hi,

I would recommend using term (or terms) query for file extension (and btw  
you should consider using appropriate analyzer for this item).

> **[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.

> **[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.

or more efficient would be using filters

> **[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.

> **[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.

[http://www.elasticsearch.org/guide/reference/query-dsl/terms-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/terms-query.html)As  
for the "user" query you can either add "user:123" into your query\_string  
value or using query filter:

> **[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.

[http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/query-filter.html)  
Regards,  
Lukas

On Fri, Mar 4, 2011 at 8:55 AM, aditya.kulkarni  
[aditya.kulkarni@gmail.com](mailto:aditya.kulkarni@gmail.com)wrote:

> Okay, (probably my bad) if REST API usage was wrong in previous example.
> 
> I am finally getting results with
> 
> curl -XPOST  
> '[http://localhost:9200/storage/fileinformation/\_search?pretty=true](http://localhost:9200/storage/fileinformation/_search?pretty=true)' -d '  
> {  
> "query" : {  
> "query\_string" : { "query" : "xlsx OR xlx OR xls"}  
> }  
> }'
> 
> This solves my one problem, but problem still stays for finding results  
> only  
> for user id 123 (system may have another users 100, 102... and so on.
> 
> Any thoughts please?
> 
> * * *
> 
> ## Best Regards, a
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/search-query-related-REST-API-tp2631676p2631733.html](http://elasticsearch-users.115913.n3.nabble.com/search-query-related-REST-API-tp2631676p2631733.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![aditya\_kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@aditya\_kulkarni](https://discuss.elastic.co/u/aditya_kulkarni)\
**Post date:** [March 4, 2011, 8:29am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/4 "2011-03-04T08:29:52Z")

</div>

@Lukas: Thanks for quick response.

I got results what I wanted i.e. finding results only for user id 123 with multiple extensions using following:

curl -XPOST '[http://localhost:9200/storage/fileinformation/\_search?pretty=true](http://localhost:9200/storage/fileinformation/_search?pretty=true)' -d '  
{  
"sort" :[{ "date" : {"reverse" : false }}], //-\> this is new field added, storing date for sort order.  
"query" : {  
"query\_string" : { "query" : "123 AND ( xls OR xlsx )"}  
}  
}'

@Lukas: I will take up your hint and try to focus on "more efficient would be using filters". Is efficiency from memory or processor requirements?

---

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [March 4, 2011, 8:58am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/5 "2011-03-04T08:58:38Z")

</div>

Hi,

right this works for you now but what if there will be user called "xls" or  
file extension called "123"? Would this still work fine for you?

As far as I can see you are trying to pull documents that belong to some  
user, have specific file extensions and order the set of data by date. Now,  
search engines (or Lucene in general) was mainly built to pull relevant  
documents for you (based on the given query) and give you the most relevant  
documents first. In your use case you are not using relevancy at all but you  
are using it more in SQL fashion. Not saying it is wrong, that can be valid  
use case but you can gain a lot of performance boost (processing speed up)  
by giving search engine a hit and tell it not to calculate the relevancy.  
And that is what filters was made for.

In your case you can simply tell the search engine: give me all document, do  
not calculate the relevancy, and filter our everything except user:123 and  
file\_extension:(xls OR xla)  
Something like this:

{ "sort" ... ,  
"query" :  
{  
"constant\_score" : {  
"filter" : {  
"and" : [  
{ "terms" : { "file\_extension" : ["xls","xla"]} }, // using exact  
match  
{ "query" : { "field" : { "user" : "123" }}} // using field query on  
"user"  
]  
}  
}  
}

}

Disclaimer: I did not test this query so there can be some typos and issues,  
but basically the idea should be working.

Regards,  
Lukas

On Fri, Mar 4, 2011 at 9:29 AM, aditya.kulkarni  
[aditya.kulkarni@gmail.com](mailto:aditya.kulkarni@gmail.com)wrote:

> @Lukas: Thanks for quick response.
> 
> I got results what I wanted i.e. finding results only for user id 123 with  
> multiple extensions using following:
> 
> curl -XPOST  
> '[http://localhost:9200/storage/fileinformation/\_search?pretty=true](http://localhost:9200/storage/fileinformation/_search?pretty=true)' -d '  
> {  
> "sort" :[{ "date" : {"reverse" : false }}], //-\> this is new  
> field added,  
> storing date for sort order.  
> "query" : {  
> "query\_string" : { "query" : "123 AND ( xls OR xlsx )"}  
> }  
> }'
> 
> @Lukas: I will take up your hint and try to focus on "more efficient would  
> be using filters". Is efficiency from memory or processor requirements?
> 
> * * *
> 
> ## Best Regards, a
> 
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/search-query-related-REST-API-tp2631676p2631864.html](http://elasticsearch-users.115913.n3.nabble.com/search-query-related-REST-API-tp2631676p2631864.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![aditya\_kulkarni](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@aditya\_kulkarni](https://discuss.elastic.co/u/aditya_kulkarni)\
**Post date:** [March 5, 2011, 4:40pm UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/6 "2011-03-05T16:40:42Z")

</div>

Hi,  
@Lukas: I will check and update thread back. I am sure, newbies like me will certainly help from this. 🙂  
Thanks again.

Best Regards,  
Aditya

---

<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, 4:10am UTC](https://discuss.elastic.co/t/search-query-related-rest-api/4030/7 "2017-07-06T04:10:46Z")

</div>


