ElasticSearch - 查找与查询密切相关的文档

时间:2017-08-24 13:45:00

标签: ruby-on-rails elasticsearch

我使用ElasticSearch搜索产品数据库。我需要搜索以查找具有字段的文档,其中字段数据可能接近搜索项但不精确。例如,如果用户搜索型号" T9200"那么我想要一个型号为#9; T 9200"退回或反之亦然。据我所知,我需要某种过滤器来连接,但我还没能找到我相信会起作用的东西。有人可以提供任何参考或信息吗?我编写的所有代码都是用Ruby编写的,但json解释应该与ElasticSearch文档相同。

settings index: { number_of_shards: 1 },
    analysis: {
      filter: {
        synonym: {
          type: "synonym",
          ignore_case: true,
          synonyms: File.readlines(Rails.root / 'config' / 'analysis' / 'data' / 'synonyms.txt').map(&:chomp)
        }
    },
  analyzer: {
    synonym: {
      tokenizer: "whitespace",
      filter: ["synonym", "lowercase", "stop", "snowball"]
    }
  }
} do
  mappings dynamic: 'false' do
    indexes :name, analyzer: 'synonym'
    indexes :status, analyzer: 'english'
    #indexes :description, analyzer: 'english'
    indexes :part_number, analyzer: 'english'
    indexes :text, analyzer: 'english'
    indexes :normal_model, type: 'nested' do
      indexes :name, analyzer: 'synonym'
      indexes :number, analyzer: 'english'
      indexes :machine_type, analyzer: 'english'

      indexes :normal_brand, type: 'nested' do
        indexes :name, analyzer: 'synonym'
      end
    end
  end
end




 def as_indexed_json(options={})
    as_json(
        only: [:name, :description, :part_number, :url_key, :image, :price, :shipping, :warranty, :eta, :status, :sku],
        include: {
            normal_model: { only: [:name, :number, :machine_type],
                include: {
                    normal_brand: { only: :name}
                }
            }
        }
    )
 end



@products = Product.search(
           query: { 
             query_string: {
              #query: "*manual* AND status:\"Disabled\""
                #fields: ['name', 'normal_model.normal_brand.name', 'normal_model.name', '_all'],

             query: "#{params[:q]} AND status:\"Viewable On Website & Backend\""
                #query:  "*" + params[:q]+ "*"
             }
          }
          ).page(page).per(15)

由于

0 个答案:

没有答案
相关问题