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

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

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

具體有:
一鹅经、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é)僅僅作為開發(fā)學(xué)習(xí)來構(gòu)建基礎(chǔ)的環(huán)境阀圾,并未考慮elasticsearch的高可用性,僅說明一些基礎(chǔ)知識狗唉,帶大家有一個認(rèn)識初烘。當(dāng)然既然是自我發(fā)揮,也就是一家之言分俯,難免有遺漏地方肾筐,希望大家也就本著看看。

1.1. 下載&安裝

1.1.1. 環(huán)境需求

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

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镊逝,會得到一個文件 elasticsearch-X.X.X-linux-x86_64.tar.gz
  • 創(chuàng)建個文件夾 elastic
  • 再創(chuàng)建一個組壮啊,案例中我以 dev命名
  • 再創(chuàng)建一個用戶, elasticsearch不允許使用 root啟動撑蒜,創(chuàng)建一個新的用戶 elastic,并為這個賬戶賦予相應(yīng)的權(quán)限來啟動 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)不允許外部訪問骂远,我們這里做演示,就需要將這個配置項(xiàng)修改掉腰根,路徑在 config/elasticsearch.yml


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

修改后的效果如下:


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

-- 允許外部IP訪問
network.host: 0.0.0.0

-- 把這個注釋先放開并修改
cluster.initial_master_nodes: ["node-1"]

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

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

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

elastic

注意,啟動后可能會有兩種錯誤導(dǎo)致啟動失敗额嘿。

20201215104646

從中可以看出主要是打開時數(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 為啟動 elasticsearch的用戶名。

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

再執(zhí)行 sysctl -p

1.1.6. 小結(jié)

從中我們看到册养, elasticsearch安裝本身并不困難东帅,比較簡單,修改配置文件以及注意 JDK版本球拦。

1.2. 可視化工具

elasticsearch 的可視化工具有很多靠闭,比如 elasticsearch-headDejavu坎炼、ElasticHD等愧膀。

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

ElasticHD下載界面

提供Windows和linux扇调,但是這個版本有一個弊端,就是好久沒更新抢肛,湊合著用唄狼钮。

1莲镣、unzip elasticHD_linux_amd64.zip

2瑞侮、chmod -R 777 ElasticHD

3半火、./ElasticHD -p 0.0.0.0:9800

ElasticHD啟動

在瀏覽器中打開 http://192.168.244.128:9800/ 就可以看到。

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

1.3. 中文分詞插件IK

elasticsearch 本身對中文支持不夠好,所以需要中文的分詞插件消痛,目前主流的都用 IK秩伞。 以下這是 Google的中文詞條稠歉。

IK Analyzer是一個開源的带饱,基于java語言開發(fā)的輕量級的中文分詞工具包勺疼。從2006年12月推出1.0版開始酪耕, IKAnalyzer已經(jīng)推出了4個大版本迂烁。最初藏斩,它是以開源項(xiàng)目Luence為應(yīng)用主體的狰域,結(jié)合詞典分詞和文法分析算法的中文分詞組件兆览。從3.0版本開始,IK發(fā)展為面向Java的公用分詞組件驶睦,獨(dú)立于Lucene項(xiàng)目匿醒,同時提供了對Lucene的默認(rèn)優(yōu)化實(shí)現(xiàn)场航。在2012版本中,IK實(shí)現(xiàn)了簡單的分詞歧義排除算法孩饼,標(biāo)志著IK分詞器從單純的詞典分詞向模擬語義分詞衍化镀娶。

1.3.1. IK安裝

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

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)建一個名為 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 重啟陪每,我們觀察控制臺

elasticsearch加載 ik

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

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

1.3.2. 分詞器

上一章節(jié)我們演示對中文分詞的安裝,下來我們開始我們分詞器驗(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ì)粒度的組合來拆分字旭,比如會將“中華五千年華夏”拆分為“五千年遗淳、五千屈暗、五千年華、華夏弃甥、千年華夏”潘飘,總之是可能的組合;

  • ik_smart: 最粗粒度的拆分艰毒,比如會將“五千年華夏”拆分為“五千年丑瞧、華夏”

當(dāng)不添加分詞類別,Elastic對于漢字默認(rèn)使用standard只是將漢字拆分成一個個的漢字西乖,而我們ik則更加的智能薄腻,下面通過幾個案例來說明。

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>

