ES中高級檢索
1. 檢索方式
ES官方提供了兩中檢索方式:一種是通過
URL 參數進行搜索
,另一種是通過DSL(Domain Specified Language) 進行搜索
。官方更推薦使用第二種方式第二種方式是基於傳遞JSON作爲請求體(request body)格式與ES進行交互,這種方式更強大,更簡潔
。
1.1 測試數據
1.刪除索引
DELETE /ems
2.創建索引並指定類型
PUT /ems
{
"mappings":{
"emp":{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"integer"
},
"bir":{
"type":"date"
},
"content":{
"type":"text"
},
"address":{
"type":"keyword"
}
}
}
}
}
3.插入測試數據
PUT /ems/emp/_bulk
{"index":{}}
{"name":"小黑","age":23,"bir":"2012-12-12","content":"爲開發團隊選擇一款優秀的MVC框架是件難事兒,在衆多可行的方案中決擇需要很高的經驗和水平","address":"北京"}
{"index":{}}
{"name":"王小黑","age":24,"bir":"2012-12-12","content":"Spring 框架是一個分層架構,由 7 個定義良好的模塊組成。Spring 模塊構建在覈心容器之上,核心容器定義了創建、配置和管理 bean 的方式","address":"上海"}
{"index":{}}
{"name":"張小五","age":8,"bir":"2012-12-12","content":"Spring Cloud 作爲Java 語言的微服務框架,它依賴於Spring Boot,有快速開發、持續交付和容易部署等特點。Spring Cloud 的組件非常多,涉及微服務的方方面面,井在開源社區Spring 和Netflix 、Pivotal 兩大公司的推動下越來越完善","address":"無錫"}
{"index":{}}
{"name":"win7","age":9,"bir":"2012-12-12","content":"Spring的目標是致力於全方位的簡化Java開發。 這勢必引出更多的解釋, Spring是如何簡化Java開發的?","address":"南京"}
{"index":{}}
{"name":"梅超風","age":43,"bir":"2012-12-12","content":"Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基於內存亦可持久化的日誌型、Key-Value數據庫,並提供多種語言的API","address":"杭州"}
{"index":{}}
{"name":"張無忌","age":59,"bir":"2012-12-12","content":"ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口","address":"北京"}
1.2 URL檢索
GET /ems/emp/_search?q=*&sort=age:asc
_search
搜索的API
q=*
匹配所有文檔
sort
以結果中的指定字段排序
1.3 DSL檢索
NOTE: 以下重點講解DSL語法
GET /ems/emp/_search
{
"query": {"match_all": {}},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
2. DSL高級檢索(Query)
0. 查詢所有(match_all)
match_all關鍵字:
返回索引中的全部文檔
GET /ems/emp/_search
{
"query": { "match_all": {} }
}
1. 查詢結果中返回指定條數(size)
size 關鍵字
: 指定查詢結果中返回指定條數。默認返回值10條
GET /ems/emp/_search
{
"query": { "match_all": {} },
"size": 1
}
2. 分頁查詢(from)
from 關鍵字
: 用來指定起始返回位置,和**size關鍵字連用可實現分頁效果
**
GET /ems/emp/_search
{
"query": {"match_all": {}},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"size": 2,
"from": 1
}
3. 查詢結果中返回指定字段(_source)
_source
關鍵字: 是一個數組,在數組中用來指定展示那些字段
GET /ems/emp/_search
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}
4. 關鍵詞查詢(term)
term關鍵字
: 用來使用關鍵詞查詢
GET /ems/emp/_search
{
"query": {
"term": {
"address": {
"value": "北京"
}
}
}
}
NOTE1:
通過使用term查詢得知ES中默認使用分詞器爲標準分詞器(StandardAnalyzer),標準分詞器對於英文單詞分詞,對於中文單字分詞
。NOTE2:
通過使用term查詢得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 這些類型不分詞只有text類型分詞
。
5. 範圍查詢(range)
range 關鍵字
: 用來指定查詢指定範圍內的文檔
GET /ems/emp/_search
{
"query": {
"range": {
"age": {
"gte": 8,
"lte": 30
}
}
}
}
6. 前綴查詢(prefix)
prefix 關鍵字
: 用來檢索含有指定前綴的關鍵詞的相關文檔
GET /ems/emp/_search
{
"query": {
"prefix": {
"content": {
"value": "redis"
}
}
}
}
7. 通配符查詢(wildcard)
wildcard 關鍵字
: 通配符查詢 ? 用來匹配一個任意字符 * 用來匹配多個任意字符
GET /ems/emp/_search
{
"query": {
"wildcard": {
"content": {
"value": "re*"
}
}
}
}
8. 多id查詢(ids)
ids 關鍵字
: 值爲數組類型,用來根據一組id獲取多個對應的文檔
GET /ems/emp/_search
{
"query": {
"ids": {
"values": ["lg5HwWkBxH7z6xax7W3_","lQ5HwWkBxH7z6xax7W3_"]
}
}
}
9. 模糊查詢(fuzzy)
fuzzy 關鍵字
: 用來模糊查詢含有指定關鍵字的文檔
GET /ems/emp/_search
{
"query": {
"fuzzy": {
"content":"spring"
}
}
}
10. 布爾查詢(bool)
bool
: 用來組合多個條件實現複雜查詢
must
: 相當於&& 同時成立
should
: 相當於|| 成立一個就行
must_not
: 相當於! 不能滿足任何一個
GET /ems/emp/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 0,
"lte": 30
}
}
}
],
"must_not": [
{"wildcard": {
"content": {
"value": "redi?"
}
}}
]
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
11. 高亮查詢(highlight)
highlight 關鍵字
: 可以讓符合條件的文檔中的關鍵詞高亮
GET /ems/emp/_search
{
"query": {
"term": {
"content": {
"value": "redis"
}
}
},
"highlight": {
"fields": {
"*": {}
}
}
}
自定義高亮html標籤: 可以在highlight中使用
pre_tags
和post_tags
GET /ems/emp/_search
{
"query":{
"term":{
"content":"框架"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"fields": {
"*":{}
}
}
}
多字段高亮 使用
require_field_match
開啓多個字段高亮
GET /ems/emp/_search
{
"query":{
"term":{
"content":"框架"
}
},
"highlight": {
"pre_tags": ["<span style='color:red'>"],
"post_tags": ["</span>"],
"require_field_match":false,
"fields": {
"*":{}
}
}
}
12. 多字段查詢(multi_match)
**
multi_match 關鍵字:
**可以實現多字段查詢
GET /ems/emp/_search
{
"query": {
"multi_match": {
"query": "中國",
"fields": ["name","content"] #這裏寫要檢索的指定字段
}
}
}
13. 多字段分詞查詢(query_string)
query_string 關鍵字:
可以實現多字段分詞查詢
GET /dangdang/book/_search
{
"query": {
"query_string": {
"query": "中國聲音",
"analyzer": "ik_max_word",
"fields": ["name","content"]
}
}
}