SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(一)

在網(wǎng)上上我看已經(jīng)有好多關(guān)于Elasticsearch的介紹,我就不在翻來(lái)覆去講一些基本概念,大家感興趣的可以自己去找一些資料鞏固下。我這只為了顧及眾多首次接觸Elasticsearch,案例都講的很淺顯,還有就是受個(gè)人能力所限,實(shí)在寫(xiě)不出高大上的博文,各位讀者發(fā)現(xiàn)有錯(cuò)誤之處,還不要取笑我,給我指出來(lái)即可。

內(nèi)容規(guī)劃總共分為三個(gè)章節(jié)來(lái)寫(xiě),分別運(yùn)行環(huán)境構(gòu)建、利用Web應(yīng)用管理索引以及Web應(yīng)用管理數(shù)據(jù)三大塊來(lái)說(shuō)明。

具體有:
一、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(一):這些操作都是在CentOS下操作的,主要帶大家熟悉下Elasticsearch環(huán)境。

  • 1.1.下載& Linux下ElasticSearch安裝
  • 1.2.中文分詞插件IK
  • 1.3.索引
  • 1.4.如何數(shù)據(jù)管理

二、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(二):在Springboot環(huán)境下,利用JAVA環(huán)境操作索引。

  • 2.1.新增索引
  • 2.2.查詢索引
  • 2.3.刪除索引

三、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(三):在Springboot環(huán)境下,管理數(shù)據(jù)。

  • 3.1.WEB HTTP提交數(shù)據(jù)<單條提交、批量提交>
  • 3.2.WEB HTTP方式條件查詢
  • 3.3.WEB HTTP刪除數(shù)據(jù)
elasticsearch

1. Linux單機(jī)安裝

題外話,這一章節(jié)僅僅作為開(kāi)發(fā)學(xué)習(xí)來(lái)構(gòu)建基礎(chǔ)的環(huán)境,并未考慮elasticsearch的高可用性,僅說(shuō)明一些基礎(chǔ)知識(shí),帶大家有一個(gè)認(rèn)識(shí)。當(dāng)然既然是自我發(fā)揮,也就是一家之言,難免有遺漏地方,希望大家也就本著看看。

1.1. 下載&安裝

1.1.1. 環(huán)境需求

  • CentOs7
  • 內(nèi)存4G+:這個(gè)因環(huán)境而異
  • JDK11+:由于 elasticsearch 運(yùn)行需要JDK環(huán)境,我機(jī)器 JDK 版本是 11,如果 JDK 低于 9.0 會(huì)有一些問(wèn)題,下圖我也貼出來(lái)。

Java HotSpot(TM) 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

1.1.2. 下載

官方 elasticsearch 下載,下載 elasticsearch,目前最新的穩(wěn)定版本為 7.4.0 版本。

1.1.3. 安裝

  • 下載 elasticsearch,會(huì)得到一個(gè)文件 elasticsearch-X.X.X-linux-x86_64.tar.gz
  • 創(chuàng)建個(gè)文件夾 elastic
  • 再創(chuàng)建一個(gè)組,案例中我以 dev命名
  • 再創(chuàng)建一個(gè)用戶, elasticsearch不允許使用 root啟動(dòng),創(chuàng)建一個(gè)新的用戶 elastic,并為這個(gè)賬戶賦予相應(yīng)的權(quán)限來(lái)啟動(dòng) elasticsearch。
  • 解壓文件
  • elasticsearch-X.X.X-linux-x86_64.tar.gz 文件移入 elastic
  • 重新調(diào)整下權(quán)限 chown -R

[root@localhost download]$ pwd
/data/download/

[root@localhost download]$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0-linux-x86_64.tar.gz

[root@localhost download]$ cd ../app/

[root@localhost app]$ mkdir elastic

[root@localhost app]$ groupadd dev

[root@localhost app]$ useradd elastic -g dev

[root@localhost app]$ passwd elastic

[root@localhost app]$ chown -R elastic:dev elastic

[root@localhost app]$ su elastic

[elastic@localhost app]$ cd /elastic

[elastic@localhost elastic]$ cp ../../download/elasticsearch-7.4.0-linux-x86_64.tar.gz .

[elastic@localhost elastic]$ tar -zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz

[elastic@localhost elastic]$ mv elasticsearch-7.4.0/ .

1.1.4. 修改配置文件

配置文件中有很多配置項(xiàng),例如集群信息、端口等。

