Giter VIP home page Giter VIP logo

searchkick's Introduction

Searchkick

🚀 Intelligent search made easy

Searchkick learns what your users are looking for. As more people search, it gets smarter and the results get better. It’s friendly for developers - and magical for your users.

Searchkick handles:

  • stemming - tomatoes matches tomato
  • special characters - jalapeno matches jalapeño
  • extra whitespace - dishwasher matches dish washer
  • misspellings - zuchini matches zucchini
  • custom synonyms - pop matches soda

Plus:

  • query like SQL - no need to learn a new query language
  • reindex without downtime
  • easily personalize results for each user
  • autocomplete
  • “Did you mean” suggestions
  • supports many languages
  • works with Active Record and Mongoid

Check out Searchjoy for analytics and Autosuggest for query suggestions

🍊 Battle-tested at Instacart

Build Status

Contents

Getting Started

Install Elasticsearch or OpenSearch. For Homebrew, use:

brew install elastic/tap/elasticsearch-full
brew services start elasticsearch-full
# or
brew install opensearch
brew services start opensearch

Add these lines to your application’s Gemfile:

gem "searchkick"

gem "elasticsearch"   # select one
gem "opensearch-ruby" # select one

The latest version works with Elasticsearch 7 and 8 and OpenSearch 1 and 2. For Elasticsearch 6, use version 4.6.3 and this readme.

Add searchkick to models you want to search.

class Product < ApplicationRecord
  searchkick
end

Add data to the search index.

Product.reindex

And to query, use:

products = Product.search("apples")
products.each do |product|
  puts product.name
end

Searchkick supports the complete Elasticsearch Search API and OpenSearch Search API. As your search becomes more advanced, we recommend you use the search server DSL for maximum flexibility.

Querying

Query like SQL

Product.search("apples", where: {in_stock: true}, limit: 10, offset: 50)

Search specific fields

fields: [:name, :brand]

Where

where: {
  expires_at: {gt: Time.now},    # lt, gte, lte also available
  orders_count: 1..10,           # equivalent to {gte: 1, lte: 10}
  aisle_id: [25, 30],            # in
  store_id: {not: 2},            # not
  aisle_id: {not: [25, 30]},     # not in
  user_ids: {all: [1, 3]},       # all elements in array
  category: {like: "%frozen%"},  # like
  category: {ilike: "%frozen%"}, # ilike
  category: /frozen .+/,         # regexp
  category: {prefix: "frozen"},  # prefix
  store_id: {exists: true},      # exists
  _not: {store_id: 1},           # negate a condition
  _or: [{in_stock: true}, {backordered: true}],
  _and: [{in_stock: true}, {backordered: true}]
}

Order

order: {_score: :desc} # most relevant first - default

All of these sort options are supported

Limit / offset

limit: 20, offset: 40

Select

select: [:name]

These source filtering options are supported

Results

Searches return a Searchkick::Relation object. This responds like an array to most methods.

results = Product.search("milk")
results.size
results.any?
results.each { |result| ... }

By default, ids are fetched from the search server and records are fetched from your database. To fetch everything from the search server, use:

Product.search("apples", load: false)

Get total results

results.total_count

Get the time the search took (in milliseconds)

results.took

Get the full response from the search server

results.response

Note: By default, Elasticsearch and OpenSearch limit paging to the first 10,000 results for performance. This applies to the total count as well.

Boosting

Boost important fields

fields: ["title^10", "description"]

Boost by the value of a field (field must be numeric)

boost_by: [:orders_count] # give popular documents a little boost
boost_by: {orders_count: {factor: 10}} # default factor is 1

Boost matching documents

boost_where: {user_id: 1}
boost_where: {user_id: {value: 1, factor: 100}} # default factor is 1000
boost_where: {user_id: [{value: 1, factor: 100}, {value: 2, factor: 200}]}

Boost by recency

boost_by_recency: {created_at: {scale: "7d", decay: 0.5}}

You can also boost by:

Get Everything

Use a * for the query.

Product.search("*")

Pagination

Plays nicely with kaminari and will_paginate.

# controller
@products = Product.search("milk", page: params[:page], per_page: 20)

View with kaminari

<%= paginate @products %>

View with will_paginate

<%= will_paginate @products %>

Partial Matches

By default, results must match all words in the query.

Product.search("fresh honey") # fresh AND honey

To change this, use:

Product.search("fresh honey", operator: "or") # fresh OR honey

By default, results must match the entire word - back will not match backpack. You can change this behavior with:

class Product < ApplicationRecord
  searchkick word_start: [:name]
end

And to search (after you reindex):

Product.search("back", fields: [:name], match: :word_start)

Available options are:

Option Matches Example
:word entire word apple matches apple
:word_start start of word app matches apple
:word_middle any part of word ppl matches apple
:word_end end of word ple matches apple
:text_start start of text gre matches green apple, app does not match
:text_middle any part of text een app matches green apple
:text_end end of text ple matches green apple, een does not match

The default is :word. The most matches will happen with :word_middle.

To specify different matching for different fields, use:

Product.search(query, fields: [{name: :word_start}, {brand: :word_middle}])

Exact Matches

To match a field exactly (case-sensitive), use:

Product.search(query, fields: [{name: :exact}])

Phrase Matches

To only match the exact order, use:

Product.search("fresh honey", match: :phrase)

Stemming and Language

Searchkick stems words by default for better matching. apple and apples both stem to appl, so searches for either term will have the same matches.

Searchkick defaults to English for stemming. To change this, use:

class Product < ApplicationRecord
  searchkick language: "german"
end

See the list of languages. A few languages require plugins:

You can also use a Hunspell dictionary for stemming.

class Product < ApplicationRecord
  searchkick stemmer: {type: "hunspell", locale: "en_US"}
end

Disable stemming with:

class Image < ApplicationRecord
  searchkick stem: false
end

Exclude certain words from stemming with:

class Image < ApplicationRecord
  searchkick stem_exclusion: ["apples"]
end

Or change how words are stemmed:

class Image < ApplicationRecord
  searchkick stemmer_override: ["apples => other"]
end

Synonyms

class Product < ApplicationRecord
  searchkick search_synonyms: [["pop", "soda"], ["burger", "hamburger"]]
end

Call Product.reindex after changing synonyms. Synonyms are applied at search time before stemming, and can be a single word or multiple words.

For directional synonyms, use:

search_synonyms: ["lightbulb => halogenlamp"]

Dynamic Synonyms

The above approach works well when your synonym list is static, but in practice, this is often not the case. When you analyze search conversions, you often want to add new synonyms without a full reindex.

Elasticsearch 7.3+ and OpenSearch

For Elasticsearch 7.3+ and OpenSearch, we recommend placing synonyms in a file on the search server (in the config directory). This allows you to reload synonyms without reindexing.

pop, soda
burger, hamburger

Then use:

class Product < ApplicationRecord
  searchkick search_synonyms: "synonyms.txt"
end

And reload with:

Product.search_index.reload_synonyms

Elasticsearch < 7.3

You can use a library like ActsAsTaggableOn and do:

class Product < ApplicationRecord
  acts_as_taggable
  scope :search_import, -> { includes(:tags) }

  def search_data
    {
      name_tagged: "#{name} #{tags.map(&:name).join(" ")}"
    }
  end
end

Search with:

Product.search(query, fields: [:name_tagged])

Misspellings

By default, Searchkick handles misspelled queries by returning results with an edit distance of one.

You can change this with:

Product.search("zucini", misspellings: {edit_distance: 2}) # zucchini

To prevent poor precision and improve performance for correctly spelled queries (which should be a majority for most applications), Searchkick can first perform a search without misspellings, and if there are too few results, perform another with them.

Product.search("zuchini", misspellings: {below: 5})

If there are fewer than 5 results, a 2nd search is performed with misspellings enabled. The result of this query is returned.

Turn off misspellings with:

Product.search("zuchini", misspellings: false) # no zucchini

Specify which fields can include misspellings with:

Product.search("zucini", fields: [:name, :color], misspellings: {fields: [:name]})

When doing this, you must also specify fields to search

Bad Matches

If a user searches butter, they may also get results for peanut butter. To prevent this, use:

Product.search("butter", exclude: ["peanut butter"])

