ElasticSearch操作索引【Index】与数据新增、查询

查询所有索引:
GET _search
{
"query": {
"match_all": {}
}
}

展示结果
{
"took": 490,
"timed_out": false,
"_shards": {
"total": 39,
"successful": 39,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 10000,
"relation": "gte"
},
"max_score": 1,
"hits": [
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "nJS1tJMBmKrfXV2vs9mQ",
"_score": 1,
"_source": {
"host": "175.27.167.4",
"@version": "1",
"port": 54022,
"tags": [
"_jsonparsefailure"
],
"message": ":\x93Pm\xE9DpPR\xDA\xDFK \x8C\xD2\u003C\xDFu\xA2qF|XSG\u0015@҆4\xCB\xDC\xE4\xD1\xE3\xA69Be\x9Aҝ\xCA\xD8j\u0000&\xC0+\xC0/\xC0,\xC00̨̩\xC0\t\xC0\u0013\xC0",
"@timestamp": "2024-12-11T07:53:47.049Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "m5S1tJMBmKrfXV2vs9mQ",
"_score": 1,
"_source": {
"host": "175.27.167.4",
"@version": "1",
"port": 54022,
"tags": [
"_jsonparsefailure"
],
"message": "\u0016\u0003\u0001\u0000\xF2\u0001\u0000\u0000\xEE\u0003\u0003\xDF\xC4p\xF1\u0006\xFD\xC1\xA2\xEAo.\u0001v\xF6\xBFʰƧ",
"@timestamp": "2024-12-11T07:53:47.048Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "npS1tJMBmKrfXV2vs9mQ",
"_score": 1,
"_source": {
"host": "175.27.167.4",
"@version": "1",
"port": 54022,
"tags": [
"_jsonparsefailure"
],
"message": "\u0013\u0001\u0013\u0002\u0013\u0003\u0001\u0000\u0000\u0000\u0005\u0000\u0005\u0001\u0000\u0000\u0000\u0000\u0000",
"@timestamp": "2024-12-11T07:53:47.050Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "pZS1tJMBmKrfXV2v2tnF",
"_score": 1,
"_source": {
"host": "175.27.167.4",
"@version": "1",
"port": 58378,
"tags": [
"_jsonparsefailure"
],
"message": "Connection: close\r",
"@timestamp": "2024-12-11T07:53:57.087Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "ppS1tJMBmKrfXV2v2tnF",
"_score": 1,
"_source": {
"host": "175.27.167.4",
"@version": "1",
"port": 58378,
"tags": [
"_jsonparsefailure"
],
"message": "Accept-Encoding: gzip\r",
"@timestamp": "2024-12-11T07:53:57.087Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "XZRBs5MBmKrfXV2vv9kL",
"_score": 1,
"_source": {
"host": "59.53.80.203",
"@version": "1",
"port": 37454,
"tags": [
"_jsonparsefailure"
],
"message": "\u0003\u0000\u0000*%\xE0\u0000\u0000\u0000\u0000\u0000Cookie: mstshash=nmap\r",
"@timestamp": "2024-12-11T01:07:30.490Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "nZS1tJMBmKrfXV2vs9mQ",
"_score": 1,
"_source": {
"host": "175.27.167.4",
"@version": "1",
"port": 54022,
"tags": [
"_jsonparsefailure"
],
"message": "\xC0\u0014\u0000\x9C\u0000\x9D\u0000/\u00005\xC0\u0012\u0000",
"@timestamp": "2024-12-11T07:53:47.050Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "X5RBs5MBmKrfXV2v5dmN",
"_score": 1,
"_source": {
"host": "59.53.80.203",
"@version": "1",
"port": 39934,
"tags": [
"_jsonparsefailure"
],
"message": "\u0003\u0000\u0000\v\u0006\xE0\u0000\u0000\u0000\u0000\u0000",
"@timestamp": "2024-12-11T01:07:40.455Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "YpRCs5MBmKrfXV2vINla",
"_score": 1,
"_source": {
"host": "59.53.80.203",
"@version": "1",
"port": 33624,
"tags": [
"_jsonparsefailure"
],
"message": "OPTIONS / HTTP/1.0\r",
"@timestamp": "2024-12-11T01:07:55.508Z"
}
},
{
"_index": "%{[spring.application.name]}-2024.12.11",
"_type": "_doc",
"_id": "YJRBs5MBmKrfXV2v-dkw",
"_score": 1,
"_source": {
"host": "59.53.80.203",
"@version": "1",
"port": 36646,
"tags": [
"_jsonparsefailure"
],
"message": "GET / HTTP/1.0\r",
"@timestamp": "2024-12-11T01:07:45.483Z"
}
}
]
}
}

索引的应用#
一切准备就绪之后我们开始来使用起来,体验 ElasticSearch 的世界。首先,我们来查看我们的所有索引信息:

GET _search
{
"query": {
"match_all": {}
}
}
得到如下结果信息:

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "5.6.0",
"_score": 1,
"_source": {
"buildNum": 15523
}
}
]
}
}
可以发现,当前只有一个索引,是.kibana,当然这不是我们自己的,这是kibana的。

创建第一个简单索引#
NBA的新的赛季又开始了,我相信大部分人有精彩比赛的时候还是会去关注的,我们创建一个 NBA 球队的索引,开始我们的学习之路,索引名称需是小写。

PUT nba
{
"settings":{
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings":{
"nba":{
"properties":{
"name_cn":{
"type":"text"
},
"name_en":{
"type":"text"
},
"gymnasium":{
"type":"text"
},
"topStar":{
"type":"text"
},
"championship":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss|| yyy-MM-dd||epoch_millis"
}
}
}
}
}
字段说明:

字段名称 字段说明
nba 索引
number_of_shards 分片数
number_of_replicas 副本数
name_cn 球队中文名
name_en 球队英文名
gymnasium 球馆名称
championship 总冠军次数
topStar 当家球星
date 加入NBA年份
如果格式书写正确,我们会得到如下返回信息,表示创建成功

{
"acknowledged": true,
"shards_acknowledged": true,
"index": "nba"
}
新增索引数据#
索引创建完成之后,我们往索引中加入球队数据,1,2,3 是我们指定的 ID,如果不写 ES 会默认ID。

其实我们可以不创建上面的索引 mapping 直接推送数据,但是这样 ES 会根据数据信息自动为我们设定字段类型,这会造成索引信息不准确的风险。

PUT /nba/nba/1
{
"name_en":"San Antonio Spurs SAS",
"name_cn":"圣安东尼安马刺",
"gymnasium":"AT&T中心球馆",
"championship": 5,
"topStar":"蒂姆·邓肯",
"date":"1995-04-12"
}

PUT /nba/nba/2
{
"name_en":"Los Angeles Lakers",
"name_cn":"洛杉矶湖人",
"gymnasium":"斯台普斯中心球馆",
"championship": 16,
"topStar":"科比·布莱恩特",
"date":"1947-05-12"
}

PUT /nba/nba/3
{
"name_en":"Golden State Warriors",
"name_cn":"金州勇士队",
"gymnasium":"甲骨文球馆",
"championship": 6,
"topStar":"斯蒂芬·库里",
"date":"1949-06-13"
}

PUT /nba/nba/4
{
"name_en":"Miami Heat",
"name_cn":"迈阿密热火队",
"gymnasium":"美国航空球场",
"championship": 3,
"topStar":"勒布朗·詹姆斯",
"date":"1988-06-13"
}

PUT /nba/nba/5
{
"name_en":"Cleveland Cavaliers",
"name_cn":"克利夫兰骑士队",
"gymnasium":"速贷球馆",
"championship": 1,
"topStar":"勒布朗·詹姆斯",
"date":"1970-06-13"
}
索引数据 PUT 成功,会返回如下信息

{
"_index": "nba",
"_type": "nba",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}

查询索引数据#
上面新增完数据之后,这时候我们再执行开始的 MATCH_ALL ,就会发现我们自己的索引信息也在查询的结果里面了,只是查询的结果是全部信息,其中包括索引、分片和副本的信息,内容比较多。我们可单独查询自己需要的索引信息。

Elasticsearch 提供丰富且灵活的查询语言叫做 **DSL 查询 (Query DSL) **,它允许你构建更加复杂、强大的搜索。

1.匹配查询 match,match_all

我们尝试一个最简单的搜索全部员工的请求:

查询全部球队的信息

GET /nba/nba/_search
{
"query": {
"match_all": {}
}
}
得到的查询结果如下

{
"took": 4,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "nba",
"_type": "nba",
"_id": "2",
"_score": 1,
"_source": {
"name_en": "Los Angeles Lakers",
"name_cn": "洛杉矶湖人",
"gymnasium": "斯台普斯中心球馆",
"championship": 16,
"topStar": "科比·布莱恩特",
"date": "1947-05-12"
}
},
{
"_index": "nba",
"_type": "nba",
"_id": "1",
"_score": 1,
"_source": {
"name_en": "San Antonio Spurs SAS",
"name_cn": "圣安东尼安马刺",
"gymnasium": "AT&T中心球馆",
"championship": 5,
"topStar": "蒂姆·邓肯",
"date": "1995-04-12"
}
},
{
"_index": "nba",
"_type": "nba",
"_id": "3",
"_score": 1,
"_source": {
"name_en": "Golden State Warriors",
"name_cn": "金州勇士队",
"gymnasium": "甲骨文球馆",
"championship": 6,
"topStar": "斯蒂芬·库里",
"date": "1949-06-13"
}
···
}
]
}
}
响应的数据结果分为两部分

