検索ブログ

主に検索のことについて書いています。

Elasticsearchでサンプルのデータセット(ドキュメント1000件)を用いてQuery DSLの簡単なフィルター機能(range)を試す

Elasticsearchでサンプルのデータセット(ドキュメント1000件)を用いてQuery DSLの簡単な検索(match queryとbool query)をいくつか試す方法を記す。

公式ドキュメント Executing Filters を参考にした。 www.elastic.co

検証環境: Elasticsearch 6.0.0-rc1

前提として、Elasticsearchにサンプルのデータセット(ドキュメント1000件)をbulk APIを使って登録しておきQuery DSLを用いて検索方法を試す。 sakura818uuu.hatenadiary.com

sakura818uuu.hatenadiary.com

sakura818uuu.hatenadiary.com

sakura818uuu.hatenadiary.com

検索結果の score フィールドは、検索クエリとドキュメントがどれだけ似ているかを示す指標である。
scoreフィールドの値が高いほどドキュメントとの関連度が高く、_scoreフィールドの値が低いほどドキュメントとの関連度が低い。

また、前提で紹介したbool queryにはfilterというフィルター機能がある。 例えば、rangeはドキュメントに対して範囲をフィルタリングすることができる。rangeは通常、数値や日付のフィルタリングをするときに使用される。

以下のクエリはbalance(アカウントの残高を意味する)が20000〜30000のドキュメントを検索することを意味する。

bash-3.2$ curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d'
> {
>   "query": {
>     "bool": {
>       "must": { "match_all": {} },
>       "filter": {
>         "range": {
>           "balance": {
>             "gte": 20000,
>             "lte": 30000
>           }
>         }
>       }
>     }
>   }
> }
> '
{
  "took" : 98,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 217,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "253",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 253,
          "balance" : 20240,
          "firstname" : "Melissa",
          "lastname" : "Gould",
          "age" : 31,
          "gender" : "M",
          "address" : "440 Fuller Place",
          "employer" : "Buzzopia",
          "email" : "melissagould@buzzopia.com",
          "city" : "Lumberton",
          "state" : "MD"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "400",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 400,
          "balance" : 20685,
          "firstname" : "Kane",
          "lastname" : "King",
          "age" : 21,
          "gender" : "F",
          "address" : "405 Cornelia Street",
          "employer" : "Tri@Tribalog",
          "email" : "kaneking@tri@tribalog.com",
          "city" : "Gulf",
          "state" : "VT"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "520",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 520,
          "balance" : 27987,
          "firstname" : "Brandy",
          "lastname" : "Calhoun",
          "age" : 32,
          "gender" : "M",
          "address" : "818 Harden Street",
          "employer" : "Maxemia",
          "email" : "brandycalhoun@maxemia.com",
          "city" : "Sidman",
          "state" : "OR"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "645",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 645,
          "balance" : 29362,
          "firstname" : "Edwina",
          "lastname" : "Hutchinson",
          "age" : 26,
          "gender" : "F",
          "address" : "892 Pacific Street",
          "employer" : "Essensia",
          "email" : "edwinahutchinson@essensia.com",
          "city" : "Dowling",
          "state" : "NE"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "734",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 734,
          "balance" : 20325,
          "firstname" : "Keri",
          "lastname" : "Kinney",
          "age" : 23,
          "gender" : "M",
          "address" : "490 Balfour Place",
          "employer" : "Retrotex",
          "email" : "kerikinney@retrotex.com",
          "city" : "Salunga",
          "state" : "PA"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "784",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 784,
          "balance" : 25291,
          "firstname" : "Mabel",
          "lastname" : "Thornton",
          "age" : 21,
          "gender" : "M",
          "address" : "124 Louisiana Avenue",
          "employer" : "Zolavo",
          "email" : "mabelthornton@zolavo.com",
          "city" : "Lynn",
          "state" : "AL"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "880",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 880,
          "balance" : 22575,
          "firstname" : "Christian",
          "lastname" : "Myers",
          "age" : 35,
          "gender" : "M",
          "address" : "737 Crown Street",
          "employer" : "Combogen",
          "email" : "christianmyers@combogen.com",
          "city" : "Abrams",
          "state" : "OK"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "14",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 14,
          "balance" : 20480,
          "firstname" : "Erma",
          "lastname" : "Kane",
          "age" : 39,
          "gender" : "F",
          "address" : "661 Vista Place",
          "employer" : "Stockpost",
          "email" : "ermakane@stockpost.com",
          "city" : "Chamizal",
          "state" : "NY"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "19",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 19,
          "balance" : 27894,
          "firstname" : "Schwartz",
          "lastname" : "Buchanan",
          "age" : 28,
          "gender" : "F",
          "address" : "449 Mersereau Court",
          "employer" : "Sybixtex",
          "email" : "schwartzbuchanan@sybixtex.com",
          "city" : "Greenwich",
          "state" : "KS"
        }
      },
      {
        "_index" : "bank",
        "_type" : "account",
        "_id" : "204",
        "_score" : 1.0,
        "_source" : {
          "account_number" : 204,
          "balance" : 27714,
          "firstname" : "Mavis",
          "lastname" : "Deleon",
          "age" : 39,
          "gender" : "F",
          "address" : "400 Waldane Court",
          "employer" : "Lotron",
          "email" : "mavisdeleon@lotron.com",
          "city" : "Stollings",
          "state" : "LA"
        }
      }
    ]
  }
}