我們一般將需要自動更新的熱詞放在一個UTF8的txt文件里,再利用 nginx 直奋,當(dāng) .txt 文件修改時脚线,http server 會在客戶端請求該文件時自動返回相應(yīng)的 Last-ModifiedETag邮绿〈可以另外做一個工具來從業(yè)務(wù)系統(tǒng)提取相關(guān)詞匯,并更新這個 .txt 文件酱鸭。

1.4. 索引

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

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

ElasticSearch是基于Lucene框架的全文搜索引擎,將所有文檔的信息寫入到倒排索引(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é)果則會出錯:Root mapping definition has unsupported parameters(剛開始接觸就踩了這個坑俩功,折煞勞資好久)椿肩。如果在6.x上執(zhí)行厂榛,則會正常執(zhí)行。

出現(xiàn)這個的原因是,elasticsearch7默認(rèn)不在支持指定索引類型,默認(rèn)索引類型是_doc四敞,如果想改變硬纤,則配置include_type_name: true 即可(這個沒有測試腮鞍,官方文檔說的,無論是否可行祝懂,建議不要這么做,因?yàn)閑lasticsearch8后就不在提供該字段)教届。

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

1.4.1.1. 官方例子說明


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

1.4.1.2. 自定義索引

  • 使用 JSON 文件創(chuàng)建索引
    使用 -d‘@your jsonFile’指定你的 JSON 文件合住。下邊我創(chuàng)建了一個索引名稱為 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ù),并且自動生成主鍵掌实,本文就不再演示滋将,請自行查閱相關(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" : "測試添加索引產(chǎn)品名稱",
    "short_name" : "測試添加索引產(chǎn)品短標(biāo)題",
    "desc" : "測試添加索引產(chǎn)品描述"
}
'

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

9
  • 指定id為1父丰,還可以加上參數(shù)op_type=create,這樣在創(chuàng)建重復(fù)id時會報錯導(dǎo)致創(chuàng)建失敗掘宪,否則會更新該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" : "測試添加索引產(chǎn)品名稱",
    "short_name" : "測試添加索引產(chǎn)品短標(biāo)題",
    "desc" : "測試添加索引產(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. 條件查詢

條件查詢會涉及到精確詞查詢、匹配查詢魏滚、多條件查詢镀首、聚合查詢四種,分別為"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)識作為條件查詢匹配
[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)沒有匹配任何數(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. 高級條件查詢

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

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

[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. 過濾coerce查詢

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

  • 字符串會被強(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)建第一個數(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é)果為成功聂示,說明col_1列數(shù)據(jù)沒問題。

1.5.3.2.3. 創(chuàng)建第二個數(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)造自定義超級字段_all. 也就是說,多字段的取值被復(fù)制到一個字段并且取值所有字段的取值組合, 并且可以當(dāng)成一個單獨(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

是為了加快排序、聚合操作编曼,在建立倒排索引的時候豆巨,額外增加一個列式存儲映射,是一個空間換時間的做法掐场。默認(rèn)是開啟的往扔,對于確定不需要聚合或者排序的字段可以關(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)情況下熊户,字段可以自動添加到文檔或者文檔的內(nèi)部對象萍膛,elasticsearc也會自動索引映射字段。

[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)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市北苟,隨后出現(xiàn)的幾起案子拟赊,更是在濱河造成了極大的恐慌,老刑警劉巖粹淋,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異瑟慈,居然都是意外死亡桃移,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門葛碧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來借杰,“玉大人,你說我怎么就攤上這事进泼≌岷猓” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵乳绕,是天一觀的道長绞惦。 經(jīng)常有香客問我,道長洋措,這世上最難降的妖魔是什么济蝉? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上王滤,老公的妹妹穿的比我還像新娘贺嫂。我一直安慰自己,他們只是感情好雁乡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布第喳。 她就那樣靜靜地躺著,像睡著了一般踱稍。 火紅的嫁衣襯著肌膚如雪曲饱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天寞射,我揣著相機(jī)與錄音渔工,去河邊找鬼。 笑死桥温,一個胖子當(dāng)著我的面吹牛引矩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播侵浸,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼旺韭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了掏觉?” 一聲冷哼從身側(cè)響起区端,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎澳腹,沒想到半個月后织盼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡酱塔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年沥邻,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片羊娃。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡唐全,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蕊玷,到底是詐尸還是另有隱情邮利,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布垃帅,位于F島的核電站延届,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏贸诚。R本人自食惡果不足惜祷愉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一窗宦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧二鳄,春花似錦赴涵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至欺殿,卻和暖如春寄纵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背脖苏。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工程拭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人棍潘。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓恃鞋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親亦歉。 傳聞我的和親對象是個殘疾皇子恤浪,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容

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