0
1

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.

Groongaでadjusterのスコアを重視して使う方法

Last updated at Posted at 2017-01-06

--adjusterによるスコア調整

Groongaのselectコマンドにはadjusterと呼ばれるスコア調整の仕組みがあります。

これは、カラムやインデックスを検索し、スコアのみを加算するものです。

--adjusterは、--query--filterの実行後に評価されるため、adjusterによるスコアだけでなく、
クエリ検索時のマッチスコアも加算されてしまいます。

adjusterによるスコアを重要視したいときに、マッチスコアが非常に大きくなると調整しにくいことがあります。

下記の例では、tagsにある重みよりも、bodyの出現回数が多い方のスコアが高くなってしまっています。
bodyがとても大きいようなケースでは1000や10000以上になることもあるでしょう。

table_create Tags TABLE_PAT_KEY ShortText
[[0,0.0,0.0],true]
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
[[0,0.0,0.0],true]
table_create Memos TABLE_NO_KEY
[[0,0.0,0.0],true]
column_create Memos body COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
column_create Memos tags COLUMN_VECTOR|WITH_WEIGHT Tags
[[0,0.0,0.0],true]
column_create Tags memos_tags COLUMN_INDEX|WITH_WEIGHT Memos tags
[[0,0.0,0.0],true]
column_create Terms index COLUMN_INDEX|WITH_POSITION Memos body
[[0,0.0,0.0],true]
load --table Memos
[
{
  "body": "Groonga Groonga Groonga Groonga Groonga Groonga Mroonga",
  "tags": {
    "groonga": 2
  }
},
{
  "body": "Groonga Groonga Mroonga",
  "tags": {
    "groonga": 5
  }
}
]
[[0,0.0,0.0],2]
select Memos   --match_columns 'body' \
  --query "groonga" \
  --adjuster 'tags @ "groonga"' \
  --output_columns body,_score
[
  [
    0,
    0.0,
    0.0
  ],
  [
    [
      [
        2
      ],
      [
        [
          "body",
          "ShortText"
        ],
        [
          "_score",
          "Int32"
        ]
      ],
      [
        "Groonga Groonga Groonga Groonga Groonga Groonga Mroonga",
        9
      ],
      [
        "Groonga Groonga Mroonga",
        8
      ]
    ]
  ]
]

bodyの出現回数よりもtagsの重みを重視したいような場合は、以下のようにmatch_columnsscorer_tf_at_mostスコアラー関数を適用すると
クエリ検索によるマッチスコアを0もしくは所定の上限値に抑えることができます。

select Memos  \
 --match_columns 'scorer_tf_at_most(body, 0)' \
 --query "groonga" \
 --adjuster 'tags @ "groonga"' \
 --output_columns body,_score
[
  [
    0,
    0.0,
    0.0
  ],
  [
    [
      [
        2
      ],
      [
        [
          "body",
          "ShortText"
        ],
        [
          "_score",
          "Int32"
        ]
      ],
      [
        "Groonga Groonga Groonga Groonga Groonga Groonga Mroonga",
        3
      ],
      [
        "Groonga Groonga Mroonga",
        6
      ]
    ]
  ]
]

これによりmatch_columnsqueryのスコアは0に抑えられ、adjusterのスコアのみが適用されました。

なお、この方法で抑えられるのは、--queryによるスコアのみです。--filterによって加算されるスコア
は調整できません。

filterによるスコアの影響を抑えるには、adjusterの係数を少し大きくすることによって調整する必要があるかもしれません。

--scorerによるスコア調整

adjusterはカラムやインデックスを検索することによりスコアを加算することができますが、単純にカラムに入っている値をスコアに加算したい
ような場合は、--scorerを使うことができます。

この--scorerは、--adjusterの調整後に評価されるため、--adjusterの前にスコアをリセットする用途で利用することはできません。

table_create Tags TABLE_PAT_KEY ShortText
[[0,0.0,0.0],true]
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
[[0,0.0,0.0],true]
table_create Memos TABLE_NO_KEY
[[0,0.0,0.0],true]
column_create Memos body COLUMN_SCALAR ShortText
[[0,0.0,0.0],true]
column_create Memos tags COLUMN_VECTOR|WITH_WEIGHT Tags
[[0,0.0,0.0],true]
column_create Memos some_score COLUMN_SCALAR Int32
[[0,0.0,0.0],true]
column_create Tags memos_tags COLUMN_INDEX|WITH_WEIGHT Memos tags
[[0,0.0,0.0],true]
column_create Terms index COLUMN_INDEX|WITH_POSITION Memos body
[[0,0.0,0.0],true]
load --table Memos
[
{
  "body": "Groonga Groonga Groonga Groonga Groonga Groonga Mroonga",
  "tags": {
    "groonga": 2
  },
  "some_score": 100
},
{
  "body": "Groonga Groonga Mroonga",
  "tags": {
    "groonga": 5
  },
  "some_score": 10
}
]
[[0,0.0,0.0],2]
select Memos  \
 --match_columns 'scorer_tf_at_most(body, 0)' \
 --query "groonga" \
 --adjuster 'tags @ "groonga"' \
 --scorer '_score += some_score' \
 --output_columns body,_score
[
  [
    0,
    0.0,
    0.0
  ],
  [
    [
      [
        2
      ],
      [
        [
          "body",
          "ShortText"
        ],
        [
          "_score",
          "Int32"
        ]
      ],
      [
        "Groonga Groonga Groonga Groonga Groonga Groonga Mroonga",
        103
      ],
      [
        "Groonga Groonga Mroonga",
        16
      ]
    ]
  ]
]
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?