elasticsearch 本身為安全考慮,默認(rèn)不允許外部訪問(wèn),我們這里做演示,就需要將這個(gè)配置項(xiàng)修改掉,路徑在 config/elasticsearch.yml


[elastic@localhost elastic]$ vi config/elasticsearch.yml

修改后的效果如下:


-- 激活節(jié)點(diǎn)1
node.name: node-1

-- 允許外部IP訪問(wèn)
network.host: 0.0.0.0

-- 把這個(gè)注釋先放開(kāi)并修改
cluster.initial_master_nodes: ["node-1"]

1.1.5. 啟動(dòng)&驗(yàn)證結(jié)果

  • 啟動(dòng)
[elastic@localhost elastic]$ ./bin/elasticsearch
  • 驗(yàn)證結(jié)果

elasticsearch 默認(rèn)端口是 9200 ,打開(kāi)地址:http://192.168.147.128:9200/

elastic

注意,啟動(dòng)后可能會(huì)有兩種錯(cuò)誤導(dǎo)致啟動(dòng)失敗。

20201215104646

從中可以看出主要是打開(kāi)時(shí)數(shù)量不夠以及虛擬內(nèi)存不足。

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

解決措施

  • vi /etc/security/limits.conf
elastic hard nofile 65536
elastic soft nofile 65536

其中 elastic 為啟動(dòng) elasticsearch的用戶名。

  • vi /etc/sysctl.conf
vm.max_map_count=655360

再執(zhí)行 sysctl -p

1.1.6. 小結(jié)

從中我們看到, elasticsearch安裝本身并不困難,比較簡(jiǎn)單,修改配置文件以及注意 JDK版本。

1.2. 可視化工具

elasticsearch 的可視化工具有很多,比如 elasticsearch-head、DejavuElasticHD等。

此處選擇 ElasticHD,比較適合學(xué)習(xí)以及演示,這是一個(gè)開(kāi)源 elasticHD, Github下載地址

ElasticHD下載界面

提供Windows和linux,但是這個(gè)版本有一個(gè)弊端,就是好久沒(méi)更新,湊合著用唄。

1、unzip elasticHD_linux_amd64.zip

2、chmod -R 777 ElasticHD

3、./ElasticHD -p 0.0.0.0:9800

ElasticHD啟動(dòng)

在瀏覽器中打開(kāi) http://192.168.244.128:9800/ 就可以看到。

ElasticHD監(jiān)控
ElasticHD監(jiān)控

1.3. 中文分詞插件IK

elasticsearch 本身對(duì)中文支持不夠好,所以需要中文的分詞插件,目前主流的都用 IK。 以下這是 Google的中文詞條。

IK Analyzer是一個(gè)開(kāi)源的,基于java語(yǔ)言開(kāi)發(fā)的輕量級(jí)的中文分詞工具包。從2006年12月推出1.0版開(kāi)始, IKAnalyzer已經(jīng)推出了4個(gè)大版本。最初,它是以開(kāi)源項(xiàng)目Luence為應(yīng)用主體的,結(jié)合詞典分詞和文法分析算法的中文分詞組件。從3.0版本開(kāi)始,IK發(fā)展為面向Java的公用分詞組件,獨(dú)立于Lucene項(xiàng)目,同時(shí)提供了對(duì)Lucene的默認(rèn)優(yōu)化實(shí)現(xiàn)。在2012版本中,IK實(shí)現(xiàn)了簡(jiǎn)單的分詞歧義排除算法,標(biāo)志著IK分詞器從單純的詞典分詞向模擬語(yǔ)義分詞衍化。

1.3.1. IK安裝

安裝地址,截止當(dāng)前 IK 最新版本是 v7.4.0 ,但是我 elasticsearch 版本是 7.4所以下載與自己相對(duì)應(yīng)的版本,否則分詞插件將不能被識(shí)別。

elasticsearch-analysis-ik
  • 下載
[root@localhost download]$ wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.0/elasticsearch-analysis-ik-7.4.0.zip
  • 安裝插件

elasticsearch 安裝目錄下 找到 plugins 文件夾并創(chuàng)建一個(gè)名為 ik的目錄,將下載的 elasticsearch-analysis-ik-7.10.0.zip 移入。

[elastic@localhost elastic]$ cd plugins

[elastic@localhost plugins]$ cd mkdir ik && cd ik