You can map queries and terms to exclude with:

exclude_queries = {
  "butter" => ["peanut butter"],
  "cream" => ["ice cream", "whipped cream"]
}

Product.search(query, exclude: exclude_queries[query])

You can demote results by boosting by a factor less than one:

Product.search("butter", boost_where: {category: {value: "pantry", factor: 0.5}})

Emoji

Search 🍨🍰 and get ice cream cake!

Add this line to your application’s Gemfile:

gem "gemoji-parser"

And use:

Product.search("🍨🍰", emoji: true)

Indexing

Control what data is indexed with the search_data method. Call Product.reindex after changing this method.

class Product < ApplicationRecord
  belongs_to :department

  def search_data
    {
      name: name,
      department_name: department.name,
      on_sale: sale_price.present?
    }
  end
end

Searchkick uses find_in_batches to import documents. To eager load associations, use the search_import scope.

class Product < ApplicationRecord
  scope :search_import, -> { includes(:department) }
end

By default, all records are indexed. To control which records are indexed, use the should_index? method.

class Product < ApplicationRecord
  def should_index?
    active # only index active records
  end
end

If a reindex is interrupted, you can resume it with:

Product.reindex(resume: true)

For large data sets, try parallel reindexing.

To Reindex, or Not to Reindex

Reindex

  • when you install or upgrade searchkick
  • change the search_data method
  • change the searchkick method

No need to reindex

  • app starts

Strategies

There are four strategies for keeping the index synced with your database.

  1. Inline (default)

Anytime a record is inserted, updated, or deleted

  1. Asynchronous

Use background jobs for better performance

class Product < ApplicationRecord
  searchkick callbacks: :async
end

Jobs are added to a queue named searchkick.

  1. Queuing

Push ids of records that need updated to a queue and reindex in the background in batches. This is more performant than the asynchronous method, which updates records individually. See how to set up.

  1. Manual

Turn off automatic syncing

class Product < ApplicationRecord
  searchkick callbacks: false
end

And reindex a record or relation manually.

product.reindex
# or
store.products.reindex(mode: :async)

You can also do bulk updates.

Searchkick.callbacks(:bulk) do
  Product.find_each(&:update_fields)
end

Or temporarily skip updates.

Searchkick.callbacks(false) do
  Product.find_each(&:update_fields)
end

Or override the model’s strategy.

product.reindex(mode: :async) # :inline or :queue

Associations

Data is not automatically synced when an association is updated. If this is desired, add a callback to reindex:

class Image < ApplicationRecord
  belongs_to :product

  after_commit :reindex_product

  def reindex_product
    product.reindex
  end
end

Default Scopes

If you have a default scope that filters records, use the should_index? method to exclude them from indexing:

class Product < ApplicationRecord
  default_scope { where(deleted_at: nil) }

  def should_index?
    deleted_at.nil?
  end
end

If you want to index and search filtered records, set:

class Product < ApplicationRecord
  searchkick unscope: true
end

Intelligent Search

The best starting point to improve your search by far is to track searches and conversions. Searchjoy makes it easy.

Product.search("apple", track: {user_id: current_user.id})

See the docs for how to install and use. Focus on top searches with a low conversion rate.

Searchkick can then use the conversion data to learn what users are looking for. If a user searches for “ice cream” and adds Ben & Jerry’s Chunky Monkey to the cart (our conversion metric at Instacart), that item gets a little more weight for similar searches. This can make a huge difference on the quality of your search.

Add conversion data with:

class Product < ApplicationRecord
  has_many :conversions, class_name: "Searchjoy::Conversion", as: :convertable
  has_many :searches, class_name: "Searchjoy::Search", through: :conversions

  searchkick conversions: [:conversions] # name of field

  def search_data
    {
      name: name,
      conversions: searches.group(:query).distinct.count(:user_id)
      # {"ice cream" => 234, "chocolate" => 67, "cream" => 2}
    }
  end
end

Reindex and set up a cron job to add new conversions daily. For zero downtime deployment, temporarily set conversions: false in your search calls until the data is reindexed.

Performant Conversions

A performant way to do conversions is to cache them to prevent N+1 queries. For Postgres, create a migration with:

add_column :products, :search_conversions, :jsonb

For MySQL, use :json, and for others, use :text with a JSON serializer.

Next, update your model. Create a separate method for conversion data so you can use partial reindexing.

class Product < ApplicationRecord
  searchkick conversions: [:conversions]

  def search_data
    {
      name: name,
      category: category
    }.merge(conversions_data)
  end

  def conversions_data
    {
      conversions: search_conversions || {}
    }
  end
end

Deploy and reindex your data. For zero downtime deployment, temporarily set conversions: false in your search calls until the data is reindexed.

Product.reindex

Then, create a job to update the conversions column and reindex records with new conversions. Here’s one you can use for Searchjoy:

class UpdateConversionsJob < ApplicationJob
  def perform(class_name, since: nil, update: true, reindex: true)
    model = Searchkick.load_model(class_name)

    # get records that have a recent conversion
    recently_converted_ids =
      Searchjoy::Conversion.where(convertable_type: class_name).where(created_at: since..)
      .order(:convertable_id).distinct.pluck(:convertable_id)

    # split into batches
    recently_converted_ids.in_groups_of(1000, false) do |ids|
      if update
        # fetch conversions
        conversions =
          Searchjoy::Conversion.where(convertable_id: ids, convertable_type: class_name)
          .joins(:search).where.not(searchjoy_searches: {user_id: nil})
          .group(:convertable_id, :query).distinct.count(:user_id)

        # group by record
        conversions_by_record = {}
        conversions.each do |(id, query), count|
          (conversions_by_record[id] ||= {})[query] = count
        end

        # update conversions column
        model.transaction do
          conversions_by_record.each do |id, conversions|
            model.where(id: id).update_all(search_conversions: conversions)
          end
        end
      end

      if reindex
        # reindex conversions data
        model.where(id: ids).reindex(:conversions_data)
      end
    end
  end
end

Run the job:

UpdateConversionsJob.perform_now("Product")

And set it up to run daily.

UpdateConversionsJob.perform_later("Product", since: 1.day.ago)

Personalized Results

Order results differently for each user. For example, show a user’s previously purchased products before other results.

class Product < ApplicationRecord
  def search_data
    {
      name: name,
      orderer_ids: orders.pluck(:user_id) # boost this product for these users
    }
  end
end

Reindex and search with:

Product.search("milk", boost_where: {orderer_ids: current_user.id})

Instant Search / Autocomplete

Autocomplete predicts what a user will type, making the search experience faster and easier.

Autocomplete

Note: To autocomplete on search terms rather than results, check out Autosuggest.

Note 2: If you only have a few thousand records, don’t use Searchkick for autocomplete. It’s much faster to load all records into JavaScript and autocomplete there (eliminates network requests).

First, specify which fields use this feature. This is necessary since autocomplete can increase the index size significantly, but don’t worry - this gives you blazing faster queries.

class Movie < ApplicationRecord
  searchkick word_start: [:title, :director]
end

Reindex and search with:

Movie.search("jurassic pa", fields: [:title], match: :word_start)

Typically, you want to use a JavaScript library like typeahead.js or jQuery UI.

Here’s how to make it work with Rails

First, add a route and controller action.

class MoviesController < ApplicationController
  def autocomplete
    render json: Movie.search(params[:query], {
      fields: ["title^5", "director"],
      match: :word_start,
      limit: 10,
      load: false,
      misspellings: {below: 5}
    }).map(&:title)
  end
end

Note: Use load: false and misspellings: {below: n} (or misspellings: false) for best performance.

Then add the search box and JavaScript code to a view.

<input type="text" id="query" name="query" />

<script src="jquery.js"></script>
<script src="typeahead.bundle.js"></script>
<script>
  var movies = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
      url: '/movies/autocomplete?query=%QUERY',
      wildcard: '%QUERY'
    }
  });
  $('#query').typeahead(null, {
    source: movies
  });
</script>

Suggestions

Suggest

class Product < ApplicationRecord
  searchkick suggest: [:name] # fields to generate suggestions
end

Reindex and search with:

products = Product.search("peantu butta", suggest: true)
products.suggestions # ["peanut butter"]

