简体   繁体   English

如何在Tire(elasticsearch)中添加facet到has_many关联

[英]How to add facet in Tire (elasticsearch) to has_many association

I want to add facet to has_many association. 我想将facet添加到has_many关联中。

I am migrating from PostgreSQL fulltext search to elasticsearch. 我正在从PostgreSQL全文搜索迁移到elasticsearch。

Currently I have following SQL query to do search (PostgreSQL way): 目前我有以下SQL查询做搜索(PostgreSQL方式):

rt = "#{Rule.table_name}"
Sentence.
  joins(:rules).joins(:regulations).joins(:speakers).
  where(:authority_name => params[:authority_name], :authority_detail_1 => params[:authority_detail_1]).
  where(:regulations => {:regulation_name => params[:regulation_name]}).
  where("#{rt}.description @@ #{Sentence.sanitize(words)} OR #{rt}.headline @@ #{Sentence.sanitize(words)}")

I looked at SO answer about similiar problem (HABTM association) 我看了关于类似问题的答案(HABTM协会)

Facet Troubles with Elasticsearch on Query 在查询上使用Elasticsearch进行方面的麻烦

and implemented code with similiar approach 并用类似的方法实现代码

-Sentence AR class -Sentence AR类

class Sentence < ActiveRecord::Base
  # other staff

  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :id
    indexes :authority_name, :type => :string, :index => "not_analyzed"
    indexes :authority_detail_1, :type => :string, :index => "not_analyzed"
    indexes :authority_detail_2, :type => :string, :index => "not_analyzed"
    indexes :regulations, :type => :object, :properties => {
      :regulation_name => { :type => :string, :index => "not_analyzed" }
    }
  end

  def to_indexed_json
    to_json(:include => [:rules, :regulations, :speaker])
  end

  def self.search(params)
    tire.search(page: params[:page], per_page: 5, load: true) do
      query do
        boolean do
          must { string params[:query], default_operator: "AND" } if params[:query].present?
          must { term :authority_name, params[:authority_name] } if params[:authority_name].present?
          must { term :authority_detail_1, params[:authority_detail_1] } if params[:authority_detail_1].present?
          must { term :authority_detail_2, params[:authority_detail_2] } if params[:authority_detail_2].present?

          must { terms 'regulations.regulation_name', params[:regulation_name] } if params[:regulation_name].present?
        end
      end
      facet 'authorities' do
        terms :authority_name
        terms :authority_detail_1
        terms :authority_detail_2
      end
      facet 'regulations' do
        terms 'regulations.regulation_name'
      end
      # raise to_curl
    end
  end

  # other staff

end # end of Class

But this code doesnt work. 但是这段代码不起作用。

After call to API I got an error: 调用API后我收到一个错误:

Tire::Search::SearchRequestFailed in SentencesController#search

{ {

"error": "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[-ocpucv-TwGCmLfPd3U9hQ][sentences][4]: SearchParseException[[sentences][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"appalto\",\"default_operator\":\"AND\"}},{\"terms\":{\"regulations.regulation_name\":\"Cod. civ.\"}}]}},\"facets\":{\"authorities\":{\"terms\":{\"field\":\"authority_detail_2\",\"size\":10,\"all_terms\":false}},\"regulations\":{\"terms\":{\"field\":\"regulations.regulation_name\",\"size\":10,\"all_terms\":false}}},\"size\":5,\"from\":0}]]]; nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][0]: SearchParseException[[sentences][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"appalto\",\"default_operator\":\"AND\"}},{\"terms\":{\"regulations.regulation_name\":\"Cod. civ.\"}}]}},\"facets\":{\"authorities\":{\"terms\":{\"field\":\"authority_detail_2\",\"size\":10,\"all_terms\":false}},\"regulations\":{\"terms\":{\"field\":\"regulations.regulation_name\",\"size\":10,\"all_terms\":false}}},\"size\":5,\"from\":0}]]]; nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][1]: SearchParseException[[sentences][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"query\":{\"bool\":{\"must\":[{\"query_string\":{\"query\":\"appalto\",\"default_operator\":\"AND\"}},{\"terms\":{\"regulations.regulation_name\":\"Cod. civ.\"}}]}},\"facets\":{\"authorities\":{\"terms\":{\"field\":\"authority_detail_2\",\"size\":10,\"all_terms\":false}},\"regulations\":{\"terms\":{\"field\":\"regulations.regulation_name\",\"size\":10,\"all_terms\":false}}},\"size\":5,\"from\":0}]]]; nested: }]",
"status": 500

} }

