VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • SpringBoot整合ES多个精确值查询 terms功能实现

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

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

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

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

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

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

  1. 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查询。

  1. ElasticSearch terms 查询数值类型数据
    一定要了解 term 和 terms 是包含操作,而非等值操作。 如何理解这句话呢?

在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
        }
      }
    ]
  }
}
  1. 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 查询文本类型的数据。因为会分词,查询可能会出现意想不到的结果。

  1. 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"
        }
      }
    ]
  }
}
  1. 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
        }
      }
    ]
  }
}
  1. 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"
          ]
        }
      }
    ]
  }
}
  1. 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"
          }
        }
      }
    ]
  }
}
  1. 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);
    }
}
  1. 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查询内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持

原文链接:https://hengheng.blog.csdn.net/article/details/129825049


相关教程