SpringBoot整合ES多个精确值查询 terms功能实现

 更新时间:2024年06月28日 10:14:02   作者:我一直在流浪  
这篇文章主要介绍了SpringBoot整合ES多个精确值查询 terms功能实现,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧

ElasticSearch - SpringBoot整合ES:多个精确值查询 terms

01. ElasticSearch terms 查询支持的数据类型

在Elasticsearch中,terms查询支持多种数据类型,包括:

字符串类型:可以将多个字符串值作为数组传递给terms查询,以匹配包含任何一个指定字符串值的文档。

数值类型:可以将多个数值作为数组传递给terms查询,以匹配包含任何一个指定数值的文档。

日期类型:可以将多个日期值作为数组传递给terms查询,以匹配包含任何一个指定日期值的文档。

布尔类型:可以将多个布尔值作为数组传递给terms查询,以匹配包含任何一个指定布尔值的文档。

复杂数据类型如数组类型,对象类型也可以支持,具体可以参考term查询,term查询支持的数据类型,terms查询就会支持。区别在于 term查询用于匹配一个字段中包含指定值的文档,terms查询用于匹配一个字段中包含指定值之一的文档。

02. ElasticSearch term和 terms 查询的区别

在Elasticsearch中,term和terms查询都用于匹配一个字段中包含指定值的文档,但它们之间有一些区别。

term查询用于匹配包含完全相同值的文档,而无法匹配包含部分匹配值的文档。例如,以下查询将返回包含"red"颜色的文档:

{
  "query": {
    "term": {
      "color": "red"
    }
  }
}

但是,如果要查询包含"red"或"blue"颜色的文档,应该使用terms查询,而不是term查询。例如,以下查询将返回包含"red"或"blue"颜色中任何一个的文档:

{
  "query": {
    "terms": {
      "color": ["red", "blue"]
    }
  }
}

terms查询可以将多个值作为数组传递,以匹配包含任何一个指定值的文档,而term查询只能匹配包含单个指定值的文档。因此,如果要匹配包含多个值的文档,应该使用terms查询,而如果要匹配包含单个值的文档,应该使用term查询。

03. ElasticSearch terms 查询数值类型数据

一定要了解 termterms 是包含操作,而非等值操作。 如何理解这句话呢?

在Elasticsearch中,term查询用于匹配一个字段中包含指定值的文档,terms查询用于匹配一个字段中包含指定值之一的文档。可以将多个值作为数组传递给terms查询,以匹配包含任何一个指定值的文档。

① 索引文档,构造数据:

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": {
    "terms": {
      "price": [
        "10",
        "20"
      ]
    }
  }
}
{
  "took" : 11,
  "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 terms 查询字符串型数据

terms查询用于匹配一个字段中包含指定值之一的文档。

① 索引文档,数据构造:

PUT /my_index
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}
PUT /my_index/_doc/1
{
  "tag":"tag1"
}
PUT /my_index/_doc/2
{
  "tag":"tag2"
}
PUT /my_index/_doc/3
{
  "tag":"tag3"
}

② 查询 tag 字段包含 tag1 和 tag2 的文档:

GET /my_index/_search
{
  "query": {
    "terms": {
      "tag": [
        "tag1",
        "tag2"
      ]
    }
  }
}
{
  "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" : "1",
        "_score" : 1.0,
        "_source" : {
          "tag" : "tag1"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "tag" : "tag2"
        }
      }
    ]
  }
}

不要使用term 和terms 查询文本类型的数据。因为会分词,查询可能会出现意想不到的结果。

05. ElasticSearch terms 查询日期性数据

① 索引文档,构造数据:

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:30:11” 或 “2023-03-29 10:38:11” 的文档:

{
  "took" : 672,
  "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" : {
          "createTime" : "2023-03-29 10:30:11"
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "createTime" : "2023-03-29 10:38:11"
        }
      }
    ]
  }
}

06. ElasticSearch terms 查询布尔型数据

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "flag":{
        "type": "boolean"
      }
    }
  }
}
PUT /my_index/_doc/1
{
  "flag":true
}
PUT /my_index/_doc/2
{
  "flag":true
}
PUT /my_index/_doc/3
{
  "flag":false
}

② 查询 flag 字段包含 true 或 false 的文档:

GET /my_index/_search
{
  "query": {
    "terms": {
      "flag": [
        "true",
        "false"
      ]
    }
  }
}
{
  "took" : 30,
  "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" : {
          "flag" : true
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "flag" : true
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "flag" : false
        }
      }
    ]
  }
}

07. ElasticSearch terms 查询数组类型数据

terms查询可以用于匹配一个字段中包含指定值之一的文档。对于数组类型的字段,可以将多个值作为数组传递给terms查询,以匹配包含任何一个指定值的文档。

