0

太陽黒点の solr ドキュメントから取った実用的な例を使用します。コメント付きの投稿があり、投稿がそのタイトルとコメントにインデックスを付けているとしましょう:

class Post < ActiveRecord::Base
  searchable do
    text :title
    text :comments do
      comments.map { |comment| comment.body }
    end
  end
end

私が知りたいのは、投稿を検索したときに一致したコメントです (コメント フィールドを使用して一致した場合)。そのため、投稿と検索に一致したコメントをユーザーに表示できます。複数の一致がある場合は、スコアに基づいて最適な一致を取得します。ID をメタデータとしてコメント フィールドに追加することを考えていましたが、これを行う方法が見つかりませんでした。

そのフィールドを使用して一致した場合に取得できる、ある種のインデックスのないメタデータ (この場合は各コメントの ID) を添付する方法はありますか? これを解決するための他の提案はありますか?

4

1 に答える 1

0

私見では、投稿とコメントをさまざまなドキュメントに保存する必要があります。コメント ドキュメントに投稿 ID を入力できるため、各コメントがどの投稿に属しているかがわかります。

私のフィールドの提案は次のとおりです。

id - if you have the risk of getting the same id for a comment and a post, you should use some kind of prefix at the document id. In this case, you have another field to store the original id
_type (post or comment) - 
post_id - this field will store the post id, a comment is child. It will be empty at the post documents
title (empty for comments, maybe?)
content - the actual text a comment or post has
text - the field that contains all text you want to query in a document, via copyfield (only if you don't want to boost the individual fields)
<any other fields you may need>

単に ?q=text:"foo" を実行して投稿またはコメントをクエリすると、foo に一致するすべてのドキュメントが取得されます。その後、返された post_id を照会するか、何らかの結合を試みることができます。http://wiki.apache.org/solr/Joinを見てください。?q=({join from= post_id to=id}text:"foo") (+text:foo +_type:post) のようにします。

于 2013-07-01T00:05:26.293 に答える