3

has_manyアソシエーションにファセットを追加したいと思います。

PostgreSQL全文検索からelasticsearchに移行しています。

現在、検索を行うために次の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)}")

私は同様の問題についてのSOの答えを見ました(HABTM協会)

クエリでのElasticsearchのファセットトラブル

同様のアプローチで実装されたコード

-文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

しかし、このコードは機能しません。

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

}

そして、ここで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: }]

Tireにhas_manyアソシエーションを正しく実装する方法はありますか?

PSそしてはい私はタイヤマッピングを変更した後にレーキタスクを実行しました。


編集:

わかった。コードの1行を変更してエラーを修正500しました(terms => term)。

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

しかし今、私の検索結果は空ですか?誰もがこれを修正する方法を知っていますか?おそらく私は他の種類のマッチを使うべきです(私は現在boolean+mustコンボを使っています)。

4

1 に答える 1

1

タイプミスがありました。これを修正すると、すべて正常に動作します。

他のユーザーのために明確なコードを投稿します

-マッピング

  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

-検索方法

  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
于 2012-09-11T12:20:37.590 に答える