1

Let's say I have an index with multiple objects in it:

class ThingsIndex < Chewy::Index
  define_type User do
    field :full_name
  end 

  define_type Post do 
    field :title
  end
end

How do I search both users' full_name and posts' titles.

The docs only talk about querying one attribute like this:

ThingsIndex.query(term: {full_name: 'Foo'})
CodeOverload
  • 47,274
  • 54
  • 131
  • 219

1 Answers1

1

There are a couple ways you could do this. Chaining is probably the easiest:

ThingsIndex.query(term: {full_name: 'Foo'}).query(term: {title: 'Foo'})

If you need to do several queries, you might consider merging them:

query = ThingsIndex.query(term: {full_name: 'Foo'})
query = query.merge(ThingsIndex.query(term: {title: 'Foo'}))

Read more about merging here: Chewy #merge docs

Make sure to set your limit or else it only shows 10 results:

query.limit(50)

CMGS
  • 71
  • 1
  • 5