1

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 >= ?) 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.

1 Answer 1

1

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:>=#{range_start_date} AND start_date:<=#{range_end_date}) (start_date:<=#{range_start_date} AND end_date:>=#{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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.