[elastic@localhost ik]$ cp ../../../download/elasticsearch-analysis-ik-7.4.0.zip .

[elastic@localhost ik]$ unzip elasticsearch-analysis-ik-7.4.0.zip

完成后,將 elasticsearch 重啟,我們觀察控制臺(tái)

elasticsearch加載 ik

其中紅線處就是 elasticsearch將分詞器加載,說(shuō)明我們安裝成功。

[2020-12-15T01:19:51,151][INFO ][o.e.p.PluginsService     ] [centos8] loaded plugin [analysis-ik]

1.3.2. 分詞器

上一章節(jié)我們演示對(duì)中文分詞的安裝,下來(lái)我們開(kāi)始我們分詞器驗(yàn)證之旅。

使用crul命令,輸入下面的URL地址,驗(yàn)證分詞器是否成功。

[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json"  "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中華五千年華夏"}';
CURL驗(yàn)證分詞器

有的人喜歡 Postman,也可以。

Postman驗(yàn)證分詞器

至此我們的中文分詞器可以用啦。

1.3.3. ik_max_word和ik_smart

  • ik_max_word: 將文本按最細(xì)粒度的組合來(lái)拆分,比如會(huì)將“中華五千年華夏”拆分為“五千年、五千、五千年華、華夏、千年華夏”,總之是可能的組合;

  • ik_smart: 最粗粒度的拆分,比如會(huì)將“五千年華夏”拆分為“五千年、華夏”

當(dāng)不添加分詞類別,Elastic對(duì)于漢字默認(rèn)使用standard只是將漢字拆分成一個(gè)個(gè)的漢字,而我們ik則更加的智能,下面通過(guò)幾個(gè)案例來(lái)說(shuō)明。

1.3.3.1. ik_smart分詞

在JSON格式中添加analyzer節(jié)點(diǎn)內(nèi)容為ik_smart

[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json"  "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中華五千年華夏","analyzer": "ik_smart"}';
elastic

1.3.3.2. ik_max_word分詞

在JSON格式中添加analyzer節(jié)點(diǎn)內(nèi)容為ik_max_word

[elastic@localhost elastic]$ curl -X GET -H "Content-Type: application/json"  "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中華五千年華夏","analyzer": "ik_max_word"}';
4

1.3.4. 自定義分詞

IK 很友好,為我們提供熱更新 IK 分詞,在配置文件 {ES_HOME}/plugins/ik/config/IKAnalyzer.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>IK Analyzer 擴(kuò)展配置</comment>
    <!--用戶可以在這里配置自己的擴(kuò)展字典 -->
    <entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
     <!--用戶可以在這里配置自己的擴(kuò)展停止詞字典-->
    <entry key="ext_stopwords">custom/ext_stopword.dic</entry>
    <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展字典 -->
    <entry key="remote_ext_dict">location</entry>
    <!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典-->
    <entry key="remote_ext_stopwords">http://xxx.com/xxx.dic</entry>
</properties>

我們一般將需要自動(dòng)更新的熱詞放在一個(gè)UTF8的txt文件里,再利用 nginx ,當(dāng) .txt 文件修改時(shí),http server 會(huì)在客戶端請(qǐng)求該文件時(shí)自動(dòng)返回相應(yīng)的 Last-ModifiedETag。可以另外做一個(gè)工具來(lái)從業(yè)務(wù)系統(tǒng)提取相關(guān)詞匯,并更新這個(gè) .txt 文件。

1.4. 索引

寫(xiě)到現(xiàn)在,終于到了索引(Index)這塊啦,我們先弄清楚索引的基本概念以及索引到底是什么?

ElasticSearch 是文檔型數(shù)據(jù)庫(kù),索引(Index)定義了文檔的邏輯存儲(chǔ)和字段類型,每個(gè)索引可以包含多個(gè)文檔類型,文檔類型是文檔的集合,文檔以索引定義的邏輯存儲(chǔ)模型,比如,指定分片和副本的數(shù)量,配置刷新頻率,分配分析器等,存儲(chǔ)在索引中的海量文檔分布式存儲(chǔ)在ElasticSearch集群中。

ElasticSearch是基于Lucene框架的全文搜索引擎,將所有文檔的信息寫(xiě)入到倒排索引(Inverted Index)的數(shù)據(jù)結(jié)構(gòu)中,倒排索引建立的是索引中詞和文檔之間的映射關(guān)系,在倒排索引中,數(shù)據(jù)是面向詞(Term)而不是面向文檔的。

1.4.1. 創(chuàng)建索引

由于在ElasticSearch 7.x之后就默認(rèn)不在支持指定索引類型,所以在在elasticsearch7.x上執(zhí)行:

{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },  
    "mappings" : {
        "twitter":{
            ......
        }
    }

執(zhí)行結(jié)果則會(huì)出錯(cuò):Root mapping definition has unsupported parameters(剛開(kāi)始接觸就踩了這個(gè)坑,折煞勞資好久)。如果在6.x上執(zhí)行,則會(huì)正常執(zhí)行。

出現(xiàn)這個(gè)的原因是,elasticsearch7默認(rèn)不在支持指定索引類型,默認(rèn)索引類型是_doc,如果想改變,則配置include_type_name: true 即可(這個(gè)沒(méi)有測(cè)試,官方文檔說(shuō)的,無(wú)論是否可行,建議不要這么做,因?yàn)閑lasticsearch8后就不在提供該字段)。

