文章目录
1. 简介
这里介绍的是es对lucene的spanQuery的一些使用接口,主要有下面的几种查询
- span_term query: 和term查询等效,一般用来在其他span query中作为组合查询
- span_near query: 和intervals查询中的match 查询很像,约束多个查询term的order和gap
- span_not query: 和boost query很像,但是包含的是 include和excludes两个条件(boost query 是positive和negative)
- span_or query: 结合多个span query ,和bool 查询中的should很像,只要命中一个即可
- span_first query: 要查询的term在对应的field中的位置距离起始位置不能超过N
- span_within query: 这个挺特殊,是分为little和big两个span query, 而且little的query中的term必须在big的多个term之间(在source field中对应的text当中)
- span_containing query: 这个感觉和span_within一样,就是查询顺序不一样,先查little再过滤big条件
- field_masking_span query: 可以让span-near or span-or across different fields,具体使用没有看太明白
- span_multi query: Wraps a term, range, prefix, wildcard, regexp, or fuzzy query.
2. 样例
原始数据
GET multi_test/_search
返回
{
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_source" : {
"shingle" : "If the calculations based on the specification determine that",
"title" : "If the calculations based on the specification determine that",
"age" : 80,
"status" : true
}
},
{
"_source" : {
"shingle" : "my favorite food hot water",
"title" : "my favorite food hot water",
"age" : 23,
"status" : true
}
},
{
"_source" : {
"shingle" : "that no optional clauses are needed, the usual rules about BooleanQueries ",
"title" : "that no optional clauses are needed, the usual rules about BooleanQueries ",
"age" : 39,
"status" : false
}
},
{
"_source" : {
"shingle" : "rules about BooleanQueries still apply at search ",
"title" : "rules about BooleanQueries still apply at search ",
"age" : 42,
"status" : false
}
},
{
"_source" : {
"shingle" : "If the calculations based",
"title" : " suggester requires special mapping up ",
"age" : 38,
"status" : false
}
},
{
"_source" : {
"shingle" : "my favorite food is pisa",
"title" : "the my favorite that is pisa",
"age" : 35,
"status" : false
}
}
]
}
}
1. span_term query
GET multi_test/_search
{
"query": {
"span_term": {
"title": {
"value": "that"
}
}
}
}
2. span_near query
GET multi_test/_search
{
"query": {
"span_near": {
"clauses": [
{
"span_term": {
"title": {
"value": "up"
}
}
},
{
"span_term": {
"title": {
"value": "suggester"
}
}
}
],
"slop": 3,
"in_order": false
}
}
}
3. span_not query
GET multi_test/_search
{
"query": {
"span_not": {
"include": {
"span_term": {
"title": {
"value": "favorite"
}
}
},
"exclude": {
"span_near": {
"clauses": [
{
"span_term": {
"title": {
"value": "my"
}
}
},
{
"span_term": {
"title": {
"value": "favorite"
}
}
}
],
"slop": 0,
"in_order": false
}
}
}
}
}
我在测试的时候,如果exclude当中使用span term的话那么会出现exclude失效的情况,具体原因不太清楚
4. span_or query
GET multi_test/_search
{
"query": {
"span_or": {
"clauses": [
{
"span_term": {
"title": {
"value": "food"
}
}
},
{
"span_term": {
"title": {
"value": "favorite"
}
}
}
]
}
}
}
5. span_first query
GET multi_test/_search
{
"query": {
"span_first": {
"match": {
"span_term": {
"title": "hot"
}
},
"end": 4
}
}
}
返回
"_source" : {
"shingle" : "my favorite food hot water",
"title" : "my favorite food hot water",
"age" : 23,
"status" : true
}
6. span_within query
GET multi_test/_search
{
"query": {
"span_within": {
"little": {
"span_term": {
"title": {
"value": "special"
}
}
},
"big": {
"span_near": {
"clauses": [
{
"span_term": {
"title": {
"value": "suggester"
}
}
},
{
"span_term": {
"title": {
"value": "mapping"
}
}
}
],
"slop": 5,
"in_order": false
}
}
}
}
}
返回
"_source" : {
"shingle" : "If the calculations based",
"title" : " suggester requires special mapping up ",
"age" : 38,
"status" : false
}
这里的litte中的term special
在源文档中必须在big查询的两个term suggester
, mapping
中间才行,否则查不出来
7. span_containing query
GET multi_test/_search
{
"query": {
"span_containing": {
"little": {
"span_term": {
"title": {
"value": "special"
}
}
},
"big": {
"span_near": {
"clauses": [
{
"span_term": {
"title": {
"value": "suggesterzh"
}
}
},
{
"span_term": {
"title": {
"value": "mapping"
}
}
}
],
"slop": 5,
"in_order": false
}
}
}
}
}
要求同span within 查询
8. field_masking_span query
这个没有用明白。。。
GET multi_test02/_search
{
"query": {
"span_near": {
"clauses": [
{
"span_term": {
"title": "requires"
}
},
{
"field_masking_span": {
"query": {
"span_term": {
"shingle": "based"
}
},
"field": "title"
}
}
],
"slop": 1,
"in_order": false
}
}
}
9. span_multi query
GET multi_test/_search
{
"query": {
"span_multi": {
"match": {
"prefix": {
"title": {
"value": "sug"
}
}
}
}
}
}
返回
"_source" : {
"shingle" : "If the calculations based",
"title" : " suggester requires special mapping up ",
"age" : 38,
"status" : false
}