1. データ準備
POST /liush/_bulk
{"index":{"_id":1}}
{ "title":"小米手机", "images":"http://image.jd.com/12479122.jpg", "price":1999, "stock": 200, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":2}}
{"title":"超米手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":3}}
{ "title":"小米电视", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "电视", "brand": "小米" } }
{"index":{"_id":4}}
{ "title":"小米笔记本", "images":"http://image.jd.com/12479122.jpg", "price":4999, "stock": 200, "attr": { "category": "笔记本", "brand": "小米" } }
{"index":{"_id":5}}
{ "title":"华为手机", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":6}}
{ "title":"华为笔记本", "images":"http://image.jd.com/12479122.jpg", "price":5999, "stock": 200, "attr": { "category": "笔记本", "brand": "华为" } }
{"index":{"_id":7}}
{ "title":"荣耀手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":8}}
{ "title":"oppo手机", "images":"http://image.jd.com/12479122.jpg", "price":2799, "stock": 400, "attr": { "category": "手机", "brand": "oppo" } }
{"index":{"_id":9}}
{ "title":"vivo手机", "images":"http://image.jd.com/12479122.jpg", "price":2699, "stock": 300, "attr": { "category": "手机", "brand": "vivo" } }
{"index":{"_id":10}}
{ "title":"华为nova手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
2.マッチクエリ(マッチ)
GET /liush/_search
{
"query": {
"match": {
"title": "小米手机"
}
}
}
「Xiaomi mobile phone」だけでなく、「Xiaomi」または「mobile phone」に関連する多くのデータが照会され、複数の単語の間に「or」関係があることを示します。
場合によっては、より正確な検索が必要です。この関係を「and」にしたい場合は、次のようにします。
GET /liush/_search
{
"query": {
"match": {
"title": {
"query": "小米手机",
"operator": "and"
}
}
}
}
3.タームクエリ(ターム)
「term」クエリは、トークン化されていない数値、時間、ブール値、または文字列などの正確な値を照合するために使用されます。
GET /liush/_search
{
"query":{
"term":{
"price": 4999
}
}
}
4.範囲クエリ(範囲)
`range` クエリは、指定された範囲内にある数値または時間を検索します
`range` クエリでは、次の文字を使用できます。
オペレーター | 例証する |
---|---|
gt | 以上 |
ゲート | 以上 |
それ | 未満 |
LTE | 以下 |
GET /liush/_search
{
"query":{
"range": {
"price": {
"gte": 1000,
"lt": 3000
}
}
}
}
5. ブールの組み合わせ (bool)
ブールクエリは複合クエリとも呼ばれます
`bool` は、他のさまざまなクエリを `must` (and)、`must_not` (not)、`should` (or) と組み合わせます。
GET /liush/_search
{
"query":{
"bool":{
"must": [
{
"range": {
"price": {
"gte": 1000,
"lte": 3000
}
}
},
{
"range": {
"price": {
"gte": 2000,
"lte": 4000
}
}
}
]
}
}
}
注: 組み合わせクエリに表示できるのは 1 つの組み合わせのみであり、混在させることはできません。
6. フィルター
すべてのクエリは、ドキュメントのスコアリングとランキングに影響します。クエリ結果をフィルタリングする必要があり、フィルター条件がスコアリングに影響を与えたくない場合は、フィルター条件をクエリ条件として使用しないでください。代わりに `filter` メソッドを使用してください
GET /liush/_search
{
"query": {
"bool": {
"must": {
"match": { "title": "小米手机" }
},
"filter": {
"range": {
"price": { "gt": 2000, "lt": 3000 }
}
}
}
}
}
注: `filter` では、`bool` 結合条件フィルタリングも再度実行できます。
7.ソート(ソート)
`sort` を使用すると、さまざまなフィールドでソートでき、 `order` でソート方法を指定できます
GET /liush/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": { "order": "desc" }
},
{
"_score": { "order": "desc"}
}
]
}
8. ページネーション (from/size)
from: どちらから始めるか
サイズ: 何枚取るか
GET /liush/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"from": 2,
"size": 2
}
9. ハイライト
発見: ハイライトの本質は、キーワードに <em> タグを追加し、フロント エンドのタグにスタイルを追加することです。
- フィールド: ハイライト フィールド
- pre_tags: プレラベル
- post_tags: 投稿タグ
GET /liush/_search
{
"query": {
"match": {
"title": "小米"
}
},
"highlight": {
"fields": {"title": {}},
"pre_tags": "<em>",
"post_tags": "</em>"
}
}
10. 結果のフィルタリング (_source)
デフォルトでは、elasticsearch は、ドキュメントの `_source` に格納されているすべてのフィールドを検索結果に返します。
一部のフィールドのみを取得したい場合は、`_source` フィルタリングを追加できます
GET /liush/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}