本文主要記錄es的基本查詢api的使用html
{ "query": { "term": { "title": "crime" } } }
指定權重正則表達式
{ "query": { "term": { "title": { "value":"crime", "boost":10.0 } } } }
多term查詢
查詢tags中包含novel或bookjson
{ "query": { "terms": { "tags": ["novel","book"] } } }
簡單理解就是去除停用詞的高權限,分高低頻兩組去查詢,像停用詞就是高頻的,cutoff_frequency表示低於這個機率的詞將出如今低頻組中。api
{ "query": { "common": { "title":{ "query":"crime and punishment", "cutoff_frequency":0.001 } } } }
不支持lucene查詢語法
){ "query": { "match": { "title": "crime and punishment" } } }
要求and或者or匹配文本的分詞elasticsearch
{ "query": { "match": { "title": { "query":"crime and punishment", "operator":"and" } } } }
{ "query": { "match_phrase": { "title": { "query":"crime punishment", "slop":1 } } } }
對查詢關鍵詞的最後一個詞條作前綴匹配ide
{ "query": { "match_phrase_prefix": { "title": { "query":"crime punish", "slop":1, "max_expansions":20 } } } }
針對多個字段查詢
){ "query": { "multi_match": { "query":"crime heller", "fields":["title","author"] } } }
支持lucene的查詢語法
)title字段包含crime,且權重爲10,也要包含punishment,可是otitle不包含cat,同事author字段包含Fyodor和dostoevsky。性能
{ "query": { "query_string": { "query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)", "default_field":"title" } } }
use_dis_max使用最大分查詢,max指對於給定的關鍵詞,只有最高分纔會包括在最後的文檔的評分中,而不是全部包含該詞條的全部字段分數之和。ui
{ "query": { "query_string": { "query":"crime heller", "fields":["title","author"], "use_dis_max":true } } }
解析出錯時不拋異常,丟棄查詢無效的部分code
{ "query": { "simple_query_string": { "query":"title:crime^10 +title:punishment -otitle:cat +author:(+Fyodor +dostoevsky)", "default_operator":"or" } } }
使用惟一表示uid來講查找regexp
{ "query": { "ids": { "type":"book", "values":["1","2","3"] } } }
前綴匹配給定的關鍵詞
{ "query": { "prefix": { "title":"cri" } } }
指定權重
{ "query": { "prefix": { "title":{ "value":"cri", "boost":3.0 } } } }
使用編輯距離的模糊查詢,計算量較大,可是對用戶拼寫錯的場景比較有用
{ "query": { "fuzzy": { "title":"crme" } } }
指定最小類似度誤差
{ "query": { "fuzzy": { "title":{ "value":"crme", "min_similarity":1 } } } }
支持*和?等通配符
{ "query": { "wildcard": { "title": "cr?me" } } }
只能針對單個字段,能夠是數值型的,也能夠是基於字符串的。
{ "query": { "range": { "year": { "gte" :1890, "lte":1900 } } } }
查詢性能取決於正則表達式
{ "query": { "regexp": { "title": { "value" :"cr.m[ae]", "boost":10.0 } } } }
組合查詢
){ "query": { "bool": { "must": { "term": { "title": "crime" } }, "should": { "range": { "year": { "from": 1900, "to": 2000 } } }, "must_not": { "term": { "otitle": "nothing" } } } } }