Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search by exact match on text field is not working as expected #1020

Open
sgessa opened this issue Sep 11, 2022 · 1 comment
Open

Search by exact match on text field is not working as expected #1020

sgessa opened this issue Sep 11, 2022 · 1 comment

Comments

@sgessa
Copy link

sgessa commented Sep 11, 2022

Hi,
I have a text field, let's say title.
Sometimes I need to search for this field with a fulltext search which it works.
Other times I need to search by exact match but it doesn't work (it works for string type tho).
I tried with:

with(:title, value)
# or
with(:title).equal_to(value)

but it return records with a partial match, not the exact match (i.e. I search for test but it returns all documents with title testsomething).
So I ended up indexing the field with two different names and type (string and text).

What I am doing wrong?

@sixty4bit
Copy link

You have to create a separate search field for the exact match scenario.

class Article < ApplicationRecord
  searchable do
    text :title
    string :title_str
  end

  def title_str
    title
  end
end

Article.search do
  with(:title_str, params[:exact]) if params[:exact].present?
  fulltext params[:q] if params[:q].present?
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants