基本查询
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" }} ] }