1
2

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 3 years have passed since last update.

Elasticsearch とかのまとめ

Posted at

Elasticsearch  まとめ 

はじめに

ただのまとめ。(自分用)
結局まとめないとわかんない。
かぶっていてもその時の自分の環境でやるなら、自分で書いてまとめるのがベストだと信じてる。

  • 知りたいこと
    検索とか そのあたり

環境

  • macOS High Sierra
  • Docker 19.03.5
  • docker-compose 1.25.0
  • Elasticsearch v6.8.3

データ

適当にいれた。

本題

  • 全検索
GET _search
{
  "query": {
    "match_all": {}
  }
}
  • indexを絞って検索
GET kibana_sample_data_ecommerce/_search
{
  "query": {
    "match_all": {}
  }
}
  • mappling
GET /library/_mappings
  • フィールドの値一覧(keyword)
GET /library/_search
{
  "size":0,
  "aggs": {
    "genre": {
      "terms": {"field":"class.keyword"}
    }
  }
}

※type=keywordでなければ、キーワードつけないが数値は列挙する必要なさそう。

Response

{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 7,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "genre" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "DVD",
          "doc_count" : 3
        },
        {
          "key" : "book",
          "doc_count" : 2
        },
        {
          "key" : "CD",
          "doc_count" : 1
        }
      ]
    }
  }
}

当然値がないのは無視される。

  • フィールドの値集計(最大・最小・平均)
GET /library/_search
{
  "size": 0,
  "query": {
    "match": {
      "class": "DVD"
    }
  },
  "aggs": {
    "max_price": {
      "max": {
        "field": "price"
      }
    },
    "min_price": {
      "min": {
        "field": "price"
      }
    },
    "average": {
      "avg": {
        "field": "price"
      }
    }
  }
}

Response

{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "average" : {
      "value" : 71.94999885559082
    },
    "max_price" : {
      "value" : 125.94999694824219
    },
    "min_price" : {
      "value" : 17.950000762939453
    }
  }
}

  • 絞り込んだキーの一覧
GET /library/_search
{
  "size":0,
  "query": {
    "match": {
      "class": "DVD"
    }
  }, 
  "aggs": {
    "genre": {
      "terms": {"field":"sub_class.keyword"
      }
    }
  }
}

Response

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 6,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "genre" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "animation",
          "doc_count" : 2
        },
        {
          "key" : "movie",
          "doc_count" : 1
        }
      ]
    }
  }
}

  • and検索
GET /library/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "class": "DVD"
          }
        },
        {
          "match": {
            "sub_class": "animation"
          }
        }
      ]
    }
  }
}

参考

https://qiita.com/nskydiving/items/1c2dc4e0b9c98d164329#mapping
https://medium.com/eureka-engineering/基礎編-elasticsearchの検索クエリを使いこなそう-ace3e18c2174
https://itdepends.hateblo.jp/entry/2018/11/16/003706

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?