Aggregations

Aggregations provide aggregated search data.

Aggregations

products = Product.search("chuck taylor", aggs: [:product_type, :gender, :brand])
products.aggs

By default, where conditions apply to aggregations.

Product.search("wingtips", where: {color: "brandy"}, aggs: [:size])
# aggregations for brandy wingtips are returned

Change this with:

Product.search("wingtips", where: {color: "brandy"}, aggs: [:size], smart_aggs: false)
# aggregations for all wingtips are returned

Set where conditions for each aggregation separately with:

Product.search("wingtips", aggs: {size: {where: {color: "brandy"}}})

Limit

Product.search("apples", aggs: {store_id: {limit: 10}})

Order

Product.search("wingtips", aggs: {color: {order: {"_key" => "asc"}}}) # alphabetically

All of these options are supported

Ranges

price_ranges = [{to: 20}, {from: 20, to: 50}, {from: 50}]
Product.search("*", aggs: {price: {ranges: price_ranges}})

Minimum document count

Product.search("apples", aggs: {store_id: {min_doc_count: 2}})

Script support

Product.search("*", aggs: {color: {script: {source: "'Color: ' + _value"}}})

Date histogram

Product.search("pear", aggs: {products_per_year: {date_histogram: {field: :created_at, interval: :year}}})

For other aggregation types, including sub-aggregations, use body_options:

Product.search("orange", body_options: {aggs: {price: {histogram: {field: :price, interval: 10}}}})

Highlight

Specify which fields to index with highlighting.

class Band < ApplicationRecord
  searchkick highlight: [:name]
end

Highlight the search query in the results.

bands = Band.search("cinema", highlight: true)

View the highlighted fields with:

bands.with_highlights.each do |band, highlights|
  highlights[:name] # "Two Door <em>Cinema</em> Club"
end

To change the tag, use:

Band.search("cinema", highlight: {tag: "<strong>"})

To highlight and search different fields, use:

Band.search("cinema", fields: [:name], highlight: {fields: [:description]})

By default, the entire field is highlighted. To get small snippets instead, use:

bands = Band.search("cinema", highlight: {fragment_size: 20})
bands.with_highlights(multiple: true).each do |band, highlights|
  highlights[:name].join(" and ")
end

Additional options can be specified for each field:

Band.search("cinema", fields: [:name], highlight: {fields: {name: {fragment_size: 200}}})

You can find available highlight options in the Elasticsearch reference.

Similar Items

Find similar items.

product = Product.first
product.similar(fields: [:name], where: {size: "12 oz"})

Geospatial Searches

class Restaurant < ApplicationRecord
  searchkick locations: [:location]

  def search_data
    attributes.merge(location: {lat: latitude, lon: longitude})
  end
end

Reindex and search with:

Restaurant.search("pizza", where: {location: {near: {lat: 37, lon: -114}, within: "100mi"}}) # or 160km

Bounded by a box

Restaurant.search("sushi", where: {location: {top_left: {lat: 38, lon: -123}, bottom_right: {lat: 37, lon: -122}}})

Note: top_right and bottom_left also work

Bounded by a polygon

Restaurant.search("dessert", where: {location: {geo_polygon: {points: [{lat: 38, lon: -123}, {lat: 39, lon: -123}, {lat: 37, lon: 122}]}}})

Boost By Distance

Boost results by distance - closer results are boosted more

Restaurant.search("noodles", boost_by_distance: {location: {origin: {lat: 37, lon: -122}}})

Also supports additional options

Restaurant.search("wings", boost_by_distance: {location: {origin: {lat: 37, lon: -122}, function: "linear", scale: "30mi", decay: 0.5}})

Geo Shapes

You can also index and search geo shapes.

class Restaurant < ApplicationRecord
  searchkick geo_shape: [:bounds]

  def search_data
    attributes.merge(
      bounds: {
        type: "envelope",
        coordinates: [{lat: 4, lon: 1}, {lat: 2, lon: 3}]
      }
    )
  end
end

See the Elasticsearch documentation for details.

Find shapes intersecting with the query shape

Restaurant.search("soup", where: {bounds: {geo_shape: {type: "polygon", coordinates: [[{lat: 38, lon: -123}, ...]]}}})

Falling entirely within the query shape

Restaurant.search("salad", where: {bounds: {geo_shape: {type: "circle", relation: "within", coordinates: {lat: 38, lon: -123}, radius: "1km"}}})

Not touching the query shape

Restaurant.search("burger", where: {bounds: {geo_shape: {type: "envelope", relation: "disjoint", coordinates: [{lat: 38, lon: -123}, {lat: 37, lon: -122}]}}})

Inheritance

Searchkick supports single table inheritance.

class Dog < Animal
end

In your parent model, set:

class Animal < ApplicationRecord
  searchkick inheritance: true
end

The parent and child model can both reindex.

Animal.reindex
Dog.reindex # equivalent, all animals reindexed

And to search, use:

Animal.search("*")                   # all animals
Dog.search("*")                      # just dogs
Animal.search("*", type: [Dog, Cat]) # just cats and dogs

Notes:

  1. The suggest option retrieves suggestions from the parent at the moment.

    Dog.search("airbudd", suggest: true) # suggestions for all animals
  2. This relies on a type field that is automatically added to the indexed document. Be wary of defining your own type field in search_data, as it will take precedence.

Debugging Queries

To help with debugging queries, you can use:

Product.search("soap", debug: true)

This prints useful info to stdout.

See how the search server scores your queries with:

Product.search("soap", explain: true).response

See how the search server tokenizes your queries with:

Product.search_index.tokens("Dish Washer Soap", analyzer: "searchkick_index")
# ["dish", "dishwash", "washer", "washersoap", "soap"]

Product.search_index.tokens("dishwasher soap", analyzer: "searchkick_search")
# ["dishwashersoap"] - no match

Product.search_index.tokens("dishwasher soap", analyzer: "searchkick_search2")
# ["dishwash", "soap"] - match!!

Partial matches

Product.search_index.tokens("San Diego", analyzer: "searchkick_word_start_index")
# ["s", "sa", "san", "d", "di", "die", "dieg", "diego"]

Product.search_index.tokens("dieg", analyzer: "searchkick_word_search")
# ["dieg"] - match!!

See the complete list of analyzers.

Testing

As you iterate on your search, it’s a good idea to add tests.

For performance, only enable Searchkick callbacks for the tests that need it.

Parallel Tests

Rails 6 enables parallel tests by default. Add to your test/test_helper.rb:

class ActiveSupport::TestCase
  parallelize_setup do |worker|
    Searchkick.index_suffix = worker

    # reindex models
    Product.reindex

    # and disable callbacks
    Searchkick.disable_callbacks
  end
end

And use:

class ProductTest < ActiveSupport::TestCase
  def setup
    Searchkick.enable_callbacks
  end

  def teardown
    Searchkick.disable_callbacks
  end

  def test_search
    Product.create!(name: "Apple")
    Product.search_index.refresh
    assert_equal ["Apple"], Product.search("apple").map(&:name)
  end
end

Minitest

Add to your test/test_helper.rb:

# reindex models
Product.reindex

# and disable callbacks
Searchkick.disable_callbacks

And use:

class ProductTest < Minitest::Test
  def setup
    Searchkick.enable_callbacks
  end

  def teardown
    Searchkick.disable_callbacks
  end

  def test_search
    Product.create!(name: "Apple")
    Product.search_index.refresh
    assert_equal ["Apple"], Product.search("apple").map(&:name)
  end
end

RSpec

Add to your spec/spec_helper.rb:

RSpec.configure do |config|
  config.before(:suite) do
    # reindex models
    Product.reindex

    # and disable callbacks
    Searchkick.disable_callbacks
  end

  config.around(:each, search: true) do |example|
    Searchkick.callbacks(nil) do
      example.run
    end
  end
end

And use:

describe Product, search: true do
  it "searches" do
    Product.create!(name: "Apple")
    Product.search_index.refresh
    assert_equal ["Apple"], Product.search("apple").map(&:name)
  end
end

Factory Bot

Use a trait and an after create hook for each indexed model:

