0

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!

1 Answer 1

2

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
              ]
           }
        }
       ]
     }
    }
  }
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the quick reply @richa I feel there is a limitation in using terms query because, I am preparing some dynamic query using query_string . So for e.g: right now, I am building query based on status and user_id and I am expecting more attributes to be added to this query_string as search gets narrower. I am a newbie into elasticsearch so, would it be possible to use both query_string and terms into 1 single query?
Updated my answer. have used term query for status instead of query_string
Thank you @richa your answer worked for me! Just for my information, I have used lot complex queries using query_string I've used almost everything query_string provides for e.g. LIKE, GTE, LTE but is [] not supported by query_string yet?
Sry but I have never used list in query_string . I prefer to use other queries instead of query_string
Sorry for putting up so many questions here but am I doing something wrong here? Article.search({ query: { bool: { must: [ { term: { status: 1 } }, { range: { comments: { gte: 2 } } }, { range: { replies: { lte: 3 } } }, { terms: { "contributors": [73089,73090] } }, { terms: { article_type: ["Blog"] } } ] } } }).results.total gives 0.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.