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!

Puneet Pandey
  • 541
  • 2
  • 6
  • 23

1 Answers1

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
              ]
           }
        }
       ]
     }
    }
  }
Richa
  • 7,419
  • 6
  • 25
  • 34
  • 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? – Puneet Pandey Mar 02 '16 at 11:27
  • Updated my answer. have used `term` query for status instead of `query_string` – Richa Mar 02 '16 at 11:42
  • 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? – Puneet Pandey Mar 02 '16 at 12:00
  • Sry but I have never used list in `query_string` . I prefer to use other queries instead of `query_string` – Richa Mar 02 '16 at 12:03
  • 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. – Puneet Pandey Mar 02 '16 at 13:17
  • The query Seems fine. :) – Richa Mar 02 '16 at 13:20