FactoryBot.define do
  factory :product do
    # ...

    # Note: This should be the last trait in the list so `reindex` is called
    # after all the other callbacks complete.
    trait :reindex do
      after(:create) do |product, _evaluator|
        product.reindex(refresh: true)
      end
    end
  end
end

# use it
FactoryBot.create(:product, :some_trait, :reindex, some_attribute: "foo")

GitHub Actions

Check out setup-elasticsearch for an easy way to install Elasticsearch:

    - uses: ankane/setup-elasticsearch@v1

And setup-opensearch for an easy way to install OpenSearch:

    - uses: ankane/setup-opensearch@v1

Deployment

For the search server, Searchkick uses ENV["ELASTICSEARCH_URL"] for Elasticsearch and ENV["OPENSEARCH_URL"] for OpenSearch. This defaults to http://localhost:9200.

Elastic Cloud

Create an initializer config/initializers/elasticsearch.rb with:

ENV["ELASTICSEARCH_URL"] = "https://user:password@host:port"

Then deploy and reindex:

rake searchkick:reindex:all

Heroku

Choose an add-on: Bonsai, SearchBox, or Elastic Cloud.

For Elasticsearch on Bonsai:

heroku addons:create bonsai
heroku config:set ELASTICSEARCH_URL=`heroku config:get BONSAI_URL`

For OpenSearch on Bonsai:

heroku addons:create bonsai --engine=opensearch
heroku config:set OPENSEARCH_URL=`heroku config:get BONSAI_URL`

For SearchBox:

heroku addons:create searchbox:starter
heroku config:set ELASTICSEARCH_URL=`heroku config:get SEARCHBOX_URL`

For Elastic Cloud (previously Found):

heroku addons:create foundelasticsearch
heroku addons:open foundelasticsearch

Visit the Shield page and reset your password. You’ll need to add the username and password to your url. Get the existing url with:

heroku config:get FOUNDELASTICSEARCH_URL

And add elastic:password@ right after https:// and add port 9243 at the end:

heroku config:set ELASTICSEARCH_URL=https://elastic:[email protected]:9243

Then deploy and reindex:

heroku run rake searchkick:reindex:all

Amazon OpenSearch Service

Create an initializer config/initializers/opensearch.rb with:

ENV["OPENSEARCH_URL"] = "https://es-domain-1234.us-east-1.es.amazonaws.com:443"

To use signed requests, include in your Gemfile:

gem "faraday_middleware-aws-sigv4"

and add to your initializer:

Searchkick.aws_credentials = {
  access_key_id: ENV["AWS_ACCESS_KEY_ID"],
  secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
  region: "us-east-1"
}

Then deploy and reindex:

rake searchkick:reindex:all

Self-Hosted and Other

Create an initializer with:

ENV["ELASTICSEARCH_URL"] = "https://user:password@host:port"
# or
ENV["OPENSEARCH_URL"] = "https://user:password@host:port"

Then deploy and reindex:

rake searchkick:reindex:all

Data Protection

We recommend encrypting data at rest and in transit (even inside your own network). This is especially important if you send personal data of your users to the search server.

Bonsai, Elastic Cloud, and Amazon OpenSearch Service all support encryption at rest and HTTPS.

Automatic Failover

Create an initializer with multiple hosts:

ENV["ELASTICSEARCH_URL"] = "https://user:password@host1,https://user:password@host2"
# or
ENV["OPENSEARCH_URL"] = "https://user:password@host1,https://user:password@host2"

Client Options

Create an initializer with:

Searchkick.client_options[:reload_connections] = true

See the docs for Elasticsearch or Opensearch for a complete list of options.

Lograge

Add the following to config/environments/production.rb:

config.lograge.custom_options = lambda do |event|
  options = {}
  options[:search] = event.payload[:searchkick_runtime] if event.payload[:searchkick_runtime].to_f > 0
  options
end

See Production Rails for other good practices.

Performance

JSON Generation

Significantly increase performance with faster JSON generation. Add Oj to your Gemfile.

gem "oj"

This speeds up all JSON generation and parsing in your application (automatically!)

Persistent HTTP Connections

Significantly increase performance with persistent HTTP connections. Add Typhoeus to your Gemfile and it’ll automatically be used.

gem "typhoeus"

To reduce log noise, create an initializer with:

Ethon.logger = Logger.new(nil)

If you run into issues on Windows, check out this post.

Searchable Fields

By default, all string fields are searchable (can be used in fields option). Speed up indexing and reduce index size by only making some fields searchable.

class Product < ApplicationRecord
  searchkick searchable: [:name]
end

Filterable Fields

By default, all string fields are filterable (can be used in where option). Speed up indexing and reduce index size by only making some fields filterable.

class Product < ApplicationRecord
  searchkick filterable: [:brand]
end

Note: Non-string fields are always filterable and should not be passed to this option.

Parallel Reindexing

For large data sets, you can use background jobs to parallelize reindexing.

Product.reindex(mode: :async)
# {index_name: "products_production_20170111210018065"}

Once the jobs complete, promote the new index with:

Product.search_index.promote(index_name)

You can optionally track the status with Redis:

Searchkick.redis = Redis.new

And use:

Searchkick.reindex_status(index_name)

You can also have Searchkick wait for reindexing to complete

Product.reindex(mode: :async, wait: true)

You can use ActiveJob::TrafficControl to control concurrency. Install the gem:

gem "activejob-traffic_control", ">= 0.1.3"

And create an initializer with:

ActiveJob::TrafficControl.client = Searchkick.redis

class Searchkick::BulkReindexJob
  concurrency 3
end

This will allow only 3 jobs to run at once.

Refresh Interval

You can specify a longer refresh interval while reindexing to increase performance.

Product.reindex(mode: :async, refresh_interval: "30s")

Note: This only makes a noticable difference with parallel reindexing.

When promoting, have it restored to the value in your mapping (defaults to 1s).

Product.search_index.promote(index_name, update_refresh_interval: true)

Queuing

Push ids of records needing reindexing to a queue and reindex in bulk for better performance. First, set up Redis in an initializer. We recommend using connection_pool.

Searchkick.redis = ConnectionPool.new { Redis.new }

And ask your models to queue updates.

class Product < ApplicationRecord
  searchkick callbacks: :queue
end

Then, set up a background job to run.

Searchkick::ProcessQueueJob.perform_later(class_name: "Product")

You can check the queue length with:

Product.search_index.reindex_queue.length

For more tips, check out Keeping Elasticsearch in Sync.

Routing

Searchkick supports routing, which can significantly speed up searches.

class Business < ApplicationRecord
  searchkick routing: true

  def search_routing
    city_id
  end
end

Reindex and search with:

Business.search("ice cream", routing: params[:city_id])

Partial Reindexing

Reindex a subset of attributes to reduce time spent generating search data and cut down on network traffic.

class Product < ApplicationRecord
  def search_data
    {
      name: name,
      category: category
    }.merge(prices_data)
  end

  def prices_data
    {
      price: price,
      sale_price: sale_price
    }
  end
end

And use:

Product.reindex(:prices_data)

Advanced

Searchkick makes it easy to use the Elasticsearch or OpenSearch DSL on its own.

Advanced Mapping

Create a custom mapping:

class Product < ApplicationRecord
  searchkick mappings: {
    properties: {
      name: {type: "keyword"}
    }
  }
end

Note: If you use a custom mapping, you'll need to use custom searching as well.

To keep the mappings and settings generated by Searchkick, use:

class Product < ApplicationRecord
  searchkick merge_mappings: true, mappings: {...}
end

Advanced Search

And use the body option to search:

products = Product.search(body: {query: {match: {name: "milk"}}})

View the response with:

products.response

To modify the query generated by Searchkick, use:

products = Product.search("milk", body_options: {min_score: 1})

or

products =
  Product.search("apples") do |body|
    body[:min_score] = 1
  end

Client

To access the Elasticsearch::Client or OpenSearch::Client directly, use:

Searchkick.client

Multi Search

To batch search requests for performance, use:

products = Product.search("snacks")
coupons = Coupon.search("snacks")
Searchkick.multi_search([products, coupons])

Then use products and coupons as typical results.

Note: Errors are not raised as with single requests. Use the error method on each query to check for errors.

Multiple Models

Search across multiple models with:

Searchkick.search("milk", models: [Product, Category])

