Title: Elastic search Query String in Rails 4
Tags: ruby-on-rails-4;elasticsearch
Question: I am using Elasticsearch in my application to search for a matching word anywhere in a table.
This is the query string i have used to fetch my result:
search({ query: { prefix: { _all:  keywords }}, sort: [ { start_date: 'asc', start_time: 'asc' } ] })
The selected records were then being queried with the dates to match the date range(s) specified in the application, by the following query:
where("status_id= ? and active=? and (((start_date &gt;= ?) and (start_date <= ?))
or ((start_date <= ?) and (? <= end_date)))",2,true,range_start_date,
range_end_date,range_start_date,range_start_date)
But i know this is not a good way to fetch results. Now i want to modify this to fetch just the required data from elasticsearch index.
After a long search i found "query_string" and "simple_query_string" to match my requirement. But i am unsuccessful till now to get the required result.
How can i append the query with the elasticsearch result to get the required records?
Can someone please help?
Thanks in advance.


Here is another answer: Finally, i was able to find the answer for the question myself. I was able to filter the searched content according to date with a "filter" keyword.
I modified the query as:
search_query = {
query: {
prefix: {
_all:  keywords
filter: {
query: {
query_string: {
query: "status_id:2 AND active:true AND
((start_date:&gt;=#{range_start_date} AND start_date:<=#{range_end_date}) (start_date:<=#{range_start_date} AND end_date:&gt;=#{range_start_date}))"
}
sort: [ { start_date: 'asc', start_time: 'asc' } ] }
And finally fetched the result by:
@result = self.search(search_query)
If there is anyway i could modify this code, please suggest. Thank You.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:07/11/16', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<ruby-on-rails-4><elasticsearch>', 'Title': 'Elastic search Query String in Rails 4', 'ViewCount': 257, 'id': 38301097, 'user_AboutMe': '<p>I am a fresher, recently recruited for the post of Software Engineer at Maxxion System Pvt. Ltd. I am working in ruby on rails development platform. Seeking for more knowledge in this field.</p>\n', 'user_AccountId': 8795230.0, 'user_DisplayName': 'Toby Joy', 'user_Reputation': 21.0, 'user_Views': 17.0, 'user_id': 6573321.0}}