elasticsearch,Ruby On Rails,elasticsearch" /> elasticsearch,Ruby On Rails,elasticsearch" />

Ruby on rails Elasticsearch、multi_匹配、ruby on rails

Ruby on rails Elasticsearch、multi_匹配、ruby on rails,ruby-on-rails,elasticsearch,Ruby On Rails,elasticsearch,我想使用ElasticSearch搜索我的产品集合 如果我有这样一个对象: { name: "Product Name", producer: "Best Producer"} mapping do indexes :name, type: :string, search_analyzer: :str_search_analyzer, index_analyzer: :str_index_analyzer indexes :producer, type: :string, search

我想使用ElasticSearch搜索我的产品集合

如果我有这样一个对象:

{ name: "Product Name", producer: "Best Producer"}
mapping do
  indexes :name, type: :string, search_analyzer: :str_search_analyzer, index_analyzer: :str_index_analyzer
  indexes :producer, type: :string, search_analyzer: :str_search_analyzer, index_analyzer: :str_index_analyzer
end
我想通过搜索以下内容来查找此对象:

  • 产品
  • 贝斯
  • 专业的
  • 名字
  • 制作人
  • 最佳产品
  • 最佳越南酒店
  • 等等

    此代码运行良好,但例如“name”不是前缀:

    Product.search(
      {
        multi_match: {
          query: params[:search_input],
          type: "phrase_prefix",
          fields: ["name", "producer"],
          operator: "and"
        }
      })
    

    如何改进它?

    您可以使用bool、should和term来表示多重匹配参数

           Product.search(:query => @query)
    
           @query = {
              :bool => {
                  :must => @must_queries
              }
           }
    
           :bool => {
                :should => [
                    {
                        :terms => {
                            :your_mathcing_field_name => value
                        }
                    }
                ]
            }
    

    您可以为每个字段输入多个术语。

    谢谢您的回答

    我设法解决了这个问题。 这是我的解决方案:

    Product.search(
      {
        multi_match: {
          query: params[:search_input],
          type: "best_fields",
          fields: ["name", "producer"],
          operator: "and"
        }
      }
    )
    
    我的映射如下所示:

    { name: "Product Name", producer: "Best Producer"}
    
    mapping do
      indexes :name, type: :string, search_analyzer: :str_search_analyzer, index_analyzer: :str_index_analyzer
      indexes :producer, type: :string, search_analyzer: :str_search_analyzer, index_analyzer: :str_index_analyzer
    end
    

    你能展示你的地图吗?用这样的方法得到它: