文章目录

01. ElasticSearch range查询是什么?02. ElasticSearch range 查询支持哪些数据类型?03. ElasticSearch range 查询数值型数据?04. ElasticSearch 字符串类型和文本类型的区别?05. ElasticSearch range 查询字符串类型的数据?06. ElasticSearch range 无法查询文本类型的数据?07. ElasticSearch range 查询日期类型的数据?08. ElasticSearch range 查询时间戳类型的数据?09. ElasticSearch range 查询数组类型数据?10. ElasticSearch range 查询对象类型的数据?11. SpringBoot整合ES实现 range 查询

01. ElasticSearch range查询是什么?

Elasticsearch 中的 range 查询可以用于查询某个字段在一定范围内的文档。

range 查询可同时提供包含和不包含这两种范围表达式,可供组合的选项如下:

gt: > 大于(greater than)lt: < 小于(less than)gte: >= 大于或等于(greater than or equal to)lte: <= 小于或等于(less than or equal to)

02. ElasticSearch range 查询支持哪些数据类型?

它支持数值、日期、字符串、IP地址、地理范围等类型的字段。

03. ElasticSearch range 查询数值型数据?

① 索引文档,构造数据:

PUT /my_index

{

"mappings": {

"properties": {

"price":{

"type": "integer"

}

}

}

}

PUT /my_index/_doc/1

{

"price":10

}

PUT /my_index/_doc/2

{

"price":20

}

PUT /my_index/_doc/3

{

"price":30

}

② 查询 price 字段的值在 10 到 20 之间的文档 :

GET /my_index/_search

{

"query": {

"range": {

"price": {

"gte": 10,

"lte": 20

}

}

}

}

{

"took" : 9,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 2,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"price" : 10

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"price" : 20

}

}

]

}

}

04. ElasticSearch 字符串类型和文本类型的区别?

在Elasticsearch中,字符串类型和文本类型是两种不同的数据类型,它们在索引和搜索时有一些区别。

字符串类型是未经分词的字符串,它们被视为单个词项,并且可以用于精确匹配和范围查询。字符串类型的字段可以映射为keyword类型或text类型。如果将字段映射为keyword类型,则该字段将被视为未经分词的字符串,可以用于精确匹配和范围查询。如果将字段映射为text类型,则该字段将被视为经过分词器处理的文本,可以用于全文搜索和短语匹配。

文本类型是经过分词器处理的文本,它们被拆分成多个词项,并且可以用于全文搜索和短语匹配。文本类型的字段只能映射为text类型。

以下是一些字符串类型和文本类型的区别:

字符串类型可以用于精确匹配和范围查询,而文本类型可以用于全文搜索和短语匹配。 字符串类型的字段可以映射为keyword类型或text类型,而文本类型的字段只能映射为text类型。 字符串类型的字段被视为单个词项,而文本类型的字段被拆分成多个词项。 字符串类型的字段不会被分词器处理,而文本类型的字段会被分词器处理。

总之,字符串类型和文本类型是两种不同的数据类型,它们在索引和搜索时有一些区别。您需要根据具体的需求选择适合的数据类型。

05. ElasticSearch range 查询字符串类型的数据?

在Elasticsearch中,您可以使用range查询来搜索字符串类型的数据,但是在索引时需要注意一些细节。

默认情况下,Elasticsearch会将字符串类型的字段视为text类型,这意味着它们会被分词器处理,并且会被拆分成多个词项。因此,如果您想使用range查询来搜索字符串类型的数据,您需要将字段映射为keyword类型,以便将其视为未经分词的字符串。

① 索引文档,构造数据:

PUT /my_index

{

"mappings": {

"properties": {

"tag":{

"type": "keyword"

}

}

}

}

PUT /my_index/_doc/1

{

"tag":"A"

}

PUT /my_index/_doc/2

{

"tag":"B"

}

PUT /my_index/_doc/3

{

"tag":"C"

}

② 查询 tag 字段的值在A到C之间的文档:

GET /my_index/_search

{

"query": {

"range": {

"tag": {

"gte": "A",

"lte": "C"

}

}

}

}

{

"took" : 2,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 3,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"tag" : "A"

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"tag" : "B"

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"tag" : "C"

}

}

]

}

}

需要注意的是,使用range查询搜索字符串类型的数据时,查询结果是基于字典序的,而不是基于数值大小的。总之,您可以使用range查询来搜索字符串类型的数据,但是在索引时需要将字段映射为keyword类型,以便将其视为未经分词的字符串。

06. ElasticSearch range 无法查询文本类型的数据?

在Elasticsearch中,range查询不适用于文本类型的数据。range查询是基于数值或日期范围的查询,而不是基于文本范围的查询。如果您需要搜索文本类型的数据,可以使用其他类型的查询,例如match查询、prefix查询、wildcard查询、regexp查询等。

