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 查询数值类型数据
一定要了解 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 } } ] } }
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查询内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论