高级查询
一、布尔组合(bool)
bool
把各种其它查询通过must
(与)、must_not
(非)、should
(或)的方式进行组合
must+must :求交集
should+should :求并集
GET /jaychan/_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": "jaychan",
"_type": "goods",
"_id": "2",
"_score": 0.5753642,
"_source": {
"title": "大米手机",
"images": "http://image.leyou.com/12479122.jpg",
"price": 2899
}
}
]
}
}
二、范围查询(range)
range
查询找出那些落在指定区间内的数字或者时间
GET /jaychan/_search
{
“query”:{
“range”: {
“price”: {
“gte”: 1000.0,
“lt”: 2800.00
}
}
}
}
rang字段字符说明:
操作符 | 说明 |
---|---|
gt | 大于 |
gte | 大于等于 |
lt | 小于 |
lte | 小于等于 |
三、模糊查询(fuzzy)
新增一个商品:
POST /jaychan/goods/4
{
"title":"apple手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":6899.00
}
fuzzy
查询是 term
查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离(误差范围)不得超过2。
GET /jaychan/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
上面的查询,也能查询到apple手机
我们可以通过fuzziness
来指定允许的编辑距离:
GET /jaychan/_search
{
"query": {
"fuzzy": {
"title": {
"value":"appla",
"fuzziness":1
}
}
}
}