弹性搜索同义词 - Rails

Elastic Search Synonyms - Rails

我在 Rails 上使用 Ruby 和 elasticsearch-rails gem,我正在尝试使用同义词过滤器。我一直在关注此处发布的问题以获取指导(我的实现按预期工作,同义词部分除外):

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"
          }
        }
      }
    }
  }
}

您的查询是在 _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".

的记录