https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html

1.4.1.1. 官方例子說(shuō)明


curl -X PUT "localhost:9200/twitter" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
'
  • -d指定了你的參數(shù),這里將這些參數(shù)放到了 JSON 文件中

  • settings設(shè)置內(nèi)容含義

name 價(jià)格
number_of_shards 分片數(shù)
number_of_replicas 副本數(shù)
mappings 結(jié)構(gòu)化數(shù)據(jù)設(shè)置 下面的一級(jí)屬性 是自定義的類型
properties 類型的屬性設(shè)置節(jié)點(diǎn),下面都是屬性
epoch_millis 表示時(shí)間戳

1.4.1.2. 自定義索引

  • 使用 JSON 文件創(chuàng)建索引
    使用 -d‘@your jsonFile’指定你的 JSON 文件。下邊我創(chuàng)建了一個(gè)索引名稱為 product(可自己定義)的索引。

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter?pretty=true"  -d'@prod.json'
5
  • 參數(shù)形式創(chuàng)建索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter?pretty=true"  -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },  
    "mappings" : {
            "dynamic": false,
            "properties" : {
                "productid":{
                    "type" : "long"
                },  
                "name":{
                    "type":"text",
                    "index":true,
                    "analyzer":"ik_max_word"
                },  
                "short_name":{
                    "type":"text",
                    "index":true,
                    "analyzer":"ik_max_word"
                },  
                "desc":{
                    "type":"text",
                    "index":true,
                    "analyzer":"ik_max_word"
                }
            }
    }
}
'
6

1.4.2. 查看索引

1.4.2.1. 全部索引

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/_cat/indices?v"
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   twitter scSSD1SfRCio4F77Hh8aqQ   3   2          0            0       690b           690b

8

1.4.2.2. 條件查詢

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter?pretty=true"
{
  "twitter" : {
    "aliases" : { },
    "mappings" : {
      "dynamic" : "false",
      "properties" : {
        "desc" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "name" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "productid" : {
          "type" : "long"
        },
        "short_name" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1571153735610",
        "number_of_shards" : "3",
        "number_of_replicas" : "2",
        "uuid" : "scSSD1SfRCio4F77Hh8aqQ",
        "version" : {
          "created" : "7040099"
        },
        "provided_name" : "twitter"
      }
    }
  }
}

1.4.3. 查看索引分詞器

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_analyze?pretty=true" -d'
{
  "field": "text",
  "text": "秦皇漢武."
}
'
7

1.4.4. 修改索引

1.4.5. 刪除索引

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X DELETE "http://localhost:9200/twitter?pretty=true"

1.5. 如何數(shù)據(jù)管理

1.5.1. 添加數(shù)據(jù)

  • 這里演示PUT方式為twitter索引添加數(shù)據(jù),并且指定id,應(yīng)當(dāng)注意此處的默認(rèn)類型為<font color=red>_doc</font>,還有一種就是采用POST方式添加數(shù)據(jù),并且自動(dòng)生成主鍵,本文就不再演示,請(qǐng)自行查閱相關(guān)材料。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter/_doc/1?pretty=true" -d'
{
    "productid" : 1,
    "name" : "測(cè)試添加索引產(chǎn)品名稱",
    "short_name" : "測(cè)試添加索引產(chǎn)品短標(biāo)題",
    "desc" : "測(cè)試添加索引產(chǎn)品描述"
}
'

