1

我正在尝试将搜索查询与两个字段进行匹配,如果从页面上的下拉列表中选择,则按构面进行过滤。

当用户输入关键字时,如果在两个数据库字段中找到它应该匹配:标题和描述。下拉列表按状态和类型过滤。

这是我的轮胎搜索配置:

  def self.search(params)
      tire.search(load: true, page: params[:page], per_page: 25) do
        query do
          boolean do
            should { string "title:#{params[:query]}", default_operator: "OR" } if params[:query].present?
            should { string "description:#{params[:query]}", default_operator: "OR" } if params[:query].present?
            must { term :status_id, params[:status_id] } if params[:status_id].present?
            must { term :type_id, params[:type_id] } if params[:type_id].present?
          end
        end
        sort { by :updated_at, "desc" } if params[:query].blank?
        facet "status" do
          terms :status_id
        end
        facet "type" do
          terms :type_id
        end
      end
  end

索引设置:

  settings :analysis => {
      :filter => {
          :my_ngram => {
              "type" => "nGram",
              "max_gram" => 10,
              "min_gram" => 3}
      },
      :analyzer => {
          :my_analyzer => {
              "type" => "custom",
              "tokenizer" => "lowercase",
              "filter" => ["my_ngram"]
          }
      }
  } do
    mapping do
      indexes :title, boost: 10, analyzer: 'my_analyzer'
      indexes :description, boost: 5, analyzer: 'my_analyzer'
      indexes :status_id, :type => 'integer'
      indexes :type_id, :type => 'integer'
    end
  end

我最初只有标题和描述字段,工作正常。我现在正在尝试添加按状态和类型进行过滤的功能。

配置它的正确方法是什么?如果选择了状态,它应该只返回具有该状态的记录。类型相同,如果两者都被选中。

任何帮助表示赞赏。

不是错误发生,而是结果不再按关键字或方面过滤:

curl -X GET 'http://localhost:9200/projects/project/_search?load=true&size=25&pretty' -d '{"query":{"bool":{"should":[{"query_string":{"query":"title:test","default_operator":"OR"}},{"query_string":{"query":"description:test","default_operator":"OR"}}],"must":[{"term":{"status_id":{"term":"1"}}},{"term":{"type_id":{"term":"1"}}}]}},"facets":{"status":{"terms":{"field":"status_id","size":10,"all_terms":false}},"type":{"terms":{"field":"type_id","size":10,"all_terms":false}}},"size":25}'

# 2013-08-16 12:08:34:791 [200] (31 msec)
#
# {"took":31,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]},"facets":{"status":{"_type":"terms","missing":0,"total":0,"other":0,"terms":[]},"type":{"_type":"terms","missing":0,"total":0,"other":0,"terms":[]}}}
4

1 回答 1

3

如果你能创造出这样的东西,我想你会得到想要的结果。(请原谅 JSON 键上缺少引号!)

{
    query: {
      multi_match: {
        query: "test",
        fields: ["title", "description"]
      }
    },
    filter: {
      and: [
        {
          term: { status_id: 123 }
        },
        {
          term: { type_id: 456 }
        }
      ]
    },
    facets: {
      type: {
        terms: {
          field: "type_id",
          size: 10
        }
      },
      status: {
        terms: {
          field: "status_id",
          size: 10
        }
      }
    }
}

更新

我不知道轮胎,但会尝试写点东西!

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 25) do
      query do
        boolean do
          should { match :title params[:query] } if params[:query].present?
          should { match :description  params[:query] } if params[:query].present?
        end
      end

      sort { by :updated_at, "desc" } if params[:query].blank?

      filter :and, { :term => { :status_id => params[:status_id] } } if params[:status_id].present?
                   { :term => { :type_id => params[:type_id] } } if params[:type_id].present?
    end
  end

您可能必须修复红宝石,但有几点需要注意。匹配查询是推荐的默认字符串搜索,它们比query_string那些查询更快(尽管您的控制力稍差)。还

于 2013-08-17T12:15:57.523 回答