Boost specific models with:

indices_boost: {Category => 2, Product => 1}

Multi-Tenancy

Check out this great post on the Apartment gem. Follow a similar pattern if you use another gem.

Scroll API

Searchkick also supports the scroll API. Scrolling is not intended for real time user requests, but rather for processing large amounts of data.

Product.search("*", scroll: "1m").scroll do |batch|
  # process batch ...
end

You can also scroll batches manually.

products = Product.search("*", scroll: "1m")
while products.any?
  # process batch ...

  products = products.scroll
end

products.clear_scroll

Deep Paging

By default, Elasticsearch and OpenSearch limit paging to the first 10,000 results. Here’s why. We don’t recommend changing this, but if you really need all results, you can use:

class Product < ApplicationRecord
  searchkick deep_paging: true
end

If you just need an accurate total count, you can instead use:

Product.search("pears", body_options: {track_total_hits: true})

Nested Data

To query nested data, use dot notation.

Product.search("san", fields: ["store.city"], where: {"store.zip_code" => 12345})

Nearest Neighbors

You can use custom mapping and searching to index vectors and perform k-nearest neighbor search. See the examples for Elasticsearch and OpenSearch.

Reference

Reindex one record

product = Product.find(1)
product.reindex

Reindex multiple records

Product.where(store_id: 1).reindex

Reindex associations

store.products.reindex

Remove old indices

Product.search_index.clean_indices

Use custom settings

class Product < ApplicationRecord
  searchkick settings: {number_of_shards: 3}
end

Use a different index name

class Product < ApplicationRecord
  searchkick index_name: "products_v2"
end

Use a dynamic index name

class Product < ApplicationRecord
  searchkick index_name: -> { "#{name.tableize}-#{I18n.locale}" }
end

Prefix the index name

class Product < ApplicationRecord
  searchkick index_prefix: "datakick"
end

For all models

Searchkick.index_prefix = "datakick"

Use a different term for boosting by conversions

Product.search("banana", conversions_term: "organic banana")

Multiple conversion fields

class Product < ApplicationRecord
  has_many :searches, class_name: "Searchjoy::Search"

  # searchkick also supports multiple "conversions" fields
  searchkick conversions: ["unique_user_conversions", "total_conversions"]

  def search_data
    {
      name: name,
      unique_user_conversions: searches.group(:query).distinct.count(:user_id),
      # {"ice cream" => 234, "chocolate" => 67, "cream" => 2}
      total_conversions: searches.group(:query).count
      # {"ice cream" => 412, "chocolate" => 117, "cream" => 6}
    }
  end
end

and during query time:

Product.search("banana") # boost by both fields (default)
Product.search("banana", conversions: "total_conversions") # only boost by total_conversions
Product.search("banana", conversions: false) # no conversion boosting

Change timeout

Searchkick.timeout = 15 # defaults to 10

Set a lower timeout for searches

Searchkick.search_timeout = 3

Change the search method name

Searchkick.search_method_name = :lookup

Change search queue name

Searchkick.queue_name = :search_reindex

Eager load associations

Product.search("milk", includes: [:brand, :stores])

Eager load different associations by model

Searchkick.search("*",  models: [Product, Store], model_includes: {Product => [:store], Store => [:product]})

Run additional scopes on results

Product.search("milk", scope_results: ->(r) { r.with_attached_images })

Specify default fields to search

class Product < ApplicationRecord
  searchkick default_fields: [:name]
end

Turn off special characters

class Product < ApplicationRecord
  # A will not match Ä
  searchkick special_characters: false
end

Turn on stemming for conversions

class Product < ApplicationRecord
  searchkick stem_conversions: true
end

Make search case-sensitive

class Product < ApplicationRecord
  searchkick case_sensitive: true
end

Note: If misspellings are enabled (default), results with a single character case difference will match. Turn off misspellings if this is not desired.

Change import batch size

class Product < ApplicationRecord
  searchkick batch_size: 200 # defaults to 1000
end

Create index without importing

Product.reindex(import: false)

Use a different id

class Product < ApplicationRecord
  def search_document_id
    custom_id
  end
end

Add request parameters like search_type

Product.search("carrots", request_params: {search_type: "dfs_query_then_fetch"})

Set options across all models

Searchkick.model_options = {
  batch_size: 200
}

Reindex conditionally

class Product < ApplicationRecord
  searchkick callbacks: false

  # add the callbacks manually
  after_commit :reindex, if: -> (model) { model.previous_changes.key?("name") } # use your own condition
end

Reindex all models - Rails only

rake searchkick:reindex:all

Turn on misspellings after a certain number of characters

Product.search("api", misspellings: {prefix_length: 2}) # api, apt, no ahi

Note: With this option, if the query length is the same as prefix_length, misspellings are turned off with Elasticsearch 7 and OpenSearch 1

Product.search("ah", misspellings: {prefix_length: 2}) # ah, no aha

BigDecimal values are indexed as floats by default so they can be used for boosting. Convert them to strings to keep full precision.

class Product < ApplicationRecord
  def search_data
    {
      units: units.to_s("F")
    }
  end
end

Gotchas

Consistency

Elasticsearch and OpenSearch are eventually consistent, meaning it can take up to a second for a change to reflect in search. You can use the refresh method to have it show up immediately.

product.save!
Product.search_index.refresh

Inconsistent Scores

Due to the distributed nature of Elasticsearch and OpenSearch, you can get incorrect results when the number of documents in the index is low. You can read more about it here. To fix this, do:

class Product < ApplicationRecord
  searchkick settings: {number_of_shards: 1}
end

For convenience, this is set by default in the test environment.

Upgrading

5.0

Searchkick 5 supports both the elasticsearch and opensearch-ruby gems. Add the one you want to use to your Gemfile:

gem "elasticsearch"
# or
gem "opensearch-ruby"

If using the deprecated faraday_middleware-aws-signers-v4 gem, switch to faraday_middleware-aws-sigv4.

Also, searches now use lazy loading:

# search not executed
Product.search("milk")

# search executed
Product.search("milk").to_a

You can reindex relations in the background:

store.products.reindex(mode: :async)
# or
store.products.reindex(mode: :queue)

And there’s a new option for models with default scopes.

Check out the changelog for the full list of changes.

History

View the changelog.

Thanks

Thanks to Karel Minarik for Elasticsearch Ruby and Tire, Jaroslav Kalistsuk for zero downtime reindexing, and Alex Leschenko for Elasticsearch autocomplete.

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/ankane/searchkick.git
cd searchkick
bundle install
bundle exec rake test

Feel free to open an issue to get feedback on your idea before spending too much time on it.

searchkick's People

Contributors

abookyun avatar acallaghan avatar ankane avatar armilam avatar aud-tree avatar bdelmas avatar coryodaniel avatar danielwestendorf avatar feliksg avatar gotar avatar jeremiahchurch avatar jhdavids8 avatar johnnykei avatar jordanderson avatar jtomaszewski avatar kahirul avatar klebervirgilio avatar meetrajesh avatar mehulkar avatar monkbroc avatar nragaz avatar nviennot avatar readmecritic avatar richardking avatar samuel02 avatar sharshenov avatar subvertallchris avatar titanmaru avatar will-r avatar zapnap avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

searchkick's Issues

Controlling the behavior of special characters

Is it possible to change the behavior of the handling of the special characters. For example I do not wan't search for 'ÅÄÖ' to be equivalent to the search 'AAO'.

The example in the documentation(jalapeño/jalapeno) is valid but A and Ä are very different letters and they have their own button on the swedish keyboard located on the opposite sides.

Otherwise I'm really enjoying the gem. Using it in production as we speak.

shardFailure

Hey, sorry to keep opening issues without first addressing your replies on the other ones, but we keep running into stuff in different places. Extremely appreciative of your quick replies!

I'm currently trying to set up elasticsearch in its own vm and point app to it. I've reindexed and everything, but when I run a search, I get this stack trace:

https://gist.github.com/mehulkar/1343abf27ea947590173

I've been googling around for what causes shardFailures, but I don't know what is causing the Parse Failure.

Slow importing

Hi! I'm testing searchkick and it's fantastic, but I have a major issue: reindex is very slow.