執(zhí)行返回結(jié)果如圖,則添加數(shù)據(jù)成功。

9
  • 指定id為1,還可以加上參數(shù)op_type=create,這樣在創(chuàng)建重復(fù)id時(shí)會(huì)報(bào)錯(cuò)導(dǎo)致創(chuàng)建失敗,否則會(huì)更新該id的屬性值。
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/twitter/_doc/1?op_type=create&pretty=true" -d'
{
    "productid" : 1,
    "name" : "測(cè)試添加索引產(chǎn)品名稱",
    "short_name" : "測(cè)試添加索引產(chǎn)品短標(biāo)題",
    "desc" : "測(cè)試添加索引產(chǎn)品描述"
}
'
14

1.5.2. 基礎(chǔ)查詢

1.5.2.1. 查詢所有

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true"
10

1.5.2.2. 條件查詢

條件查詢會(huì)涉及到精確詞查詢、匹配查詢、多條件查詢、聚合查詢四種,分別為"term"、"match"、"multi_match"、"multi_match"。

  • 按找數(shù)據(jù)的名稱作為條件查詢匹配
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
    "query" : {
        "match" : { 
            "name" : "產(chǎn)品" 
        }
    }
}
'
11
  • 按找數(shù)據(jù)的標(biāo)識(shí)作為條件查詢匹配
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
    "query" : {
        "match" : { 
            "productid" : 100
        }
    }
}
'
12
  • 多條件匹配

選擇匹配desc、short_name列作為多條件

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
    "query" : {
        "multi_match" : { 
            "query":"產(chǎn)品",
            "fields" : ["desc","short_name"]
        }
    }
}
'
13
  • 當(dāng)沒(méi)有匹配任何數(shù)據(jù)適合則如下:
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
> {
>     "query" : {
>         "match" : { 
>             "productid" : 100
>         }
>     }
> }
> '
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

1.5.3. 高級(jí)條件查詢

1.5.3.1. 權(quán)重boost查詢

指定一個(gè)boost值來(lái)控制每個(gè)查詢子句的相對(duì)權(quán)重,該值默認(rèn)為1。一個(gè)大于1的boost會(huì)增加該查詢子句的相對(duì)權(quán)重。
索引映射定義的時(shí)候指定boost在elasticsearch5之后已經(jīng)棄用。建議在查詢的時(shí)候使用。

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/twitter/_search?pretty=true" -d'
{
    "query": {
        "match" : {
            "title": {
                "query": "quick brown fox",
                "boost": 2
            }
        }
    }
}
'

1.5.3.2. 過(guò)濾coerce查詢

數(shù)據(jù)不總是我們想要的,由于在轉(zhuǎn)換JSON body為真正JSON 的時(shí)候,整型數(shù)字5有可能會(huì)被寫(xiě)成字符串"5"或者浮點(diǎn)數(shù)5.0。coerce屬性可以用來(lái)清除臟數(shù)據(jù)。
一般在以下場(chǎng)景中:

  • 字符串會(huì)被強(qiáng)制轉(zhuǎn)換為整數(shù)
  • 浮點(diǎn)數(shù)被強(qiáng)制轉(zhuǎn)換為整數(shù)
1.5.3.2.1. 創(chuàng)建索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/wongs?pretty=true"  -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },  
    "mappings" : {
            "properties" : {
                "col_1":{
                    "type" : "integer"
                },  
                "col_2":{
                    "type":"integer",
                    "coerce": false
                }
            }
    }
}
'
1.5.3.2.2. 創(chuàng)建第一個(gè)數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/wongs/_doc/1?pretty=true" -d'
{
    "col_1" : "20"
}
'

結(jié)果為成功,說(shuō)明col_1列數(shù)據(jù)沒(méi)問(wèn)題。

1.5.3.2.3. 創(chuàng)建第二個(gè)數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/wongs/_doc/1?pretty=true" -d'
> {
>     "col_2" : "20"
> }
> '
{
  "error" : {
    "root_cause" : [
      {
        "type" : "mapper_parsing_exception",
        "reason" : "failed to parse field [col_2] of type [integer] in document with id '1'. Preview of field's value: '20'"
      }
    ],
    "type" : "mapper_parsing_exception",
    "reason" : "failed to parse field [col_2] of type [integer] in document with id '1'. Preview of field's value: '20'",
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Integer value passed as String"
    }
  },
  "status" : 400
}

