ElasticSearch / Searchkick - LIKE query

1.2k views Asked by At

I'm using ElasticSearch gem Searchkick with Rails and I want to perform a search based on 2 values:

- params[:full_text_query]
- params[:only_from_this_location]

One should search full-text on all fields and another should restrict the search only to records that partially match one field.

I want something like this:

Post.search(params[:full_text_query], where: { location: params[:only_from_this_location] })

However I need location filter to match not only exact values, but also partial ones. So not only "New York", but also "New York, US". Like %something% you would do in SQL.

I also need support for pagination so it needs to be in one query.

What are my options?

2

There are 2 answers

0
Vijith mv On

Regular expressions are allowed to use inside Searchkick where method. Try using something like this in your code.

Post.search(params[:full_text_query], where: { location: /.*#{params[:only_from_this_location]}.*/ })
0
yassine2020 On

I'm also struggling with this kind of issue in my current project, if you found anything to solve the query LIKE problem in searchckick please share.

However concerning the pagination you can add the following parameter to your line

Post.search(params[:full_text_query], where: { location: params[:only_from_this_location] }, per_page: 10)