{
----------------first part--------------------
"took": 0,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
---------------second part---------------------
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
第一部分为:分片副本信息,第二部分 hits 包装的为查询的数据集。

注意:
响应内容不仅会告诉我们哪些文档被匹配到,而且这些文档内容完整的被包含在其中—我们在给用户展示搜 索结果时需要用到的所有信息都有了。

查询英文名称为:"Golden State Warriors" 的球队信息

GET /nba/nba/_search
{
"query": {
"match": {
"name_en": "Golden State Warriors"
}
}
}
可得到的查询结果为:

{
"took": 6,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.9646256,
"hits": [
{
"_index": "nba",
"_type": "nba",
"_id": "3",
"_score": 1.9646256,
"_source": {
"name_en": "Golden State Warriors",
"name_cn": "金州勇士队",
"gymnasium": "甲骨文球馆",
"championship": 6,
"topStar": "斯蒂芬·库里",
"date": "1949-06-13"
}
}
]
}
}
2.过滤查询 Filter

我们让搜索变的复杂一些。我们想要找到当家球星是勒布朗·詹姆斯,但是我们只想得到总冠军多余1次的球队。我们的语句将做一些改变用来添加过滤器(filter),它允许我们有效的执行一个结构化搜索:

GET /nba/nba/_search
{
"query": {
"bool": {
"filter": {
"range": {
"championship": {
"gt": 1
}
}
},
"must": {
"match": {
"topStar": "勒布朗·詹姆斯"
}
}
}
}
}
我们发现每次查询,查询结果里面都有一个 _score字段,一般Elasticsearch根据相关评分排序,相关评分是根据文档与语句的匹配度来得出, _score值越高说明匹配度越高。

Elasticsearch如何进行全文字段搜索且首先返回相关性性最大的结果。相关性(relevance)概念在Elasticsearch中非常重要,而这也是它与传统关系型数据库中记录只有匹配和不匹配概念最大的不同。

posted @ 2024-12-17 15:07  vello  阅读(681)  评论(0)    收藏  举报