0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Solrで類似検索 (MoreLikeThis)と特徴語抽出を行う

Posted at

類似検索の仕組み

Apache Solr に蓄えた記事データを使って、ある記事と類似する記事を探すことができる。
仕組みとしてはまず対象とする記事の 特徴語 を抽出し、その特徴語とのマッチ度が高い記事を探している模様。

特徴語とは、その記事に含まれていて、かつ他の記事にはあまり含まれていないワードのこと。
例えば、「横須賀市」「マイクロソフト」「ドラえもん」などの固有名詞は条件を満たしやすく、逆に「」「」「」「」などの副詞や「」「」などの汎用的な名詞などは条件を満たしにくいということになる。

Solrでの検索例

/select?q=id:1&mlt=true&mlt.fl=mlt.mindf=1&mlt.mintf=1
パラメーター名
mlt true とすることで有効化する
mlt.fl どの項目を元に類似検索をするか、項目名をカンマ区切りで指定する
mlt.mintf Minimum Term Frequency。 元となる記事での出現頻度が指定数以下のワードは無視される
mlt.mindf Minimum Document Frequency。 複数記事上での出現頻度が指定数以下のワードは無視される

qfq パラメーターでヒットした各記事に対して類似記事を表示してくれる。

{
  "responseHeader":{
    "status":0,
    "QTime":33,
    "params":{
      "q":"id:335676",
      "mlt":"true",
      "mlt.fl":"title_txt_ja",
      "mlt.mindf":"1",
      "mlt.mintf":"1",
      "_":"1577422689083"}},
  "response":{"numFound":1,"start":0,"maxScore":13.465984,"docs":[
      {
        "id":"335676",
        "title_txt_ja":"○○市で○○事件が発生",
        "score":13.465984}]
  },
  "moreLikeThis":{
    "product_details-335676":{"numFound":156617,"start":0,"maxScore":19.945429,"docs":[
        {
          "id":"335219",
          "title_txt_ja":"○○事件の犯人を逮捕",
          "score":12.291839},
        {
          "id":"335218",
          "title_txt_ja":"○○事件の続報",
          "score":12.291838}]}
   }
}

特徴語も抽出したい

mltハンドラー にアクセスすれば特徴語も抽出できる模様。

solrconfig.xml に mltハンドラーが定義されていなければ定義しておく。

<requestHandler name="/mlt" class="solr.MoreLikeThisHandler">
</requestHandler>
/mlt?q=id:1&mlt=true&mlt.fl=mlt.mindf=1&mlt.mintf=1&mlt.interestingTerms=list
パラメーター名
mlt.interestingTerms none: 特徴語を出力しない
list: 出力する
details: フィールド名やboost値なども出力する

結果

"interestingTerms":[
    "ビーフカレー",
    "熟成"]

Solrに未登録の記事を元に類似検索したい場合は…?

やり方が見つからず今の所、記事をSolrに追加してから検索し、終わったら消すという風にしている。何か方法があれば…。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?