07. ElasticSearch range 查询日期类型的数据?

① 索引文档,构造数据:

PUT /my_index

{

"mappings": {

"properties": {

"createTime":{

"type": "date",

"format": "yyyy-MM-dd HH:mm:ss"

}

}

}

}

PUT /my_index/_doc/1

{

"createTime":"2023-03-29 10:30:11"

}

PUT /my_index/_doc/2

{

"createTime":"2023-03-29 10:35:11"

}

PUT /my_index/_doc/3

{

"createTime":"2023-03-29 10:38:11"

}

② 查询 createTime 字段的值在日期 2023-03-29 10:35:11 到 2023-03-29 10:38:11 之间的文档:

GET /my_index/_search

{

"query": {

"range": {

"createTime": {

"gte": "2023-03-29 10:35:11",

"lte": "2023-03-29 10:38:11"

}

}

}

}

{

"took" : 5,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 2,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"createTime" : "2023-03-29 10:35:11"

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"createTime" : "2023-03-29 10:38:11"

}

}

]

}

}

08. ElasticSearch range 查询时间戳类型的数据?

① 索引文档,构造数据:

PUT /my_index

{

"mappings": {

"properties": {

"createTime":{

"type": "date",

"format": "epoch_millis"

}

}

}

}

PUT /my_index/_doc/1

{

"createTime":1640995200000

}

PUT /my_index/_doc/2

{

"createTime":1672531198000

}

PUT /my_index/_doc/3

{

"createTime":1672531199000

}

② 查询 createTime 字段的值在时间戳 1640995200000 到 1672531199000之间的文档:

{

"took" : 1038,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 3,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"createTime" : 1640995200000

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"createTime" : 1672531198000

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"createTime" : 1672531199000

}

}

]

}

}

09. ElasticSearch range 查询数组类型数据?

range查询将会匹配包含任何一个数组元素在指定范围内的文档。如果您需要匹配所有数组元素都在指定范围内的文档,可以使用nested查询或parent-child关系来处理嵌套的数组类型数据。

① 索引文档,数据构造:

PUT /my_index

{

"mappings": {

"properties": {

"price":{

"type": "integer"

}

}

}

}

PUT /my_index/_doc/1

{

"price":[10]

}

PUT /my_index/_doc/2

{

"price":[10,20]

}

PUT /my_index/_doc/3

{

"price":[10,20,30]

}

② 查询 price 字段的值包含指定范围内(10-20)数值的文档,range查询将会匹配包含任何一个数组元素在指定范围内的文档。

{

"took" : 15,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 3,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "1",

"_score" : 1.0,

"_source" : {

"price" : [

10

]

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"price" : [

10,

20

]

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"price" : [

10,

20,

30

]

}

}

]

}

}

10. ElasticSearch range 查询对象类型的数据?

① 索引文档,数据构造:

PUT /my_index

{

"mappings": {

"properties": {

"person": {

"type": "object",

"properties": {

"name": {

"type": "keyword"

},

"age": {

"type": "integer"

},

"address": {

"type": "keyword"

}

}

}

}

}

}

PUT /my_index/_doc/1

{

"person": {

"name": "John",

"age": 30,

"address": "123 Main St"

}

}

PUT /my_index/_doc/2

{

"person": {

"name": "Alex",

"age": 20,

"address": "123 Main St"

}

}

PUT /my_index/_doc/3

{

"person": {

"name": "Smith",

"age": 10,

"address": "123 Main St"

}

}

② 查询 person.age 的值字段在10-20之间的文档:

GET /my_index/_search

{

"query": {

"range": {

"person.age": {

"gte": 10,

"lte": 20

}

}

}

}

{

"took" : 1,

"timed_out" : false,

"_shards" : {

"total" : 1,

"successful" : 1,

"skipped" : 0,

"failed" : 0

},

"hits" : {

"total" : {

"value" : 2,

"relation" : "eq"

},

"max_score" : 1.0,

"hits" : [

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "2",

"_score" : 1.0,

"_source" : {

"person" : {

"name" : "Alex",

"age" : 20,

"address" : "123 Main St"

}

}

},

{

"_index" : "my_index",

"_type" : "_doc",

"_id" : "3",

"_score" : 1.0,

"_source" : {

"person" : {

"name" : "Smith",

"age" : 10,

"address" : "123 Main St"

}

}

}

]

}

}

11. SpringBoot整合ES实现 range 查询

GET /my_index/_search

{

"query": {

"range": {

"price": {

"gte": 10,

"lte": 20

}

}

}

}

@Slf4j

@Service

public class ElasticSearchImpl {

@Autowired

private RestHighLevelClient restHighLevelClient;

public void searchUser() throws IOException {

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

// range 查询

RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("price").gte(10).lte(20);

searchSourceBuilder.query(rangeQueryBuilder);

SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);

SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

System.out.println(searchResponse);

}

}

精彩文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。