国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ElasticSearch系列 - SpringBoot整合ES:多個精確值查詢 terms

這篇具有很好參考價值的文章主要介紹了ElasticSearch系列 - SpringBoot整合ES:多個精確值查詢 terms。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。


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ù)

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

在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ù):

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • ElasticSearch系列 - SpringBoot整合ES:查詢字段不為空的文檔 exists

    1. ElasticSearch exists 查詢是什么 在某些場景下,我們希望找到某個字段不為空的文檔,則可以用exists搜索。字段不為空的條件有: 值存在且不是 null; 值不是空數(shù)組; 值是數(shù)組,但不是 [null] 例如,查詢在字段中至少有一個非空值的文檔: 這些文檔都將匹配上面的查詢: ①

    2024年02月06日
    瀏覽(23)
  • ElasticSearch系列 - SpringBoot整合ES:查詢條件 query 和過濾條件 filter 的區(qū)別

    01. Elasticsearch 查詢條件和過濾條件的區(qū)別? Elasticsearch中的查詢條件和過濾條件都是用于搜索和過濾文檔的條件,但它們之間有一些區(qū)別。 查詢條件是用于計算文檔相關(guān)度得分的條件,它會將所有符合條件的文檔按照相關(guān)度得分從高到低排序,并返回前N個文檔。查詢條件可以

    2024年02月14日
    瀏覽(25)
  • ElasticSearch序列 - SpringBoot整合ES:范圍查詢 range

    01. ElasticSearch range查詢是什么? Elasticsearch 中的 range 查詢可以用于查詢某個字段在一定范圍內(nèi)的文檔。 range 查詢可同時提供包含和不包含這兩種范圍表達式,可供組合的選項如下: gt : 大于(greater than) lt : 小于(less than) gte : = 大于或等于(greater than or equal to) lte : = 小于

    2024年02月09日
    瀏覽(28)
  • ElasticSearch序列 - SpringBoot整合ES:根據(jù)指定的 ids 查詢

    1. ElasticSearch 根據(jù) ids 查詢文檔 ① 索引文檔,構(gòu)造數(shù)據(jù) ② 查詢文檔 id 為 1 或者 2 的文檔: 我們索引文檔時,文檔的id為整型,為什么查詢出來的文檔 id為字符串類型呢?如果我們使用字符串類型的文檔id查詢呢? 可以看到仍然可以查詢到匹配的文檔。 在Elasticsearch中,文檔

    2024年02月11日
    瀏覽(23)
  • ElasticSearch系列 - SpringBoot整合ES:ElasticSearch分析器

    1. ElasticSearch match 文本搜索的過程? Elasticsearch 的 match 查詢是一種基于文本匹配的查詢方式,它的搜索過程如下: ① 將查詢字符串分詞:Elasticsearch 會將查詢字符串分成一個個詞項(term),并去除停用詞(如“的”、“是”等常用詞匯)和標點符號等無意義的字符。 ② 構(gòu)建

    2023年04月18日
    瀏覽(25)
  • ElasticSearch系列 - SpringBoot整合ES:分析器

    1. ElasticSearch match 文本搜索的過程? Elasticsearch 的 match 查詢是一種基于文本匹配的查詢方式,它的搜索過程如下: ① 將查詢字符串分詞:Elasticsearch 會將查詢字符串分成一個個詞項(term),并去除停用詞(如“的”、“是”等常用詞匯)和標點符號等無意義的字符。 ② 構(gòu)建

    2024年02月06日
    瀏覽(24)
  • ElasticSearch系列 - SpringBoot整合ES:實現(xiàn)搜索結(jié)果排序 sort

    00. 數(shù)據(jù)準備 01. Elasticsearch 默認的排序方式是什么? ElasticSearch 默認的排序方式是相關(guān)性排序。相關(guān)性排序是根據(jù)查詢條件與文檔的匹配程度來計算每個文檔的相關(guān)性得分,然后按照得分從高到低進行排序。相關(guān)性排序是 ElasticSearch 中最常用的排序方式,因為它可以根據(jù)查詢

    2024年02月02日
    瀏覽(22)
  • 【Elasticsearch】ES精確查詢和范圍查詢,ES時間字段排序?qū)嵗珽S倒排索引介紹

    【Elasticsearch】ES精確查詢和范圍查詢,ES時間字段排序?qū)嵗?,ES倒排索引介紹

    termQuery matchQuery 模糊查詢 multiMatchQuery 多個字段模糊查詢 如果時間字段寫入時用的類型是Text,可以用“時間字段.keyword”來處理 #查詢前傳入分頁參數(shù) #分頁后拿到總記錄數(shù) 把文檔D對應(yīng)到的映射轉(zhuǎn)換為到文檔ID的映射,每個都對應(yīng)著一系列的文檔,這些文

    2024年02月15日
    瀏覽(126)
  • ElasticSearch系列 - SpringBoot整合ES:映射中定義字段的數(shù)據(jù)類型及屬性

    ElasticSearch - SpringBoot整合ES:映射定義字段的數(shù)據(jù)類型及屬性 01. ElasticSearch 搜索結(jié)果的準確性和召回率是什么? 在Elasticsearch中,搜索結(jié)果的準確性和召回率是非常重要的指標,它們反映了搜索引擎的性能和效果。以下是這兩個指標的定義和解釋: 準確性:搜索結(jié)果的準確性

    2024年02月08日
    瀏覽(25)
  • ElasticSearch系列 - SpringBoot整合ES:restHighLevelClient.count(countRequest, RequestOptions.DEFAULT)

    restHighLevelClient.count(countRequest, RequestOptions.DEFAULT) 是 Elasticsearch Java High Level REST Client 中用于執(zhí)行計數(shù)請求的方法。 具體來說,它接受兩個參數(shù): countRequest:一個 CountRequest 對象,表示計數(shù)請求的參數(shù),包括要計數(shù)的索引、查詢條件等。 RequestOptions.DEFAULT:一個 RequestOptions 對象

    2024年02月08日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包