基本查詢
GET /索引庫名/_search { "query":{ "查詢類型":{ "查詢條件":"查詢條件值" } } }
這里的query代表一個查詢對象,里面可以有不同的查詢屬性
-
查詢類型:
-
例如:
match_all
,match
,term
,range
等等
-
-
查詢條件:查詢條件會根據類型的不同,寫法也有差異
GET /test/_search { "query":{ "match_all": {} } }
-
-
match_all
{ "took": 2, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "test", "_type": "goods", "_id": "2", "_score": 1, "_source": { "title": "大米手機", "images": "http://image.test.com/12479122.jpg", "price": 2899 } }, { "_index": "test", "_type": "goods", "_id": "r9c1KGMBIhaxtY5rlRKv", "_score": 1, "_source": { "title": "小米手機", "images": "http://image.test.com/12479122.jpg", "price": 2699 } } ] } }
-
-
time_out:是否超時
-
_shards:分片信息
-
hits:搜索結果總覽對象
-
total:搜索到的總條數
-
max_score:所有結果中文檔得分的最高分
-
hits:搜索結果的文檔對象數組,每個元素是一條搜索到的文檔信息
-
_index:索引庫
-
_type:文檔類型
-
_id:文檔id
-
_score:文檔得分
-
-
GET /test/_search { "query":{ "match":{ "title":"小米電視" } } }
結果
"hits": { "total": 2, "max_score": 0.6931472, "hits": [ { "_index": "test", "_type": "goods", "_id": "tmUBomQB_mwm6wH_EC1-", "_score": 0.6931472, "_source": { "title": "小米手機", "images": "http://image.test.com/12479122.jpg", "price": 2699 } }, { "_index": "test", "_type": "goods", "_id": "3", "_score": 0.5753642, "_source": { "title": "小米電視4A", "images": "http://image.test.com/12479122.jpg", "price": 3899 } } ] }
GET /test/_search { "query":{ "match": { "title": { "query": "小米電視", "operator": "and" } } } }
有時候這正是我們期望的,但在全文搜索的大多數應用場景下,我們既想包含那些可能相關的文檔,同時又排除那些不太相關的。換句話說,我們想要處於中間某種結果。
match
查詢支持 minimum_should_match
最小匹配參數, 這讓我們可以指定必須匹配的詞項數用來表示一個文檔是否相關。我們可以將其設置為某個具體數字,更常用的做法是將其設置為一個百分數
,因為我們無法控制用戶搜索時輸入的單詞數量:
GET /test/_search { "query":{ "match":{ "title":{ "query":"小米曲面電視", "minimum_should_match": "75%" } } } }
本例中,搜索語句可以分為3個詞,如果使用and關系,需要同時滿足3個詞才會被搜索到。這里我們采用最小品牌數:75%,那么也就是說只要匹配到總詞條數量的75%即可,這里3*75% 約等於2。所以只要包含2個詞條就算滿足條件了。
GET /test/_search { "query":{ "multi_match": { "query": "小米", "fields": [ "title", "subTitle" ] } } }
GET /test/_search { "query":{ "term":{ "price":2699.00 } } }
GET /test/_search { "query":{ "terms":{ "price":[2699.00,2899.00,3899.00] } } }
默認情況下,elasticsearch在搜索的結果中,會把文檔中保存在_source
的所有字段都返回。
GET /test/_search { "_source": ["title","price"], "query": { "term": { "price": 2699 } } }
GET /test/_search { "_source": { "includes":["title","price"] }, "query": { "term": { "price": 2699 } } }
GET /test/_search { "_source": { "excludes": ["images"] }, "query": { "term": { "price": 2699 } } }
GET /test/_search { "query":{ "bool":{ "must": { "match": { "title": "大米" }}, "must_not": { "match": { "title": "電視" }}, "should": { "match": { "title": "手機" }} } } }
{ "took": 10, "timed_out": false, "_shards": { "total": 3, "successful": 3, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5753642, "hits": [ { "_index": "test", "_type": "goods", "_id": "2", "_score": 0.5753642, "_source": { "title": "大米手機", "images": "http://image.test.com/12479122.jpg", "price": 2899 } } ] } }
GET /test/_search { "query":{ "range": { "price": { "gte": 1000.0, "lt": 2800.00 } } } }
`range`查詢允許以下字符:
| 操作符 | 說明 |
| :----: | :------: |
| gt | 大於 |
| gte | 大於等於 |
| lt | 小於 |
| lte | 小於等於 |
GET /test/_search { "query": { "fuzzy": { "title": "appla" } } }
上面的查詢,也能查詢到apple手機
我們可以通過fuzziness
GET /test/_search { "query": { "fuzzy": { "title": { "value":"appla", "fuzziness":1 } } } }
條件查詢中進行過濾
GET /test/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手機" }}, "filter":{ "range":{"price":{"gt":2000.00,"lt":3800.00}} } } } }
GET /test/_search { "query":{ "constant_score": { "filter": { "range":{"price":{"gt":2000.00,"lt":3000.00}} } } }
GET /test/_search { "query": { "match": { "title": "小米手機" } }, "sort": [ { "price": { "order": "desc" } } ] }
GET /goods/_search { "query":{ "bool":{ "must":{ "match": { "title": "小米手機" }}, "filter":{ "range":{"price":{"gt":200000,"lt":300000}} } } }, "sort": [ { "price": { "order": "desc" }}, { "_score": { "order": "desc" }} ] }