ES 查询优化(一)_es lucene 查询空字符串

        "title": "quick"
    }
}

}


2、如果查询条件与文档排序无关,则一定要用filter,既不用参与分数计算,还能缓存数据,加快下次查询。


比如说要查询类型为Ford,黄色的,名字包含dev的汽车,一般的查询语句应该如下:



GET /my_index/my_type/_search
{
“bool”: {
“must”: [
{
“term”: {
“type”: “ford”
}
},
{
“term”: {
“color”: “yellow”
}
},
{
“term”: {
“name”: “dev”
}
}
]
}
}


上述查询中类型和颜色同样参与了文档排名得分的计算,但是由于类型和颜色仅作为过滤条件,计算得分至于name的匹配相关。因此上述的查询是不合理且效率不高的。



GET /my_index/my_type/_search
{
“bool”: {
“must”: {
“term”: {
“name”: “dev”
}
},
“filter”: [
{
“term”: {
“type”: “ford”
}
},
{
“term”: {
“color”: “yellow”
}
}]
}
}


3、如果对查出的数据的顺序没有要求,则可按照\_doc排序,取数据时按照插入的顺序返回。



> 
> \_doc has no real use-case besides being the most efficient sort order. So if you don’t care about the order in which documents are returned, then you should sort by \_doc. This especially helps when scrolling. \_doc to sort by index order.
> 
> 
> 



GET /my_index/my_type/_search
{
“query”: {
“term”: {
“name”: “dev”
}
},
“sort”:[
“_doc”
]
}


4、随机取n条(n>=10000)数据


1)可以利用ES自带的方法random score查询。缺点慢,消耗内存。



GET /my_index/my_type/_search
{
“size”: 10000,
“query”: {
“function_score”: {
“query”: {
“term”: {
“name”: “dev”
}
},
“random_score”: {

        }
    }
}

}


2)可以利用ES的脚本查询。缺点比random score少消耗点内存,但比random score慢。



GET /my_index/my_type/_search
{
“query”: {
“term”: {
“name”: “dev”
}
},
“sort”: {
“_script”: {
“type”: “number”,
“script”: {
“lang”: “painless”,
“inline”: “Math.random()”
},
“order”: “asc”
}
}
}


3)插入数据时,多加一个字段mark,该字段的值随机生成。查询时,对该字段排序即可。



GET /my_index/my_type/_search
{
“query”: {
“term”: {
“name”: “dev”
}
},
“sort”:[
“mark”
]
}


5、range Aggregations时耗时太长



{
“aggs” : {
“price_ranges” : {
“range” : {
“field” : “price”,
“ranges” : [
{ “from” : 10, “to” : 50 },
{ “from” : 50, “to” : 70 },
{ “from” : 70, “to” : 100 }
]
}
}
}
}


如例子所示,我们对[10,50),[50,70),[70,100)三个区间做了聚合操作。因为涉及到比较操作,数据量较大的情况下,可能会比较慢。 解决方案:在插入时,将要聚合的区间以keyword的形式写入索引中,查询时,对该字段做聚合即可。



假设price都小于100,插入的字段为mark,mark的值为10-50, 50-70, 70-100。
{
“aggs” : {
“genres” : {
“terms” : { “field” : “mark” }
}
}
}


6、查询空字符串


如果是要查字段是否存在或丢失,用Exists Query查询即可(exists, must\_not exits)。



GET /_search
{
“query”: {
“exists” : { “field” : “user” }
}
}

GET /_search
{
“query”: {
“bool”: {
“must_not”: {
“exists”: {
“field”: “user”
}
}
}
}
}


这里指的是字段存在,且字段为“”的field。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值