由于不能被格式化,數(shù)據(jù)新增失敗。

1.5.3.3. copy_to

copy_to允許你創(chuàng)造自定義超級(jí)字段_all. 也就是說(shuō),多字段的取值被復(fù)制到一個(gè)字段并且取值所有字段的取值組合, 并且可以當(dāng)成一個(gè)單獨(dú)的字段查詢.
如,first_name和last_name可以合并為full_name字段。

1.5.3.3.1. 定義索引
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_copy_to?pretty=true"  -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },  
    "mappings" : {
            "properties" : {
                "first_name":{
                    "type" : "text",
                    "copy_to": "full_name"
                },  
                "last_name":{
                    "type":"text",
                    "copy_to": "full_name"
                },
                "full_name":{
                    "type": "text"
                }
            }
    }
}
'
1.5.3.3.2. 新增數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_copy_to/_doc/1?pretty=true" -d'
> {
>     "first_name" : "jack",
>     "last_name" : "Rose"
> }
> '
{
  "_index" : "idx_copy_to",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 3,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

1.5.3.3.3. 查詢數(shù)據(jù)
[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X GET "http://localhost:9200/idx_copy_to/_search?pretty=true" -d'
{
    "query" : {
        "match": {
            "full_name": { 
                "query": "jack Rose",
                "operator": "and"
            }
        }
    }
}
'

從下圖中得知first_name和 last_name字段取值都被復(fù)制到 full_name 字段。

15

1.5.3.4. doc_values

是為了加快排序、聚合操作,在建立倒排索引的時(shí)候,額外增加一個(gè)列式存儲(chǔ)映射,是一個(gè)空間換時(shí)間的做法。默認(rèn)是開(kāi)啟的,對(duì)于確定不需要聚合或者排序的字段可以關(guān)閉。

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_doc_val?pretty=true"  -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },  
    "mappings" : {
            "properties" : {
                "first_name":{
                    "type" : "text"
                },  
                "last_name":{
                    "type":"text",
                    "doc_values": false
                }
            }
    }
}
'

1.5.3.5. dynamic

默認(rèn)情況下,字段可以自動(dòng)添加到文檔或者文檔的內(nèi)部對(duì)象,elasticsearc也會(huì)自動(dòng)索引映射字段。

[elastic@localhost elastic]$ curl -H "Content-Type: application/json" -X PUT "http://localhost:9200/idx_dynamic?pretty=true"  -d'
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    },  
    "mappings" : {
            "properties" : {
                "first_name":{
                    "type" : "text"
                },  
                "last_name":{
                    "type":"text",
                    "doc_values": false
                }
            }
    }
}
'

2. 構(gòu)建集群模式

待完善

3. 源碼

Github演示源碼 ,記得給Star

Gitee演示源碼,記得給Star

4. 相關(guān)章節(jié)

一、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(一)

二、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(二)

三、SpringBoot集成Elasticsearch7.4 實(shí)戰(zhàn)(三)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Elastic+logstash+head簡(jiǎn)單介紹 一. 概述 ElasticSearch是一個(gè)基于Lucene的...
    柒月失凄閱讀 4,658評(píng)論 0 4
  • 本文是對(duì)ElasticSearch中文分詞學(xué)習(xí)的一個(gè)知識(shí)總結(jié),包括如下章節(jié)的內(nèi)容: 基本概念 ik分詞器的安裝 i...
    我是老薛閱讀 30,374評(píng)論 6 15
  • 內(nèi)容規(guī)劃總共分為三個(gè)章節(jié)來(lái)寫(xiě),分別運(yùn)行環(huán)境構(gòu)建、利用Web應(yīng)用管理索引以及Web應(yīng)用管理數(shù)據(jù)三大塊來(lái)說(shuō)明。 具體有...
    孤山之王閱讀 49,169評(píng)論 21 53
  • 1 Elasticsearch 簡(jiǎn)介 1.1 什么是 Elasticsearch? Elasticsearch是一...
    djm猿閱讀 499評(píng)論 0 0
  • 區(qū)分紫砂壺的精致程度和品位 第一,要看壺的造型美不美,是否形神兼?zhèn)洹V挥袃?yōu)美的造型才能打動(dòng)收藏者和使用者的心,讓...
    天下_e142閱讀 124評(píng)論 0 0

友情鏈接更多精彩內(nèi)容