I have about 3 million records, it takes 9 hours on my dev machine (3ghz dual-core with 8gb ram) and takes up 6gb of ram…that's a bit too much I think.

Is this a known issue in Searchkick or Tire? I have seen that in Tire the GC is called manually to overcome this, but doesn't work for me.

Has anyone imported such large numbers using Searchkick and/or Tire? Maybe i'm missing something?

I'm using:

  • ruby 2.0-p247
  • rails 4.0.0
  • searchkick 0.2.4
  • tire 0.6.0
  • tire-contrib 0.1.3
  • elasticsearch 0.90.3

Autocomplete on search queries, not just fields

The existing autocomplete works great for things like cities.

City.search "san fr", fields: [:name], autocomplete: true

However, for products, you sometimes want to autocomplete based on what others have searched, not the name of the product. For example, people may be searching peanut butter but there is no specific product named just peanut butter.

Search not working in production environment

Hi

I'm having trouble with my production environment. Everything works nicely on the various development environments, and oddly enough search seems to work fine from the rails console on the production server. But when you hit the search page (which works fine in production) production.log spits out this:
https://gist.github.com/johanhalse/6351288

Contrast with doing the same search from the rails console using bin/rails c production:
https://gist.github.com/johanhalse/6351304

I've cleared all the elasticsearch indexes in production and then ran Article.reindex from the production console. Any idea why this is happening? The production server is Ubuntu 13.04 running elasticsearch 0.90.3 and everything else is working well.

Error on simple search

Hi,
I am getting an error on simple search. The error is
"nested: IllegalArgumentException[minimumSimilarity >= 1];"
One of the things I am doing differently is, I already have an elasticsearch database and I don't have any other database so that's the primary data source. That's the reason I cannot called the "reindex" function on it. So I tried to manually add all the settings "searchkick" adds to the elasticsearch instance. And now in my settings I can see all the analyzers but still I am getting this error. Is there something else that needs to be added?

Duplicate results and memory usage

Hi.

I've run into two problems and couldn't solve them, so I'm hoping you could help me:

1 - I'm getting duplicate results for (from what I can see) every search, meaning that if I do a Model.search("") it will return an array with repeated results (duplicate objects with the same id). I've tried Model.reindex, Model.tire.index.refresh, Model.clean_indices and nothing seems to solve this issue. The best I can tell for now is that I have two indices for the same model (), created on different dates. How did this happen, and how can I solve this?

2 - How much memory should elasticsearch be consuming? I'm indexing a table with something like 300 lines by 7 attributes and it's already consuming 400MB, I'm afraid that this could easily blow up when the application starts to get heavy usage.

Thank you for any help regarding these issues, and also for the awesome gem.

Best regards.

Custom results for each user

Add the ability to boost results differently for each user based on application-specific logic.

For example, a music application may want to show the bands a user follows before other results that match the search query.

Search by contactenating fields?

I'm searching a denormalized table where I have four separate columns for names:

surname

first_name

other_name

nickname

I want to search using a term "Bob Dylan" that will return a record whose first name is Bob and surname is Dylan. If I search using:

Person.search("Bob Dylan")

Then I get my result. But I can't do that because I need to exclude the nickname column from results (let's assume 500 people have Bob Dylan as their nickname and those are irrelevant). I've tried:

Person.search("Bob Dylan", fields: [:first_name, :surname, :other_name])

This returns nil.

Alternatively, is there an option like:

Person.search("Bob Dylan", exclude_fields: [:nickname])

Thanks

Improve logging

Log all requests to Elasticsearch

Show actual search time returned by Elasticsearch and latency

Facets not working in Elasticsearch 0.90.5??

Hi,

Not sure if this is a Searchkick or Tire issue, but facets appear to be broken with the newest Elasticsearch version (0.90.5). I saw the issue in my own project, so I forked Searchkick and ran the test suite against 0.90.5. The test_limit and test_basic tests fail with the errors below:

  1) Error:
TestFacets#test_limit:
Tire::Search::SearchRequestFailed: 400 : {"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[38pDNhusSCKtSsam76V0ow][products_test_20130929202833285][0]: SearchParseException[[products_test_20130929202833285][0]: query[+(_all:product^10.0 | _all:product^10.0 | _all:product~1 | _all:product~1) ToParentBlockJoinQuery (filtered(function score (conversions.query:product,function=script[doc['count'].value], params [null]))->cache(_type:__conversions))],from[0],size[100000]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":{\"dis_max\":{\"queries\":[{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"boost\":10,\"analyzer\":\"searchkick_search\"}},{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"boost\":10,\"analyzer\":\"searchkick_search2\"}},{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"fuzziness\":1,\"max_expansions\":3,\"analyzer\":\"searchkick_search\"}},{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"fuzziness\":1,\"max_expansions\":3,\"analyzer\":\"searchkick_search2\"}}]}},\"should\":{\"nested\":{\"path\":\"conversions\",\"score_mode\":\"total\",\"query\":{\"custom_score\":{\"query\":{\"match\":{\"query\":\"Product\"}},\"script\":\"doc['count'].value\"}}}}}},\"size\":100000,\"from\":0,\"facets\":{\"store_id\":{\"terms\":{\"field\":\"store_id\",\"size\":1},\"facet_filter\":{\"and\":{\"filters\":[]}}}},\"fields\":[]}]]]; nested: NullPointerException; }]","status":400}
    /Users/jhdavids8/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/search.rb:139:in `perform'
    /Users/jhdavids8/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/search.rb:43:in `json'
    /Users/jhdavids8/Personal/searchkick/lib/searchkick/search.rb:278:in `search'
    /Users/jhdavids8/Personal/searchkick/test/facets_test.rb:23:in `test_limit'

  2) Error:
