【elasticsearch】查询改写
·
1.前缀查询
从Lucene的角度看,查询改写就是把费时的原始查询类型实例改写成
一个性能更高的查询类型实例。
--插入数据 。
curl -XPOST http://192.168.1.7:9200/clients/_doc/?pretty -H 'Content-Type: application/json' -d '{"id":"2","name":"Jane"}'
curl -XPOST http://192.168.1.7:9200/clients/_doc/?pretty -H 'Content-Type: application/json' -d '{"id":"1","name":"Joe"}'
curl -XPOST http://192.168.1.7:9200/clients/_doc/?pretty -H 'Content-Type: application/json' -d '{"id":"3","name":"Jack"}'
curl -XPOST http://192.168.1.7:9200/clients/_doc/?pretty -H 'Content-Type: application/json' -d '{"id":"4","name":"Rob"}'
curl -XPOST http://192.168.1.7:9200/clients/_doc/?pretty -H 'Content-Type: application/json' -d '{"id":"5","name":"Jannet"}'
--查询名字中以j开通的人。
curl -XGET 192.168.1.7:9200/clients/_search?pretty -H 'Content-Type: application/json' -d '{
"query":{"prefix":{"name":"j"}}
}'
2.带前缀查询
[esadmin@oracle1 esdb]$ curl -XGET 192.168.1.7:9200/clients/_search?pretty -H 'Content-Type: application/json' -d '{
> "query":{"prefix":{"name":"j"}}
> }'
{
"took" : 154,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "clients",
"_id" : "a0jOUpABu034hfiWNER3",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "Joe"
}
},
{
"_index" : "clients",
"_id" : "bEjOUpABu034hfiWNESU",
"_score" : 1.0,
"_source" : {
"id" : "3",
"name" : "Jack"
}
},
{
"_index" : "clients",
"_id" : "akjOUpABu034hfiWCURQ",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "Joe"
}
},
{
"_index" : "clients",
"_id" : "bkjOUpABu034hfiWNETJ",
"_score" : 1.0,
"_source" : {
"id" : "5",
"name" : "Jannet"
}
}
]
}
}
curl -XGET 192.168.1.7:9200/clients/_search?pretty -H 'Content-Type: application/json' -d '{
"query":{"prefix":{"name":"j"},
"rewrite":"constant_score_boolean"}
}'
--es8默认的应用类型是:x-www-form-urlencoded,
--同时这里不支持:rewrite:constant_score_boolean
[esadmin@oracle1 esdb]$ curl -XGET 192.168.1.7:9200/clients/_search?pretty -d '{
> "query":{"prefix":{"name":"j"}
> }'
{
"error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
"status" : 406
}
--查询所有的数据。
curl -XGET 192.168.1.7:9200/clients/_search?pretty
--ES8:查询重写语法。NAME前缀是:j
curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"prefix": {
"name": {"value":"j","rewrite": "constant_score_boolean"}
}
}
}'
--查询重写。
[esadmin@oracle1 esapp]$ curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
> {
> "query": {
> "prefix": {
> "name": {"value":"j","rewrite": "constant_score_boolean"}
> }
> }
> }'
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "clients",
"_id" : "a0jOUpABu034hfiWNER3",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "Joe"
}
},
{
"_index" : "clients",
"_id" : "bEjOUpABu034hfiWNESU",
"_score" : 1.0,
"_source" : {
"id" : "3",
"name" : "Jack"
}
},
{
"_index" : "clients",
"_id" : "akjOUpABu034hfiWCURQ",
"_score" : 1.0,
"_source" : {
"id" : "1",
"name" : "Joe"
}
},
{
"_index" : "clients",
"_id" : "bkjOUpABu034hfiWNETJ",
"_score" : 1.0,
"_source" : {
"id" : "5",
"name" : "Jannet"
}
}
]
}
}
--查询clients索引的映射。
curl -XGET 'localhost:9200/clients/_mapping?pretty'
[esadmin@oracle1 esapp]$ curl -XGET 'localhost:9200/clients/_mapping?pretty'
{
"clients" : {
"mappings" : {
"properties" : {
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
3.rewrite 的可选配置参数;
(1)
scoring_boolean:该选项将每个生成的词项转化为布尔查询中的一个或从句。
这种处理方法比较消耗CPU(要计算和保存每个词项的得分),而且有些查询生成的词项
太多从而超出了布尔值的查询限制,默认为1024个从句。改写后的查询会保存计算
出来的得分。默认的布尔查询限制可以通过设置 elasticsearch.yml 文件的
index.query.bool.max_clause_count 属性修改。需要注意,改写后的布尔查询的从句越多,
查询性能越低。
curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"prefix": {
"name": {"value":"j","rewrite": "scoring_boolean"}
}
}
}'
(2)
constant_score_boolean:该选项与前面提到的scoring_boolean类似,但是CPU消耗比较少,
这是因为该过程并不计算每个从句的得分,而是每个从句得到一个与查询权重相同的常数
得分,默认情况下等于1,当然我们也可以通过设置查询权重来改变这个值。与scoring_boolean
类似,该选项也有布尔从句的限制。
curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"prefix": {
"name": {"value":"j","rewrite": "constant_score_boolean"}
}
}
}'
(3)constant_score
正如Lucene的Javadoc描述的那样,该选项按如下方式改写原始查询:通过顺序遍历每个
词项来创建一个私有的过滤器,标记跟每个词项相关的所有文档。命中的文档被赋予
一个跟查询权重相同的常量得分。当命中词项数或文档数较大时,该方法比
scoring_boolean 和 constant_score_boolean 执行速度更快。
constant_score
curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"prefix": {
"name": {"value":"j","rewrite": "constant_score"}
}
}
}'
(4)top_terms_N
该选项将每个生成的词项转化为布尔查询中的一个或从句,并保存计算出来的查询得分。
与 scoring_boolean 不同之处在于,该方法只保留了最佳的前N 个词项,从而避免
超出了从句数的限制。
curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"prefix": {
"name": {"value":"j","rewrite": "top_terms_100"}
}
}
}'
---N可以是任意值。
(5)top_terms_boost_N
该选项与top_terms_N类似,不同之处在于该选项产生的从句类型为常量得分查询,
得分为从句的权重。
curl -XGET 'localhost:9200/clients/_search?pretty' -H 'Content-Type: application/json' -d '
{
"query": {
"prefix": {
"name": {"value":"j","rewrite": "top_terms_boost_100"}
}
}
}'
--N可以是任意整数。
4.总结
如果能接受低精度(往往伴随着高性能),那么可以采用top N查询改写方法。
如果需要更高的查询精度(往往伴随着低性能),那么可以选择使用布尔方法。
更多推荐
所有评论(0)