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?

【Elasticsearch】あいまい検索、複数のフィールドで検索

Posted at

あいまい検索とは

ある一つのワードを元に複数の項目で検索をかける

Elasticsearchでのあいまい検索の仕方

「本」というワードで検索をかけたい場合

{
  "query": {
    "bool": {
      "should": [
        { "wildcard": { "name.keyword": "*本*" }},
        { "wildcard": { "kana.keyword": "*本*" }},
        { "wildcard": { "mail.keyword": "*本*" }},
        { "wildcard": { "department.keyword": "*本*" }}
      ],
      "minimum_should_match": 1
    }
  }
}

should でOR検索、つまりどれかが一致していれば、という条件を書くことができる。
minimum_should_match にはn件一致していれば、という条件が入る。今回はどれか1つが一致していればよいので1。

wildcardで部分一致検索ができる。"*本*"のように前後に*を入れることで、例えば「本田」や「営業本部」等が検索結果としてヒットするようになる。

ちなみにname.keywordではなく、nameのみにすると、形態素解析が入るため、例えば検索ワードを「山田」として実行すると、「山」もしくは「田」のどちらかが入っていればヒットする扱いになるため、「本田」も「宮田」も「田中」もヒットするようになる。

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?