Elasticsearch7與以前版本最大的區別就是捨棄了type的概念,默認的type爲"_doc"。app
先建索引 測試
PUT /shop
dynamic,推薦用strict.net
修改dynamic參數code
POST /shop/_mapping { "dynamic": strict }
text類型 能夠被分詞 可是 keyword 分詞是不能用的 要全匹配orm
修改或新增mapping字段typeblog
POST /shop/_mapping { "properties":{ "name":{ "type":"text", }, "content":{ "type":"text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "createTime":{ "type":"date", "format": "yyyy-MM-dd HH:mm:ss" } } }
新增數據:排序
-POST shop/_doc/558pa4e2 { "name":"圓領韓版學生男女毛衣秋冬裝上裝", "breif":"150件左右圓領韓版學生男女毛衣秋冬裝,長袖寬鬆套頭情侶裝針織衫上衣", "minPrice":"15", "sn":"558pa4e2", "createTime":"2019-10-07 14:30:23", "sort":"100", "tags":"9月第2波" }
爲索引添加別名索引
-POST /_aliases { "actions": [ { "add": { "index": "shop-item", "alias": "shop-item-alias" } } ] }
分詞器測試:文檔
POST /_analyze { "analyzer": "ik_smart", "text": "關於加快建設合肥地鐵七號線的通知說明" }
單字段查詢:在name中查詢有"女裝"的數據get
POST /shop/_doc/_search { "query":{ "match":{ "name":"女裝" } } }
單字段查詢:在name中查詢有"女裝"的數據,且只返回name、brief數據
POST /shop/_doc/_search { "query":{ "match":{ "name":"女裝" } }, "_source": ["name","brief"] }
分頁查詢
POST /shop/_doc/_search { "query":{ "match":{ "name":"女裝" } }, "_source": ["name","brief"], "from":0, "size":10 }
排序,在name中查詢有"女裝"的數據,並按createTime倒序
{ "query":{ "match":{ "name":"女裝" } }, "sort":{ "createTime": { "order": "desc" } } }
多字段排序,排序,在name中查詢有"女裝"的數據,先按sort字段排序,若是相同再按createTime倒序
{ "query":{ "match":{ "name":"女裝" } }, "sort": { "sort":"asc", "createTime": "desc" } }
查詢"name"字段中包含"女裝"和"品牌"的全部數據
POST /shop/_doc/_search { "query": { "bool": { "must": [ { "match": { "name": "品牌" } }, { "match": { "name": "女裝" } } ] } } }
查詢"name"字段中包含"女裝"或"品牌"的全部數據
POST shop/_doc/_search { "query": { "bool": { "should": [ { "match": { "name": "品牌" } }, { "match": { "name": "女裝" } } ] } } }
查詢"name"字段中既不包含"女裝"也不包含"品牌"的全部數據
POST /shop/_doc/_search { "query": { "bool": { "must_not": [ { "match": { "name": "品牌" } }, { "match": { "name": "女裝" } } ] } } }
多字段查詢:在name,與brief中查詢有"女裝"的數據
POST /shop/_doc/_search { "query":{ "multi_match":{ "query":"女裝", "fields":["name","brief"] } } }
精確查找:查找tags既屬於「9月第1波」又屬於"9月第2波"的數據(terms用於精確查找,通常用於非text字段)
POST /shop/_doc/_search { "query":{ "bool":{ "must":[ {"term":{ "tags":"9月第1波" }}, {"term":{ "tags":"9月第2波" }} ] } } }
過濾器的用法,filter的效率高,儘量的用filter
POST /shop/_doc/_search { "query":{ "bool":{ "filter":[ { "term":{"tags":"9月第1波"} }, { "term":{"tags":"9月第2波"} } ] } } }
範圍查詢
POST /shop/_doc/_search { "query":{ "range":{ "minPrice":{ "gte":"4", "lte":"5" } } } }
簡單聚合
POST /shop/_doc/_search { "aggs":{ "tagsGroup":{ "terms":{ "field":"tags" } }, "priceGroup":{ "terms":{ "field":"minPrice" } } } }
參考 https://blog.csdn.net/qq_36697880/article/details/100660867