① 索引文档,构造数据:

PUT /my_index
{
  "mappings": {
    "properties": {
      "tags":{
        "type": "keyword"
      }
    }
  }
}
PUT /my_index/_doc/1
{
  "tags":["tag1"]
}
PUT /my_index/_doc/2
{
  "tags":["tag2"]
}
PUT /my_index/_doc/3
{
  "tags":["tag1","tag2"]
}
PUT /my_index/_doc/4
{
  "tags":["tag1","tag2","tag3"]
}

② 要查询 tags 字段包含"tag1"或"tag2"的文档,可以使用以下查询:

GET /my_index/_search
{
  "query": {
    "terms": {
      "tags": [
        "tag1",
        "tag2"
      ]
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "tags" : [
            "tag1"
          ]
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "tags" : [
            "tag2"
          ]
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "tags" : [
            "tag1",
            "tag2"
          ]
        }
      },
      {
        "_index" : "my_index",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "tags" : [
            "tag1",
            "tag2",
            "tag3"
          ]
        }
      }
    ]
  }
}

08. ElasticSearch terms 查询对象型数据

① 索引文档,构造数据:

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.name 字段包含 Alex 或者 Smith 的文档:

{
  "took" : 2,
  "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"
          }
        }
      }
    ]
  }
}

09. SpringBoot 整合ES实现terms查询

GET /my_index/_search
{
  "query": {
    "terms": {
      "price": [
        "10",
        "20"
      ]
    }
  }
}
@Slf4j
@Service
public class ElasticSearchImpl {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    public void searchUser() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // terms查询
        List<Integer> prices = Arrays.asList(10,20);
        // 查询所有price字段包含10或者20的文档
        TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("price",prices);
        searchSourceBuilder.query(termsQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }
}

10. SpringBoot 整合ES实现terms查询

GET /my_index/_search
{
  "query": {
    "terms": {
      "tags": ["tag1","tag2"]
    }
  }
}
@Slf4j
@Service
public class ElasticSearchImpl {
    @Autowired
    private RestHighLevelClient restHighLevelClient;
    public void searchUser() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // terms查询
        List<String> tags = Arrays.asList("tag1","tag2");
        // 查询所有tags字段包含tag1或者tag2的文档
        TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("tags",tags);
        searchSourceBuilder.query(termsQueryBuilder);
        SearchRequest searchRequest = new SearchRequest(new String[]{"my_index"},searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(searchResponse);
    }
}

到此这篇关于SpringBoot整合ES多个精确值查询 terms功能实现的文章就介绍到这了,更多相关SpringBoot整合ES查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Sentinel热门词汇限流的实现详解

    Sentinel热门词汇限流的实现详解

    这篇文章主要介绍了使用Sentinel对热门词汇进行限流的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 浅谈Java三目运算

    浅谈Java三目运算

    本文给大家主要介绍的是java中三目运算的详细介绍,并附上2个示例,希望对大家理解三目运算能够有所帮助。
    2015-03-03
  • 浅谈spring中的default-lazy-init参数和lazy-init

    浅谈spring中的default-lazy-init参数和lazy-init

    下面小编就为大家带来一篇浅谈spring中的default-lazy-init参数和lazy-init。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • IDEA2020.1常用配置说明

    IDEA2020.1常用配置说明

    这篇文章主要介绍了IDEA2020.1常用配置说明,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • spring打包到jar包的问题解决

    spring打包到jar包的问题解决

    这篇文章主要给大家介绍了关于spring打包到jar包遇到的问题的解决方法,文中通过实例代码结束的非常详细,对大家的学习或者使用spring打包具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-08-08
  • 如何解决Project SDK is not defined问题

    如何解决Project SDK is not defined问题

    这篇文章主要介绍了如何解决Project SDK is not defined问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • SpringBoot集成Swagger构建api文档的操作

    SpringBoot集成Swagger构建api文档的操作

    这篇文章主要介绍了SpringBoot集成Swagger构建api文档的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-12-12
  • RocketMQ之Consumer整体介绍启动源码分析

    RocketMQ之Consumer整体介绍启动源码分析

    这篇文章主要为大家介绍了RocketMQ源码分析之Consumer整体介绍启动分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • SpringBoot无法使用jkd8问题的解决方法

    SpringBoot无法使用jkd8问题的解决方法

    这篇文章主要介绍了SpringBoot无法使用jkd8问题的解决方法,文中通过图文结合的形式给大家讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-12-12
  • Java集合ConcurrentHashMap详解

    Java集合ConcurrentHashMap详解

    ConcurrentHashMap 是 J.U.C 包里面提供的一个线程安全并且高效的 HashMap,所以ConcurrentHashMap 在并发编程的场景中使用的频率比较高
    2023-01-01

最新评论