《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.17.Text analysis, settings 及 mappings——3.4.2.17.3.全文搜索/精确搜索(5) https://developer.aliyun.com/article/1229939
range 的 relation 计算
可以主要参考下面的实践:
1、创建一个 range 类型的字段 agerange
POST my-index-000001/_mappings { "properties": { "agerange": { "type": "integer_range" } } }
2、创建两个文档
PUT my-index-000001/_doc/11?refresh { "agerange": { "lte": 21, "gte": 10 } } PUT my-index-000001/_doc/12 { "agerange": { "lte": 30, "gte": 20 } }
3、首先使用默认的 INTERSECTS 查询 10 到 21 范围的文档,应该两个文档都返回。
POST my-index-000001/_search { "query": { "range": { "agerange": { "gte": 10, "lte": 21 } } } } # 返回内容 { ...... "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my-index-000001", "_type" : "_doc", "_id" : "12", "_score" : 1.0, "_source" : { "agerange" : { "lte" : 30, "gte" : 20 } } }, { "_index" : "my-index-000001", "_type" : "_doc", "_id" : "11", "_score" : 1.0, "_source" : { "agerange" : { "lte" : 21, "gte" : 10 } } } ] } }
4、再使用 CONTAINS 查询范围为 10 到 21 的文档,应该只返回文档 11。
POST my-index-000001/_search { "query": { "range": { "agerange": { "gte": 10, "lte": 21, "relation": "CONTAINS" } } } } # 返回内容 { ...... "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my-index-000001", "_type" : "_doc", "_id" : "11", "_score" : 1.0, "_source" : { "agerange" : { "lte" : 21, "gte" : 10 } } } ] } }
5、再使用 CONTAINS 查询范围为 20 到 21 的文档,应该两个文档都返回。
POST my-index-000001/_search { "query": { "range": { "agerange": { "gte": 20, "lte": 21, "relation": "CONTAINS" } } } } # 返回内容 { ...... "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "my-index-000001", "_type" : "_doc", "_id" : "11", "_score" : 1.0, "_source" : { "agerange" : { "lte" : 21, "gte" : 10 } } }, { "_index" : "my-index-000001", "_type" : "_doc", "_id" : "12", "_score" : 1.0, "_source" : { "agerange" : { "lte" : 30, "gte" : 20 } } } ] } }
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.17.Text analysis, settings 及 mappings——3.4.2.17.3.全文搜索/精确搜索(7) https://developer.aliyun.com/article/1229936