简体   繁体   中英

Rails, Tire, Elasticsearch: how to use synonyms?

I have no idea how to use synonyms/plural with Elasticsearch through Tire gem. Have I a synonyms file to download (an english one in enough)? Something to setup in ES regardless I use Tire or not?

class Story < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks
  attr_accessible :author, :content, :title

  mapping do
    indexes :id, :index => :not_analyzed
    indexes :author, :analyzer => 'keyword'
    indexes :title, :analyzer => 'snowball'
    indexes :content, :analyzer => 'snowball'
  end
end

class StoriesController < ApplicationController
  def index
    if params[:q].present?
      p = params
      @stories = Story.search(per_page: 30, page: params[:page], load: true) do
        query { string p[:q], default_operator: 'AND' }
      end
    end
  end
end

I found nothing in documentation...

Thanks!

i guess you mean the synonym-tokenfilter of elasticsearch: http://www.elasticsearch.org/guide/reference/index-modules/analysis/synonym-tokenfilter/

{
    "index" : {
        "analysis" : {
            "analyzer" : {
                "synonym" : {
                    "tokenizer" : "whitespace",
                    "filter" : ["synonym"]
                }
            },
            "filter" : {
                "synonym" : {
                    "type" : "synonym",
                    "synonyms_path" : "analysis/synonym.txt"
                }
            }
        }
    }
}

afaik in tire, this would go in the settings configuration:

  settings :analysis => {
             :filter => {
               :synonym  => {
                 "type"          => "synonym",
                 "synonyms_path" => Rails.root.join("config/analysis/synonym.txt").to_s
               }
             },
             :analyzer => {
               :synonym => {
                  "tokenizer"    => "lowercase",
                  "filter"       => ["synonym"],
                  "type"         => "custom" }
             }
           } do
    mapping { indexes :the_field, :type => 'string', :analyzer => "synonym" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM