弹性搜索同义词 - Rails

时间:2017-08-04 17:32:46

标签: ruby-on-rails ruby elasticsearch ruby-on-rails-5 elasticsearch-rails

我使用带有elasticsearch-rails gem的Ruby on Rails,我正在尝试使用同义词过滤器。我一直在关注这里发布的问题以获得指导(除了同义词部分,我的实现按预期工作):

https://github.com/elastic/elasticsearch-rails/issues/63

这是我的代码:

settings index: { number_of_shards: 1 },
    analysis: {
  filter: {
    synonym: {
      type: "synonym",
      ignore_case: true,
      synonyms:[
        "roller,wheel"
      ]
    }
  },
  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: 'english'
      indexes :number, analyzer: 'english'
      indexes :machine_type, analyzer: 'english'

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

以下是我在控制器操作中搜索的代码:

 @products = Product.search(
        query: { 
          query_string: {
           #query: "*manual* AND status:\"Disabled\""
           #fields: ["normal_model.name", "normal_brand.name"],
            query: "*#{params[:q]}* AND status:\"Viewable On Website & Backend\""

            #query:  "*" + params[:q]+ "*"
          }
        }
      )

我的记录名称字段设置为" wheel"但是当我搜索" roller"我得到0结果,没有错误。我希望用名称" wheel"来检索记录。在此刻。我还完全删除了索引并验证它已被删除并重新创建了我的索引以确保我不仅仅面对索引问题。我不确定此时该做什么。任何帮助,将不胜感激。

这里也是我的as_indexed_json方法

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

由于

更新

我还尝试将以下代码(在下面的答案中建议)添加到我的控制器搜索操作中。

fields: ['name', '_all'],
query: "#{params[:q]} AND status:\"Viewable On Website & Backend\""

我在搜索操作中将此代码替换为原始代码,但是当我搜索单词" roller"时,这仍然没有产生任何结果。我仍然能够搜索" wheel"并检索几个结果,但我没有运气与指定的同义词。

更新

这是包含单词" wheel"的文件之一。在产品名称字段中。

{
  "_index" : "products",
  "_type" : "product",
  "_id" : "288374",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "wheel",
    "description" : "This is the O.E.M. wheel for the Spirit CE800 Elliptical with a model number 800049.",
    "shipping" : null,
    "sku" : "58511",
    "eta" : "3 to 5 Business Days",
    "warranty" : "1 Year",
    "part_number" : "N/A",
    "url_key" : "spirit-ce800-elliptical-model-800049-lubricant",
    "price" : 19.99,
    "image" : "noimage-main_20837.jpg",
    "status" : "Viewable On Website & Backend",
    "normal_model" : {
      "name" : "CE800",
      "number" : "800049",
      "machine_type" : "Elliptical",
      "normal_brand" : {
        "name" : "Spirit"
      }
    }
  }
}

更新

这是我的产品映射

{
  "products" : {
    "mappings" : {
      "product" : {
        "dynamic" : "false",
        "properties" : {
          "name" : {
            "type" : "text",
            "analyzer" : "synonym"
          },
          "normal_model" : {
            "type" : "nested",
            "properties" : {
              "machine_type" : {
                "type" : "text",
                "analyzer" : "english"
              },
              "name" : {
                "type" : "text",
                "analyzer" : "english"
              },
              "normal_brand" : {
                "type" : "nested",
                "properties" : {
                  "name" : {
                    "type" : "text",
                    "analyzer" : "english"
                  }
                }
              },
              "number" : {
                "type" : "text",
                "analyzer" : "english"
              }
            }
          },
          "part_number" : {
            "type" : "text",
            "analyzer" : "english"
          },
          "status" : {
            "type" : "text",
            "analyzer" : "english"
          },
          "text" : {
            "type" : "text",
            "analyzer" : "english"
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

使用您的查询,您正在_all字段中搜索。 (query_string的默认行为)。

要使用同义词分析器,您还必须在名称字段中搜索。 像这样:

  @products = Product.search(
    query: { 
      query_string: {
       fields: ['name', '_all'],
       query: "#{params[:q]} AND status:\"Viewable On Website & Backend\""
      }
    }
  )

如果params[:q]包含“roller”,您将获得包含单词“wheel”的记录。