ES基本查詢
阿新 • • 發佈:2019-03-03
rop and operator 版本號 pre led lar time cor
# 創建索引
PUT /lib1
{
"settings": {
"number_of_shards": 5
, "number_of_replicas": 0
},
"mappings": {
"user":{
"properties": {
"name":{"type":"text"},
"adress":{"type":"text"},
"age":{"type":"integer"},
"interests":{"type":"text"},
"birthday":{"type":"date"}
}
}
}
}
# 添加數據
PUT /lib1/user/1
{
"name":"zhaoliu",
"adress":"hei long jiang sheng tie ling shi",
"age":50,
"birthday":"1970-10-12",
"interests":"xi huan hejiu,duanlian,lvyou"
}
PUT /lib1/user/2
{
"name":"zhaoming",
"adress":"bei ling dian qu qing he zhen",
"age":20,
"birthday":"1998-10-12",
"interests":"xi huan hejiu,duanlian,changge"
}
PUT /lib1/user/3
{
"name":"lisi",
"adress":"bei ling dian qu qing he zhen",
"age":50,
"birthday":"1998-10-12",
"interests":"xi huan hejiu,duanlian,lvyou"
}
PUT /lib1/user/4
{
"name":"wangwu",
"adress":"bei ling dian qu qing he zhen",
"age":20,
"birthday":"1995-10-12",
"interests":"xi huan hejiu,duanlian,changge"
}
PUT /lib1/user/5
{
"name":"zhangsan",
"adress":"bei jing chao yang qu",
"age":29,
"birthday":"1988-10-12",
"interests":"xi huan tingyinyue,changge,lvyou"
}
# 簡單條件查詢
GET /lib1/user/_search?q=name:lisi
GET /lib1/user/_search?q=interests:changge&sort=age:desc
term查詢和terms查詢
-
term query回去倒排索引中尋找確切的term,它並不知道分詞器的存在。這種查詢適合keyword、numeric、date。
-
term:查詢某個字段含有某個關鍵詞的文檔
GET /lib1/user/_search
{
"query": {
"term": {
"name": "zhaoliu"
}
}
}
# 查詢結果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
} -
terms:查詢某個字段裏含有多個關鍵詞的文檔
# 查詢
GET /lib1/user/_search
{
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
# 查詢的結果為interests字段包含heijiu、changge、hejiu和changge的文檔
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1.5467954,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 1.5467954,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "5",
"_score": 0.2824934,
"_source": {
"name": "zhangsan",
"adress": "bei jing chao yang qu",
"age": 29,
"birthday": "1988-10-12",
"interests": "xi huan tingyinyue,changge,lvyou"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2824934,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
# 指定返回結果為2個文檔
GET /lib1/user/_search
{
"from": 0,
"size": 2,
"query": {
"terms": {
"interests": ["hejiu","changge"]
}
}
}
# 指定返回結果中含版本號
GET /lib1/user/_search
{
"from": 0,
"size": 2, "version": true, "query": { "terms": { "interests": ["hejiu","changge"] } }}# 查詢結果{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 1.5467954, "hits": [ { "_index": "lib1", "_type": "user", "_id": "2", "_version": 3, "_score": 1.5467954, "_source": { "name": "zhaoming", "adress": "bei ling dian qu qing he zhen", "age": 20, "birthday": "1998-10-12", "interests": "xi huan hejiu,duanlian,changge" } }, { "_index": "lib1", "_type": "user", "_id": "5", "_version": 2, "_score": 0.2824934, "_source": { "name": "zhangsan", "adress": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,lvyou" } } ] }}
match查詢
-
math query指定分詞器的存在,會對filed進行分詞操作,然後再查詢
GET /lib1/user/_search
{
"query": {
"match": {
"name": "zhaoliuwu"
}
}
}
# 執行結果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
?
# 查詢
GET /lib1/user/_search
{
"query": {
"match": {
"name": "zhaoliuwu zhaoming"
}
}
}
# 執行結果
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.80259144,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 0.80259144,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12", "interests": "xi huan hejiu,duanlian,lvyou" } } ] }} -
match_all:查詢所有文檔
GET /lib1/user/_search
{
"query": {
"match_all": {}
}
} -
multi_match:指定多個字段
GET /lib1/user/_search
{
"query": {
"multi_match": {
"query": "changge",
"fields": ["interests","name"]
}
}
} -
match_phrase:短語匹配查詢
-
ElasticSearch引擎首先分析查詢字符串,從分析後的文本中構建短語查詢,這意味著必須匹配短語中的所有分詞,並且保證各個分詞的相對位置不變:
GET /lib1/user/_search
{
"query": {
"match_phrase": {
"interests": "duanlian,changge"
}
}
}
# 執行結果
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.5467954,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 1.5467954,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
}
}
]
}
} -
指定查詢字段
# 指定查詢字段
GET /lib1/user/_search
{
"_source": {
"includes": ["name","adress"]
},
"query": {
"match_all": {}
}
}
# 排除查詢字段
GET /lib1/user/_search
{
"_source": {
"excludes": ["name","adress"]
},
"query": {
"match_all": {}
}
}
# 通配符匹配查詢字段
GET /lib1/user/_search
{
"_source": {
"includes":"adr*",
"excludes": ["name","bir*"]
},
"query": {
"match_all": {}
}
}
# 執行結果
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "5",
"_score": 1,
"_source": {
"adress": "bei jing chao yang qu"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "4",
"_score": 1,
"_source": {
"adress": "北京海澱區清河"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 1,
"_source": {
"adress": "bei ling dian qu qing he zhen"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 1,
"_source": {
"adress": "hei long jiang sheng tie ling shi"
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "3",
"_score": 1, "_source": { "adress": "北京海澱區清河" } } ] }}
排序
-
使用sort實現排序:desc降序,asc升序
GET /lib1/user/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
GET /lib1/user/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
前綴匹配
GET /lib1/user/_search
{
"query": {
"match_phrase_prefix": {
"name": {
"query": "zhao"
}
}
}
}
範圍查詢
# []
GET /lib1/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1990-10-10",
"to": "2018-05-01"
}
}
}
}
# ()
GET /lib1/user/_search
{
"query": {
"range": {
"birthday": {
"from": "1995-10-12",
"to": "1998-10-12",
"include_lower":false,
"include_upper":false
}
}
}
}
GET /lib1/user/_search
{
"query": {
"range": {
"age": {
"gte": 20,
"lte": 28
}
}
}
}
wildcard查詢
-
允許使用通配符*和?來進行查詢
-
*代表0個或多個字符
-
?代表任意一個字符
GET /lib1/user/_search
{
"query": {
"wildcard": {
"name": {
"value": "li?i"
}
}
}
}
GET /lib1/user/_search
{
"query": {
"wildcard": {
"name": "zhao*"
}
}
} -
fuzzy實現模糊查詢
-
value:查詢的關鍵字
-
boost:查詢的權值,默認值是1.0
-
min_similarity:設置匹配的最小相似度,默認值是0.5,對於字符串,取值為0-1(包括0和1);對於數值,取值可能大於1;對於日期型取值為1d,1m等。
-
prefix_length:指名區分詞項的共同前綴長度,默認是0
-
max_expansion:查詢中的詞項可以擴展的數目,默認可以無限大
# 查詢
GET /lib1/user/_search
{
"query": {
"fuzzy": {
"name":"zhliuwu"
}
}
}
# 查詢結果
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2054872,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "1",
"_score": 0.2054872,
"_source": {
"name": "zhaoliuwu",
"adress": "hei long jiang sheng tie ling shi",
"age": 49,
"birthday": "1970-10-12",
"interests": "xi huan hejiu,duanlian,lvyou"
}
}
]
}
}
?
# 查詢
GET /lib1/user/_search
{
"query": {
"fuzzy": {
"interests": {
"value": "chagge"
}
}
}
, "highlight": {
"fields": {
"interests": {}
}
}
}
# 執行結果
{
"took": 43,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.64449805,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 0.64449805,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
},
"highlight": {
"interests": [
"xi huan hejiu,duanlian,<em>changge</em>"
]
}
},
{
"_index": "lib1",
"_type": "user", "_id": "5", "_score": 0.23541118, "_source": { "name": "zhangsan", "adress": "bei jing chao yang qu", "age": 29, "birthday": "1988-10-12", "interests": "xi huan tingyinyue,changge,lvyou" }, "highlight": { "interests": [ "xi huan tingyinyue,<em>changge</em>,lvyou" ] } } ] }}
高亮搜索結果
GET /lib1/user/_search
{
"query": {
"match": {
"interests": "changge"
}
},
"highlight": {
"fields": {"interests": {}}
}
}
# 查詢結果
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.7733977,
"hits": [
{
"_index": "lib1",
"_type": "user",
"_id": "2",
"_score": 0.7733977,
"_source": {
"name": "zhaoming",
"adress": "bei ling dian qu qing he zhen",
"age": 20,
"birthday": "1998-10-12",
"interests": "xi huan hejiu,duanlian,changge"
},
"highlight": {
"interests": [
"xi huan hejiu,duanlian,<em>changge</em>"
]
}
},
{
"_index": "lib1",
"_type": "user",
"_id": "5",
"_score": 0.2824934,
"_source": {
"name": "zhangsan",
"adress": "bei jing chao yang qu",
"age": 29,
"birthday": "1988-10-12",
"interests": "xi huan tingyinyue,changge,lvyou"
},
"highlight": {
"interests": [
"xi huan tingyinyue,<em>changge</em>,lvyou"
]
}
}
]
}
}
ES基本查詢