And here JSON error parsed to more 'humar readable form': 并且这里将JSON错误解析为更多'humar可读形式':

SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[-ocpucv-TwGCmLfPd3U9hQ][sentences][4]: SearchParseException[[sentences][4]: from[-1],size[-1]: Parse Failure [Failed to parse source
[{"query":
   {"bool":
     {"must": [
       {"query_string":{"query":"appalto","default_operator":"AND"}},
       {"terms":{"regulations.regulation_name":"Cod. civ."}}
     ]}
   },
    "facets":
     {"authorities":
       {"terms":
         {"field":"authority_detail_2","size":10,"all_terms":false}
       },
      "regulations":
       {"terms":
         {"field":"regulations.regulation_name","size":10,"all_terms":false}
       }
     },
     "size":5,"from":0}]
]];
nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][0]: SearchParseException[[sentences][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"query_string":{"query":"appalto","default_operator":"AND"}},{"terms":{"regulations.regulation_name":"Cod. civ."}}]}},"facets":{"authorities":{"terms":{"field":"authority_detail_2","size":10,"all_terms":false}},"regulations":{"terms":{"field":"regulations.regulation_name","size":10,"all_terms":false}}},"size":5,"from":0}]]];
nested: }{[-ocpucv-TwGCmLfPd3U9hQ][sentences][1]: SearchParseException[[sentences][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":[{"query_string":{"query":"appalto","default_operator":"AND"}},{"terms":{"regulations.regulation_name":"Cod. civ."}}]}},"facets":{"authorities":{"terms":{"field":"authority_detail_2","size":10,"all_terms":false}},"regulations":{"terms":{"field":"regulations.regulation_name","size":10,"all_terms":false}}},"size":5,"from":0}]]];
nested: }]

Any idea how to implement correctly has_many association in Tire? 知道如何在Tire中正确实现has_many关联吗?

PS And YES I have run rake task after changing Tire mapping. PS和是我在更改轮胎映射后运行了rake任务。


EDIT: 编辑:

OK. 好。 I fixed 500 error by changing one line in code (terms => term). 我通过更改代码中的一行(terms => term)修复了500错误。

  must { term 'regulations.regulation_name', params[:regulation_name] } if params[:regulation_name].present?

But now my search results are empty? 但现在我的搜索结果是空的? Anyone know how to fix this? 有人知道怎么修这个东西吗? Perhaps I should use other kind of match (I am currently using boolean + must combo). 也许我应该使用其他类型的匹配(我目前正在使用boolean + must combo)。

I was having a typo. 我有一个错字。 When I fixed this everything works fine. 当我修复这一切一切正常。

I will post clear code for other users 我将为其他用户发布明确的代码

-mappings -mappings

  mapping do
    indexes :id
    indexes :authority_name, :type => :string, :index => "not_analyzed"
    indexes :authority_detail_1, :type => :string, :index => "not_analyzed"
    indexes :authority_detail_2, :type => :string, :index => "not_analyzed"

    indexes :regulations do
      indexes :name, :type => :string, :index => "not_analyzed"
    end
  end

  def to_indexed_json
    to_json(:include => [:rules, :regulations, :speaker])
  end

-search method - 搜索方法

  def self.search(params)
    empty_params = false
    if params[:query].blank? && params[:authority_name].blank? &&
      params[:authority_detail_1].blank? && params[:authority_detail_2].blank? &&
      params[:regulation_name].blank? && params[:regulation_number].blank? &&
      params[:regulation_year].blank? && params[:regulation_article].blank?
      empty_params = true
    end

    unless empty_params
      tire.search(page: params[:page], per_page: 5, load: true) do
        query do
          boolean do
            must { string params[:query], default_operator: "AND" } if params[:query].present?
            must { term :authority_name, params[:authority_name] } if params[:authority_name].present?
            must { term :authority_detail_1, params[:authority_detail_1] } if params[:authority_detail_1].present?
            must { term :authority_detail_2, params[:authority_detail_2] } if params[:authority_detail_2].present?

            must { term "regulations.name", params[:regulation_name] } if params[:regulation_name].present?
            must { term "regulations.norm_number", params[:regulation_number] } if params[:regulation_number].present?
            must { term "regulations.year", params[:regulation_year] } if params[:regulation_year].present?
            must { term "regulations.article_number", params[:regulation_article] } if params[:regulation_article].present?
          end
        end
        # raise to_curl
      end
    else
      tire.search(page: params[:page], per_page: 5, load: true) do
        query do
          all
        end
      end
    end
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM