Reputation: 555
I am using ElasticSearch in 1 of my Ruby on Rails project. For ElasticSearch, I am heavily using query_string
Below are my Project setup:
Ruby-2.2.4, Rails-4.2.0, ElasticSearch-Model-0.1.8
I've already indexed the data. Using Article.import
Now, in console, I am fetching for all published article count, I am using:
Article.search(query: { query_string: { query: {"status:1"} } })
This works great! Now, I would like to find articles by user_ids
The SQL for above is working like charm!
SELECT * FROM articles WHERE articles.user_id IN (5, 10)
But, when I am trying to use the same concept for query_string, it doesn't work!!
Article.search(query: { query_string: { query: "status:1 AND user_id:[5, 10]" } })
This gives me the result:
Elasticsearch::Transport::Transport::Errors::BadRequest: [400] {"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[mUk_rhx-RP6G5sJRMpfDwg][articles][0]: SearchParseException[[articles][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"query_string\":{\"query\":\"status:1 AND user_id:[5,10]\"}}}]]]; nested: QueryParsingException[[articles] Failed to parse query [status:1 AND user_id:[5,10]]]; nested: ParseException[Cannot parse 'status:1 AND user_id:[5,10]': Encountered \" \"]\" \"] \"\" at line 1, column 98.\nWas expecting one of:\n \"TO\" ...\n <RANGE_QUOTED> ...\n <RANGE_GOOP> ...\n ];
Please help me understand what exactly I need to do to make this work! I googled a lot on this found several options like RANGE, TO etc. but not this one :(
Hope to hear from you soon!
Upvotes: 0
Views: 261
Reputation: 7649
You should not use query_string
in that case. Use terms query
instead. To combine multiple queries use bool
{
"query": {
"bool": {
"must": [
{
"term": {
"status": 1
}
},
{
"terms": {
"user_id": [
5,
10
]
}
}
]
}
}
}
Upvotes: 2