Search query related - REST API


(aditya.kulkarni) #1

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/' -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' -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


(aditya.kulkarni) #2

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' -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?


(Lukáš Vlček) #3

Hi,

I would recommend using term (or terms) query for file extension (and btw
you should consider using appropriate analyzer for this item).
http://www.elasticsearch.org/guide/reference/query-dsl/term-query.html
http://www.elasticsearch.org/guide/reference/query-dsl/terms-query.html

or more efficient would be using filters
http://www.elasticsearch.org/guide/reference/query-dsl/term-filter.html
http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html

http://www.elasticsearch.org/guide/reference/query-dsl/terms-query.htmlAs
for the "user" query you can either add "user:123" into your query_string
value or using query filter:
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.comwrote:

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' -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
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(aditya.kulkarni) #4

@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' -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?


(Lukáš Vlček) #5

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.comwrote:

@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' -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
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(aditya.kulkarni) #6

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

Best Regards,
Aditya


(system) #7