ElasticSearch - SpringBoot整合ES:多個精確值查詢 terms
01. ElasticSearch terms 查詢支持的數(shù)據(jù)類型
在Elasticsearch中,terms查詢支持多種數(shù)據(jù)類型,包括:
字符串類型:可以將多個字符串值作為數(shù)組傳遞給terms查詢,以匹配包含任何一個指定字符串值的文檔。
數(shù)值類型:可以將多個數(shù)值作為數(shù)組傳遞給terms查詢,以匹配包含任何一個指定數(shù)值的文檔。
日期類型:可以將多個日期值作為數(shù)組傳遞給terms查詢,以匹配包含任何一個指定日期值的文檔。
布爾類型:可以將多個布爾值作為數(shù)組傳遞給terms查詢,以匹配包含任何一個指定布爾值的文檔。
復(fù)雜數(shù)據(jù)類型如數(shù)組類型,對象類型也可以支持,具體可以參考term查詢,term查詢支持的數(shù)據(jù)類型,terms查詢就會支持。區(qū)別在于 term查詢用于匹配一個字段中包含指定值的文檔,terms查詢用于匹配一個字段中包含指定值之一的文檔。
02. ElasticSearch term和 terms 查詢的區(qū)別
在Elasticsearch中,term和terms查詢都用于匹配一個字段中包含指定值的文檔,但它們之間有一些區(qū)別。
term查詢用于匹配包含完全相同值的文檔,而無法匹配包含部分匹配值的文檔。例如,以下查詢將返回包含"red"顏色的文檔:
{
"query": {
"term": {
"color": "red"
}
}
}
但是,如果要查詢包含"red"或"blue"顏色的文檔,應(yīng)該使用terms查詢,而不是term查詢。例如,以下查詢將返回包含"red"或"blue"顏色中任何一個的文檔:
{
"query": {
"terms": {
"color": ["red", "blue"]
}
}
}
terms查詢可以將多個值作為數(shù)組傳遞,以匹配包含任何一個指定值的文檔,而term查詢只能匹配包含單個指定值的文檔。因此,如果要匹配包含多個值的文檔,應(yīng)該使用terms查詢,而如果要匹配包含單個值的文檔,應(yīng)該使用term查詢。
03. ElasticSearch terms 查詢數(shù)值類型數(shù)據(jù)
一定要了解 term
和 terms
是包含操作,而非等值操作。 如何理解這句話呢?
在Elasticsearch中,term查詢用于匹配一個字段中包含指定值的文檔,terms查詢用于匹配一個字段中包含指定值之一的文檔??梢詫⒍鄠€值作為數(shù)組傳遞給terms查詢,以匹配包含任何一個指定值的文檔。
① 索引文檔,構(gòu)造數(shù)據(jù):
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 查詢字符串型數(shù)據(jù)
terms查詢用于匹配一個字段中包含指定值之一的文檔。
① 索引文檔,數(shù)據(jù)構(gòu)造:
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 查詢文本類型的數(shù)據(jù)。因為會分詞,查詢可能會出現(xiàn)意想不到的結(jié)果。
05. ElasticSearch terms 查詢?nèi)掌谛詳?shù)據(jù)
① 索引文檔,構(gòu)造數(shù)據(jù):
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 查詢布爾型數(shù)據(jù)
① 索引文檔,構(gòu)造數(shù)據(jù):
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 查詢數(shù)組類型數(shù)據(jù)
terms查詢可以用于匹配一個字段中包含指定值之一的文檔。對于數(shù)組類型的字段,可以將多個值作為數(shù)組傳遞給terms查詢,以匹配包含任何一個指定值的文檔。
① 索引文檔,構(gòu)造數(shù)據(jù):
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 查詢對象型數(shù)據(jù)
① 索引文檔,構(gòu)造數(shù)據(jù):文章來源:http://www.zghlxwxcb.cn/news/detail-576453.html
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 的文檔:文章來源地址http://www.zghlxwxcb.cn/news/detail-576453.html
{
"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實現(xiàn)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實現(xiàn)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);
}
}
到了這里,關(guān)于ElasticSearch系列 - SpringBoot整合ES:多個精確值查詢 terms的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!