LoginSignup
3
0

More than 1 year has passed since last update.

この記事は 株式会社サイバー・バズ Advent calendar 2021 22日目の記事です。

概要

OpenSearchで全文検索をしている時に、想定通りに結果が返ってこないことがありました。

事象

シャンプーで全文検索したい!!

POST [index_name]/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name": {
                            "analyzer": "my_analyzer",
                            "fuzziness": "AUTO",
                            "query": "シャンプー"
                        }
                    }
                },
                {
                    "match": {
                        "content": {
                            "analyzer": "my_analyzer",
                            "fuzziness": "AUTO",
                            "query": "シャンプー"
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "publishFrom": {
                "mode": "max",
                "order": "desc",
                "nested_path": "nested_path_key"
            }
        }
    ]
}

すると返ってきた値は、

トリートメント
シャンプー
シャンプー(詰め替え)
・
・
・

でした。

うーん。シャンプーとかシャンプー(詰め替え)が先頭になってほしいな

あ!!分かった。重み付けをすれば良さそう!!

OpenSearchにはboostというものがあるので、それによって検索に重み付けをすることができます。(documentはElasticSearch)

今回はnameが一番優先されるようにしたいので、以下のようにしました。

POST [index_name]/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name": {
                            "analyzer": "my_analyzer",
                            "fuzziness": "AUTO",
                            "query": "シャンプー",
                            "boost": 2
                        }
                    }
                },
                {
                    "match": {
                        "content": {
                            "analyzer": "my_analyzer",
                            "fuzziness": "AUTO",
                            "query": "シャンプー"
                        }
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "publishFrom": {
                "mode": "max",
                "order": "desc",
                "nested_path": "nested_path_key"
            }
        }
    ]
}

これでnameが優先されるはず、、
検索!!

トリートメント
シャンプー
シャンプー(詰め替え)
・
・
・

変わらず。。

色々調べていると、ある事にふと気がつきました。

responseで返ってきている、max_scorenullです。

あれ、全文検索使っている時はmax_scoreに数値が返ってくるはずでは??

これでは、重み付けどうこうの話ではなさそうです。

結果的に以下のQueryで解決できました。

POST [index_name]/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "name": {
                            "analyzer": "my_analyzer",
                            "fuzziness": "AUTO",
                            "query": "シャンプー",
                            "boost": 2
                        }
                    }
                },
                {
                    "match": {
                        "content": {
                            "analyzer": "my_analyzer",
                            "fuzziness": "AUTO",
                            "query": "シャンプー"
                        }
                    }
                }
            ]
        }
    }
}

今までと何が異なるのかというと、sortを無くしました。

sortの並び替えが優先されるので、scoreで検索順位が変わるはずもありません。

フロントからの指定がない場合は、デフォルトでpublishFromsortを入れていたので、全く気がつきませんでした。笑

同じことでハマる方はいなさそうですが、誰かの助けになれば幸いです。

3
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
3
0