データ
まずelasticsearchに3つのテストデータを追加
PUT /ecommerce/product/1
{
"name" : "gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
PUT /ecommerce/product/2
{
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags": [ "fangzhu" ]
}
PUT /ecommerce/product/3
{
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags": [ "qingxin" ]
}
1.query列検索
構文:
タイプ/ _searchのGET /インデックス/
- すべてのデータのクエリ
{
"took": 2, //took:耗费了几毫秒
"timed_out": false, //是否超时,这里是没有
"_shards": { //数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3, //查询结果的数量,3个document
"max_score": 1, //score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
"hits": [ //包含了匹配搜索的document的详细数据
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": 1,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 1,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": 1,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin"
]
}
}
]
}
}
- 条件付きクエリデータ:
クエリ文字列検索の起源、検索パラメータが偶発的にクエリ文字列のHTTPリクエストに基づいているため、このような製品名を検索するように、商品のyagaoが含まれており、降順での価格に従い、:
GET / eコマース/製品/ _searchを?Q =名前:ソートyagao& =価格:DESC
{
"took": 35,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "ecommerce",
"_type": "product",
"_id": "3",
"_score": null,
"_source": {
"name": "zhonghua yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags": [
"qingxin"
]
},
"sort": [
40
]
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": null,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
},
"sort": [
30
]
},
{
"_index": "ecommerce",
"_type": "product",
"_id": "2",
"_score": null,
"_source": {
"name": "jiajieshi yagao",
"desc": "youxiao fangzhu",
"price": 25,
"producer": "jiajieshi producer",
"tags": [
"fangzhu"
]
},
"sort": [
25
]
}
]
}
}
すぐに必要な情報を取得するための要求を発行し、そのようなカールなどの一時的な使用のコマンドラインツール、適した;しかし、クエリ要求は非常に複雑であれば、それはほとんどのクエリを使用し、本番環境では、ビルドすることは困難です文字列検索
2.queryのDSL
DSL:ドメインは言語、言語固有のフィールド指定された
リクエストボディ、ビルドJSON形式のクエリ構文に使用することができ、より便利に、あなたは複雑な構文の様々なを構築することができます:httpリクエストボディを
- すべての商品を発見
GET /ecommerce/product/_search
{
"query": { "match_all": {} }
}
- 価格の降順にソートしながら、クエリ名は、yagao品が含まれています
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"name" : "yagao"
}
},
"sort": [
{ "price": "desc" }
]
}
- 私たちは二商品をチェックアウトするように、各ページには、商品、今ショーの2ページを表示すると仮定して、クエリ商品、3品目の合計をページング
GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
- 名前を見つけると指定するには、商品の価格がすることができ
GET /ecommerce/product/_search
{
"query": { "match_all": {} },
"_source": ["name", "price"]
}
本番環境での使用に適して、あなたは、複雑なクエリを構築することができます
3.queryフィルタ
検索製品名はyagaoが含まれており、商品の価格以上の25元
GET /ecommerce/product/_search
{
"query" : {
"bool" : {
"must" : {
"match" : {
"name" : "yagao"
}
},
"filter" : {
"range" : {
"price" : { "gt" : 25 }
}
}
}
}
}
4.fullテキスト検索(全文検索)
これは、データ照合プロデューサーとyagaoのプロデューサーが含まれています
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"producer" : "yagao producer"
}
}
}
5.phrase検索(検索語句)
フルテキスト検索とは対照的に、フルテキスト検索の解体検索文字列があれば、どのような解体に一致する単語は、結果として返すことができるよう、転置インデックスマッチングの内側にそれらを得るために、入力に開放される
ために頼む、フレーズ検索検索文字列が完全に正確に同じ含まれている、テキストフィールドに指定する必要があり、結果として返すために、試合を数えることができます
GET /ecommerce/product/_search
{
"query" : {
"match_phrase" : {
"producer" : "yagao producer"
}
}
}
6.highlight検索(ハイライトされた検索結果)
GET /ecommerce/product/_search
{
"query" : {
"match" : {
"producer" : "producer"
}
},
"highlight": {
"fields" : {
"producer" : {}
}
}
}
下に示すように、効果:
7.様々なネストされた集計クエリ
- 最初の需要分析:各タグ内の項目数を算出したが
trueに、テキストフィールドのプロパティをfielddataます
PUT /ecommerce/_mapping/product
{
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
GET /ecommerce/product/_search
{
"size": 0, //不查询出数据,只统计
"aggs": {
"group_by_tags": {
"terms": { "field": "tags" }
}
}
}
クエリ結果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2
},
{
"key": "meibai",
"doc_count": 1
},
{
"key": "qingxin",
"doc_count": 1
}
]
}
}
}
- 商品名はyagaoが各タグ内の項目数を算出し、含まれています
GET /ecommerce/product/_search
{
"size": 0,
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"all_tags": {
"terms": {
"field": "tags"
}
}
}
}
クエリ結果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2
},
{
"key": "meibai",
"doc_count": 1
},
{
"key": "qingxin",
"doc_count": 1
}
]
}
}
}
- 最初のパケットは、その後、各群について計算した平均値は、各タグに商品の平均価格を計算します
GET /ecommerce/product/_search
{
"size": 0,
"aggs" : {
"group_by_tags" : {
"terms" : { "field" : "tags" },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}
クエリ結果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
},
{
"key": "meibai",
"doc_count": 2,
"avg_price": {
"value": 40
}
},
{
"key": "qingxin",
"doc_count": 1,
"avg_price": {
"value": 40
}
}
]
}
}
}
- タグで算出された各商品の平均価格、及び平均価格の降順でソート
GET /ecommerce/product/_search
{
"size": 0,
"aggs" : {
"all_tags" : {
"terms" : { "field" : "tags", "order": { "avg_price": "desc" } },
"aggs" : {
"avg_price" : {
"avg" : { "field" : "price" }
}
}
}
}
}
クエリ結果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"all_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "qingxin",
"doc_count": 1,
"avg_price": {
"value": 40
}
},
{
"key": "meibai",
"doc_count": 1,
"avg_price": {
"value": 30
}
},
{
"key": "fangzhu",
"doc_count": 2,
"avg_price": {
"value": 27.5
}
}
]
}
}
}
- 間隔は、各グループ内のタグによってグループ化され、その後、指定された価格範囲に従ってグループ化され、そして最終的に各群の平均価格を計算します
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},
{
"from": 20,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
クエリ結果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_price": {
"buckets": [
{
"key": "0.0-20.0",
"from": 0,
"to": 20,
"doc_count": 0,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
},
{
"key": "20.0-40.0",
"from": 20,
"to": 40,
"doc_count": 2,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "fangzhu",
"doc_count": 2,
"average_price": {
"value": 27.5
}
},
{
"key": "meibai",
"doc_count": 1,
"average_price": {
"value": 30
}
}
]
}
},
{
"key": "40.0-50.0",
"from": 40,
"to": 50,
"doc_count": 1,
"group_by_tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "qingxin",
"doc_count": 1,
"average_price": {
"value": 40
}
}
]
}
}
]
}
}
}