TestFacets#test_basic:
Tire::Search::SearchRequestFailed: 400 : {"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[38pDNhusSCKtSsam76V0ow][products_test_20130929202833285][0]: SearchParseException[[products_test_20130929202833285][0]: query[+(_all:product^10.0 | _all:product^10.0 | _all:product~1 | _all:product~1) ToParentBlockJoinQuery (filtered(function score (conversions.query:product,function=script[doc['count'].value], params [null]))->cache(_type:__conversions))],from[0],size[100000]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":{\"dis_max\":{\"queries\":[{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"boost\":10,\"analyzer\":\"searchkick_search\"}},{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"boost\":10,\"analyzer\":\"searchkick_search2\"}},{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"fuzziness\":1,\"max_expansions\":3,\"analyzer\":\"searchkick_search\"}},{\"multi_match\":{\"fields\":[\"_all\"],\"query\":\"Product\",\"use_dis_max\":false,\"operator\":\"and\",\"fuzziness\":1,\"max_expansions\":3,\"analyzer\":\"searchkick_search2\"}}]}},\"should\":{\"nested\":{\"path\":\"conversions\",\"score_mode\":\"total\",\"query\":{\"custom_score\":{\"query\":{\"match\":{\"query\":\"Product\"}},\"script\":\"doc['count'].value\"}}}}}},\"size\":100000,\"from\":0,\"facets\":{\"store_id\":{\"terms\":{\"field\":\"store_id\",\"size\":100000},\"facet_filter\":{\"and\":{\"filters\":[]}}}},\"fields\":[]}]]]; nested: NullPointerException; }]","status":400}
    /Users/jhdavids8/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/search.rb:139:in `perform'
    /Users/jhdavids8/.rvm/gems/ruby-2.0.0-p247/gems/tire-0.6.0/lib/tire/search.rb:43:in `json'
    /Users/jhdavids8/Personal/searchkick/lib/searchkick/search.rb:278:in `search'
    /Users/jhdavids8/Personal/searchkick/test/facets_test.rb:15:in `test_basic'

All other tests pass, and everything is fine when run against 0.90.3.

NoMethodError: undefined method `[]' for nil:NilClass

searchkick seems to work for most of my models but on my product model I get this error trying to reindex. What is causing this?

Product.reindex
Product Load (0.6ms) SELECT "products".* FROM "products" ORDER BY "products"."id" ASC LIMIT 1000
NoMethodError: undefined method []' for nil:NilClass from /Users/test/.rvm/gems/ruby-1.9.3-p448/gems/searchkick-0.2.8/lib/searchkick/model.rb:36:into_indexed_json'

Working with abbreviations

Hi not an issue as such, I guess it could be a feature.

I know there is a way to implement synonyms but I was wondering if it would be possible to implement searchkick to somehow recognise abbreviations?

So if I have "Some Company Name" and I search for "SCN" it would bring that back as a result.

I know I could generate synonyms for them all but this seems a little messy and I think would be a great addition.

Autocomplete and search across multiple models

I want to perform autocomplete and search across multiple models.

One possible way that I can think of is querying different controller actions for each model and then merging the results - Twitter's typeahead.js @twitter supports this. The same should work for showing search results from different models.

I would love to know if there is a better way to do this currently and if not, any direction on how I should go about adding this capability to searchkick.

eager loading or chaining to search association

Here's my setup

class Organization
  belongs_to :university
end

class University
  has_many :organizations
end

I've been using this SQL query currently:

Organization.eager_load(:university).where("universities.state in (?) and lower(organizations.name) like ?", states, "%#{param}%")

and want to convert it to the searchkick syntax so I can search other fields than just organizations.name

I've tried chaining and eagerloading like this:

Chaining

orgs = Organization.eager_load(:university).where("state IN ?", states_array)
orgs.search(param)

The search for some reason doesn't chain on top of the results from the first query though. I'm getting results from the entire index.

Chaining the other way

orgs = Organization.search(param)
orgs.eager_load(:university).where("universities.state IN ?", states_array)
# or orgs.results.eager_load...

But arrays and tire collections can't use active record relation methods.

Eager loading

Organization.search(param, include: [:university], where: {"universities.state IN ?", states_array})

I'm not sure what the problem is here. Just the first part. Organization.search(param, include: [:university]) a Tire::Results::Collection, but when I try to run #count on it or any other method I can normally run, it says there is a SQL syntax error.

I'm looking through the source now, but if I'm doing something silly, let me know! Not sure if search_import applies here, but I messed around with that as well.

MongoDB Support

Hi there,

Thanks for the great gem!
Do you intend to support also MongoDB. Currently I'm using Mongoid but I was unable to migrate from Tire to this gem.

2 character search

ModelName.all.map(&:search_data)
=> [{:name=>"A1"}, {:name=>"A2"}]

ModelName.seach('A1').results
=> []

Change fixtures so that

ModelName.all.map(&:search_data)
=> [{:name=>"A1A"}, {:name=>"A2A"}]

ModelName.seach('A1A').results
=> [<ModelName id: 1, name: 'A1A'>]

Faceted search results

If I use a facet option in the search methods, I see the number of results in each facet, but I'm not seeing how to access those grouped results. Not sure if this is a missing feature or missing documentation?

Race condition for testing

context "GET /attachments/search" do
    it "matches attachment attributes with a query param" do
      Attachment.reindex
      query_param = "attachment2.png"
      get "/attachments/search?query=#{query_param}"
      # expectations ommitted
    end
  end

The test above results in a race condition causing the test to fail. I was able to make it consistently pass with a sleep 1, but obviously that is not a good solution.

I was also able to make it pass consistently by wrapping the whole test in a new Thread like so:

context "GET /attachments/search" do
    it "matches attachment attributes with a query param" do
      Thread.new do
        Attachment.reindex
        query_param = "attachment2.png"
        get "/attachments/search?query=#{query_param}"
        # expectations ommitted
      end
    end
  end

Any guidance/thoughts on the correct way to do this?

OR clause in filters with multiple values. Syntax is not clear.

It's not clear how to construct an OR query using searchkick whose logic is equivalent to
"where owner_id IN [1,2,3] OR commenter_id IN [1,2,3]"

I tried

Article.search("*",where:{
 or:[
     [{owner_id:[1,2,3]},{commenter_id:[1,2,3]}]
 ]
})

but the results are incorrect.
The filter being generated by searchkick is

:filter=>{:and=>[{:or=>[{:term=>{:owner_id=>[1,2,3]}}, {:term=>{:commenter_ids=>[1,2,3]}}]}]}, :fields=>[]}

The filter that is being generated doesn't look correct from what I understand from this question http://stackoverflow.com/questions/16703736/or-query-in-elastic-search

To get the correct results, I am having to separate each element in the OR clause, like the following -

Article.search("*",where:{
or:[
[{owner_id: 1},{owner_id:2},{owner_id:3},{commenter_id:1},{commenter_id:2},{commenter_id:3}]
]
})
Sorry, but it is not clear to me if this is a bug in how searchkick generates the filter or I am doing something wrong.

Parse error for simple queries

On Searchkick 0.1.4, ElasticSearch 0.90.2. I've followed the install instructions and indexed a very vanilla table. When I go to query it - in this case, Clip.search 'instagram', I receive this error. I'm getting it on every indexed table I try. Any insight?


{"error":"SearchPhaseExecutionException[Failed to execute phase [query_fetch], total failure; shardFailures {[O2QLohkFQnGqFGTGv0rPhg][clips_development_20130809094314][0]: SearchParseException[[clips_development_20130809094314][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{
  "query":{
    "bool":{
      "must":[
        {
          "custom_score":{
            "script":"_score",
            "query":{
              "dis_max":{
                "queries":[
                  {
                    "multi_match":{
                      "query":"instagram",
                      "boost":10,
                      "operator":"and",
                      "analyzer":"searchkick_search",
                      "fields":[
                        "_all"
                      ]
                    }
                  },
                  {
                    "multi_match":{
                      "query":"instagram",
                      "boost":10,
                      "operator":"and",
                      "analyzer":"searchkick_search2",
                      "fields":[
                        "_all"
                      ]
                    }
                  },
                  {
                    "multi_match":{
                      "query":"instagram",
                      "use_dis_max":false,
                      "fuzziness":1,
                      "max_expansions":1,
                      "operator":"and",
                      "analyzer":"searchkick_search",
                      "fields":[
                        "_all"
                      ]
                    }
                  },
                  {
                    "multi_match":{
                      "query":"instagram",
                      "use_dis_max":false,
                      "fuzziness":1,
                      "max_expansions":1,
                      "operator":"and",
                      "analyzer":"searchkick_search2",
                      "fields":[
                        "_all"
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "should":[
        {
          "nested":{
            "query":{
              "custom_score":{
                "script":"doc['count'].value",
                "query":{
                  "match":{
                    "query":{
                      "query":"instagram"
                    }
                  }
                }
              }
            },
            "path":"conversions",
            "score_mode":"total"
          }
        }
      ]
    }
  },
  "size":100000
}]]]; nested: IllegalArgumentException[minimumSimilarity >= 1]; }]","status":500}

Weird Error on Heroku

Not sure it's specific to the add-on I'm using "SearchBox ElasticSearch", but I received this error trying to create a new record that I'm indexing on:

MultiJson::LoadError: 795: unexpected token at 'One of the given indices does not belong to this user'

But, if anyone gets this error, the quick fix was running Product.reindex

Figured I'd put it here incase anyone tries googling the error. Took me a while to figure it out!

Attempt to reindex is index isn't found ( on ERR 2 (Connection refused))

Hey, I think that it would be a nice addition if when there is no index present when a search is called, it attempts to create one.

For example, Product.search() should call Product.reindex if the Product.search() fails due to there being no index present.

Which brings me to a question: When should I call Product.reindex? I feel like an initializer is a bad place, as that causes rake tasks to fail when the Product table isn't present (can't recreate the database with the initializer script present).

Thanks!

PS: This gem is actually amazing and I love you forever for it.

Autocomplete?

Is it possible to get results for partial word searches? This seems like the best option to incorporate with an Autocomplete Searchbar.

Thanks!

Found 1 result but was looking for x

I highly doubt this is an error but do you know why the below is happening to me.

class Project < ActiveRecord::Base
  searchkick
  ...
  def search_data
    {
      name: name,
      project_number: project_number.to_s
    }
  end
end
2.0.0p195 :005 > res = Project.search("10001")
  Search (9.1ms)  {"query":{"dis_max":{"queries":[{"multi_match":{"fields":["_all"],"query":"10001","use_dis_max":false,"operator":"and","boost":10,"analyzer":"searchkick_search"}},{"multi_match":{"fields":["_all"],"query":"10001","use_dis_max":false,"operator":"and","boost":10,"analyzer":"searchkick_search2"}},{"multi_match":{"fields":["_all"],"query":"10001","use_dis_max":false,"operator":"and","fuzziness":1,"max_expansions":3,"analyzer":"searchkick_search"}},{"multi_match":{"fields":["_all"],"query":"10001","use_dis_max":false,"operator":"and","fuzziness":1,"max_expansions":3,"analyzer":"searchkick_search2"}}]}},"size":100000,"from":0,"fields":[]}
 => #<Searchkick::Results:0x007f8578078de8 @response={"took"=>3, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>15, "max_score"=>0.06760579, "hits"=>[{"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"14", "_score"=>0.06760579}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"19", "_score"=>0.06760579}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"21", "_score"=>0.06760579}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"41", "_score"=>0.06657644}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"15", "_score"=>0.06657644}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"10", "_score"=>0.06657644}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"16", "_score"=>0.06374056}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"11", "_score"=>0.06374056}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"61", "_score"=>0.06374056}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"31", "_score"=>0.063121}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"12", "_score"=>0.063121}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"17", "_score"=>0.063121}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"13", "_score"=>0.062481344}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"18", "_score"=>0.062481344}, {"_index"=>"projects_development_20130910125551207", "_type"=>"project", "_id"=>"1", "_score"=>0.043368585}]}}, @options={:load=>true, :payload=>{:query=>{:dis_max=>{:queries=>[{:multi_match=>{:fields=>["_all"], :query=>"10001", :use_dis_max=>false, :operator=>"and", :boost=>10, :analyzer=>"searchkick_search"}}, {:multi_match=>{:fields=>["_all"], :query=>"10001", :use_dis_max=>false, :operator=>"and", :boost=>10, :analyzer=>"searchkick_search2"}}, {:multi_match=>{:fields=>["_all"], :query=>"10001", :use_dis_max=>false, :operator=>"and", :fuzziness=>1, :max_expansions=>3, :analyzer=>"searchkick_search"}}, {:multi_match=>{:fields=>["_all"], :query=>"10001", :use_dis_max=>false, :operator=>"and", :fuzziness=>1, :max_expansions=>3, :analyzer=>"searchkick_search2"}}]}}, :size=>100000, :from=>0, :fields=>[]}, :size=>100000, :from=>0, :term=>"10001"}, @time=3, @total=15, @facets=nil, @max_score=0.06760579, @wrapper=Tire::Results::Item> 

2.0.0p195 :008 > res.first
  Project Load (0.5ms)  SELECT `projects`.* FROM `projects` WHERE `projects`.`id` IN (14, 19, 21, 41, 15, 10, 16, 11, 61, 31, 12, 17, 13, 18, 1)
ActiveRecord::RecordNotFound: Couldn't find all Projects with IDs (14, 19, 21, 41, 15, 10, 16, 11, 61, 31, 12, 17, 13, 18, 1) (found 1 results, but was looking for 15)

For the record; 10001 is a project number, and there is actually only 1 project in the system.

I understand what it's failing on as it can't find projects with them ids, apart from 1 but not sure why it is returning them all.

I am using rails 4, ruby 2 and the git version of searchkick.

multi-index search

Searchkick should support multi-index search.

I have overcome this by using:

Model1.search("thequery", index_name: [Model1.index.name, Model2.index.name, Model3.index.name])

it works ok, but it's just wrong to have to do this.

Maybe the starting point can be #36?

not generating index urls correctly?

I'm attempting to use searchkick with the Bonsai addon on Heroku. I can call #search from the heroku console and it returns results as expected. But when I trigger the same request from the controller, I'm getting a [REQUEST FAILED] error. Here's a gist with more logs https://gist.github.com/nz/e6ae59e2dcfdd19f2d46

I see that the URL for the CURL isn't correctly interpolating the RACK_ENV variable. I have the config variable set in my heroku app.

I see the relevant place in model.rb for generating the urls here: https://github.com/ankane/searchkick/blob/master/lib/searchkick/model.rb#L14

I'm setting the ELASTICSEARCH_URL as the readme suggests

Here's the relevant code in my controller:

@organizations = Organization.filter_and_search_by_query(params[:q])

and the method being called:

def self.filter_and_search_by_query(query)
  states = query[:states] || []
  param = query[:param].downcase
  # if both states and param
  query_results = if states.present? && param.present?
    Organization.search(query[:param]).results.select do |result|
      result if query[:states].include? result.location
    end
  # if states but not param
  elsif states.present? && param.blank?
    Organization.eager_load(:university).where('universities.state IN (?)', states)
  # if param but not states
  elsif states.empty? && param.present?
    Organization.search(param).results
  elsif states.empty? && param.blank?
    []
  end
end

You can test it out in the production app here: collegedesis.com/directory. Try typing in "raas" into the searchfield.

Searching multiple specific fields in the same query

I need to be able to pair search terms with specific fields. For example

Person.search(where: {name: "Bob", location: "CA"})

The other solution would be to have some way of concatenating results so that we could do something like this:

Person.search("Bob", fields: [:name]).search("CA", fields: [:location])

The third way seems to be to perform two separate searches and attempt to concat/merge the two, but I don't see any methods for that to work. I could concat just the results from each search like this:

Person.search("Bob", fields: [:name]).results + Person.search("CA", fields: [:location]).results

But then I lose some other info.

Is there a good way to do this?

Problem with Single Table Inheritance models

Hi,

I'm attempting to use searchkick on a base model, which is extended by two other models using STI. Placing searchkick on the base model crashes searchkick for the child models. The problem seems to be around this line in searchkick/model.rb:

options = self.class.instance_variable_get("@searchkick_options")

I currently solve this by placing searchkick on the child models themselves, but it would be nice to place it on the base model, since the behavior is shared.

Geo location search

Hi again!

I wasn't able to find anything related to geo location search. Do you intend adding such features?

Autocomplete from beginning of the each token

Sorry for creating so much fuzz here but I'm really liking your library and only wan't it to be even become better. Just like you clarified in #41 autocomplete and partial does not work in conjunction. However this is a feature I would very much have and thats why I created a new issue for it.

Typing "2% Mi" should trigger "2% Milk" but so should the query "Mil" and "Milk 2". I'm currently splitting the token but that messes with multiword queries. How exactly would such an index look like and still all? Am I better of using tire directly?

Wildcard searches?

Is there any way to make wildcard searches work? Or alternately, universal searches? For example, I tried to do this to find all items created by a specific user:

Item.search '*', where: {
  user_id: user.id
}

But it returns no results. I also tried not even specifying a search term, but no luck there either. If I use a string like 'test' instead of '', then I get results. I looked through the code, and it seems as though when term='' it might cause issues with the code making parser selections, but I only took a quick glance. Any suggestions would be great.

Associated model properties in where clause?

Is there a way to specify a property in an associated model in a where clause? I don't see anything in the examples, and looking at the code I'm not sure it's possible. Are there plans for this? As a simple example, if I have a model with:

def search_data
    as_json only: [:title, :created_at],
        include: {
          user: { only: [:id] }
        }
end

And then I want to search for all of the items for a specific user, I'd try something like:

Item.search 'bob', where: {
    user.id: 123
}

Or maybe "user: {id: 123}" but that doesn't work either. Essentially it's a matter of nesting where clauses. Obviously the associated model fields could in theory be surfaced in the parent model somehow, but that gets very messy. I tried playing with facets to see if that could help, but no go.

Maybe I'm missing the solution (which would be great)? Thanks for any ideas.

inconsistent search results

In a test environment

ModelName.all.map(&:search_data)
=> [{:value=>"222"}, {:value=>"111"}]

ModelName.search('111').results
=> [<ModelName id: 909979931, value: "111", created_at: "2013-08-19 18:51:55", updated_at: "2013-08-19 18:51:55">]

ModelName.search('222').results
=> []

Not sure what's causing this. I tried reindexing multiple times.

option to disable misspellings

It would be nice to be able to disable misspellings. We're searching for codes that are small enough in length that we have lots of 1 character diffs. We don't want to treat these as misspellings.

I'm going to try and tackle this and make a PR maybe today, but wanted to file an issue to keep track of it! :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.