es请求方式调用

es · 浏览次数 : 0

小编点评

| **Field** | **Value** | |---|---| | id | 1005 | | type | doc | | source | { name: "华为", price: 3999, url: "htp123" } | index | shopping | | type | doc | | id | 1002 | | type | doc | | index | shopping | | type | doc | | id | 1003 | | type | doc | | index | shopping | | type | doc | | id | 1004 | | type | doc | | index | shopping | | type | doc | | id | 1006 | | type | doc | | index | shopping | | type | doc | | id | 1001 | | type | doc | | index | shopping | | type | doc | | id | 1001 | | type | doc | | index | shopping | | type | doc |

正文

Es基础

关系:

ElasticSearch-> mysql

index (索引)-> 数据库

Documents(文档) -> row(行)

Fileds(字段)-> column

正排索引 id 内容,类似表格

倒排索引 :keywords : ids

Postman访问实例

创建索引:创建库

ip/索引名

请求路径:PUT http://127.0.0.1:9200/shopping
请求体:none

成功:

{
	"acknowledged": true,
	"shards_acknowledged": true,
	"index": "shopping"
}
查询当前存在索引:

ip/_cat/indices?v=

请求路径:GET  http://127.0.0.1:9200/_cat/indices?v=
请求体:none

成功:

health status index            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .geoip_databases 5vZtZiLXTw-ZnE-gxFK4RA   1   0         33           35     34.1mb         34.1mb
yellow open   user1            XvuPXH4GR3qu9kYgI1vMTg   1   1          0            0       226b           226b
yellow open   product          OuJtZ2GNQjaANql9jHIhdw   1   1          0            0       226b           226b
yellow open   user             84VHenNTTtaJyKUQasAZXA   1   1          3            0      4.8kb          4.8kb
yellow open   shopping         vqraISHNSFioVa4h58y_4w   1   1         10            6       28kb           28kb

创建文档:添加行数据

ip/索引名/_doc

请求路径: POST  http://127.0.0.1:9200/shopping/_doc
请求体:
{
    "name": "小米",
    "price": 1999,
    "url": "htp12344"
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "0i0_WI8Bs7gKHbbSH-sS",
	"_version": 1,
	"result": "created",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 20,
	"_primary_term": 7
}

指定id创建文档:

ip/索引名/_doc/id

请求路径:POST http://127.0.0.1:9200/shopping/_doc/1006
请求体:
{
    "name": "魅族21",
    "price": 2999,
    "url": "htp12344"
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1006",
	"_version": 3,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 21,
	"_primary_term": 7
}
查询单挑索引:查询单条数据

ip/索引/_doc/id

请求路径: GET  http://127.0.0.1:9200/shopping/_doc/1001
请求体:none

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1001",
	"_version": 6,
	"_seq_no": 7,
	"_primary_term": 1,
	"found": true,
	"_source": {
		"name": "小米",
		"price": 3999,
		"url": "htp123"
	}
}
查询文档列表:列表查询数据

ip/索引/_search

请求路径: GET  http://127.0.0.1:9200/shopping/_search
请求体:none

成功:

{
	"took": 2,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "F2NHrY4BJgxAo-jxuDZv",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"name": "华为",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	}
}
条件查询:拆词查询
请求路径: GET  http://127.0.0.1:9200/shopping/_search

{
  "query": {
 "match": { //拆词单个查询
   "name": "华"
   }
  }
}

成功:

{
	"took": 6,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1.3340157,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1.3340157,
				"_source": {
					"name": "华为",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1.3340157,
				"_source": {
					"name": "华为",
					"price": 9999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1.1120224,
				"_source": {
					"name": "华为1",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	}
}
全词匹配查询高亮查询:
请求路径: GET  http://127.0.0.1:9200/shopping/_search
请求体:
{
    "query": {
        "match_phrase": { //全词匹配查询
            "name": "华为1"
        }
    },
    "highlight": { //高亮显示这个字段
        "fields": {
            "name": {}
        }
    }
}

成功:

{
	"took": 58,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 1,
			"relation": "eq"
		},
		"max_score": 4.0541162,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 4.0541162,
				"_source": {
					"name": "华为1",
					"price": 9999,
					"url": "htp123"
				},
				"highlight": {
					"name": [
						"<em>华</em><em>为</em><em>1</em>"
					]
				}
			}
		]
	}
}
多条件范围查询:
请求路径: GET  http://127.0.0.1:9200/shopping/_search

请求体:
{
    "query": {
        "bool": {
            "must": [ //should表示或,must表示并
                {
                    "match": {
                        "name": "小米"
                    }
                },
                {
                    "match": {
                        "price": 3999
                    }
                }
            ],
            "filter": {
                "range": {
                    "price": {
                        "lt": 5000
                    }
                }
            }
        }
    }
}

成功:

{
	"took": 18,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 2.4093566,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jFvSmY4BzKCXziUqmd-Q",
				"_score": 2.4093566,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jVvYmY4BzKCXziUqX9-H",
				"_score": 2.4093566,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			}
		]
	}
}
聚合查询:
请求路径: GET  http://127.0.0.1:9200/shopping/_search

请求体:
{
    "aggs": {
        "price_group": { //随意取名
            "terms": { //分组
                "field": "price" //分组字段
            }
        }
    }
}
{
	"took": 38,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 10,
			"relation": "eq"
		},
		"max_score": 1,
		"hits": [
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "F2NHrY4BJgxAo-jxuDZv",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jFvSmY4BzKCXziUqmd-Q",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "jVvYmY4BzKCXziUqX9-H",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "0i0_WI8Bs7gKHbbSH-sS",
				"_score": 1,
				"_source": {
					"name": "魅族",
					"price": 1999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1005",
				"_score": 1,
				"_source": {
					"name": "华为",
					"price": 3999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1002",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 1999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1003",
				"_score": 1,
				"_source": {
					"name": "小米",
					"price": 999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1004",
				"_score": 1,
				"_source": {
					"name": "华为",
					"price": 9999,
					"url": "htp123"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1006",
				"_score": 1,
				"_source": {
					"name": "魅族21",
					"price": 2999,
					"url": "htp12344"
				}
			},
			{
				"_index": "shopping",
				"_type": "_doc",
				"_id": "1001",
				"_score": 1,
				"_source": {
					"name": "华为1",
					"price": 9999,
					"url": "htp123"
				}
			}
		]
	},
	"aggregations": {
		"price_group": {
			"doc_count_error_upper_bound": 0,
			"sum_other_doc_count": 0,
			"buckets": [
				{
					"key": 1999,
					"doc_count": 3
				},
				{
					"key": 3999,
					"doc_count": 3
				},
				{
					"key": 9999,
					"doc_count": 2
				},
				{
					"key": 999,
					"doc_count": 1
				},
				{
					"key": 2999,
					"doc_count": 1
				}
			]
		}
	}
}
文档修改:修改行数据

ip/索引/_doc/id

请求路径: PUT  http://127.0.0.1:9200/shopping/_doc/1001
请求体:
{
    "name": "小米",
    "price": 9999,
    "url": "htp123"
}

成功:

{
  "_index": "shopping",
  "_type": "_doc",
  "_id": "1001",
  "_version": 7,
  "result": "updated",
  "_shards": {
   "total": 2,
   "successful": 1,
  "failed": 0
  },
  "_seq_no": 22,
  "_primary_term": 7
}
局部修改文档(某列)

ip/索引/_update/id

请求路径:PUT  http://127.0.0.1:9200/shopping/_update/1001
请求体:
{
    "doc": {
        "name": "华为1"
    }
}

成功:

{
	"_index": "shopping",
	"_type": "_doc",
	"_id": "1001",
	"_version": 8,
	"result": "updated",
	"_shards": {
		"total": 2,
		"successful": 1,
		"failed": 0
	},
	"_seq_no": 23,
	"_primary_term": 7
}

删除文档:

请求路径:DELETE  http://127.0.0.1:9200/shopping/_doc/1001
请求体:none

与es请求方式调用相似的内容:

es请求方式调用

Es基础 关系: ElasticSearch-> mysql index (索引)-> 数据库 Documents(文档) -> row(行) Fileds(字段)-> column 正排索引 id 内容,类似表格 倒排索引 :keywords : ids Postman访问实例 创建索引:创建库

深入了解Elasticsearch搜索引擎篇:倒排索引、架构设计与优化策略

首先,我们介绍了Elasticsearch(ES)的倒排索引,这是一种用于快速检索的数据结构。其次,我们了解了ES集群的架构,包括主节点、数据节点和协调节点的功能和作用。然后,我们探讨了中文分词器的选择,其中包括IK、HanLP和Jieba等常用的分词工具。接着,我们解释了写入数据和查询数据的工作原理,包括请求的分配和预处理,数据的存储和查询结果的处理过程。最后,我们讨论了ES部署的优化方法,包括调整JVM内存、分片布局和数量、节点身份设计以及配置Ingest节点等方面的策略。

java与es8实战之六:用JSON创建请求对象(比builder pattern更加直观简洁)

向ES发送请求时,如何创建请求对象呢?官方推荐的builder patter,在面对复杂的请求对象结构时还好用吗?有没有更加直观简洁的方法,尽在本文一网打尽

ES 实战复杂sql查询、修改字段类型

转载请注明出处: 1.查询索引得 mapping 与 setting get 直接查询 索引名称时,会返回 该 索引得 mapping 和 settings 得配置,上述返回得结构如下: { "terra-syslog_2023-07-12" : { "aliases" : { }, "mappin

kibana上执行ES DSL语言查询数据并查看表结构与数据、删除索引、查看文件大小

转载请注明出处: 1.kibana 上执行DSL 语言: 在kibana 中找到 Dev Tools,并双击打开,就可以进入执行DSL得执行页面了 执行DSL,示例如图: 2.在kibana上查看ES得文档信息,文档结构,字段定义等 在kibana得界面上进入到 Management --> Sta

ES 2024 新特性

ECMAScript 2024 新特性 ECMAScript 2024, the 15th edition, added facilities for resizing and transferring ArrayBuffers and SharedArrayBuffers; added a new

[转帖]龙叔学ES:Elasticsearch XPACK安全认证

https://juejin.cn/post/7081994919237287950 本文已参与「新人创作礼」活动,一起开启掘金创作之路。 Elasticsearch往往存有公司大量的数据,如果安全不过关,那么就会有严重的数据安全隐患。 Elasticsearch 的安全认证方式有不少,如http-

[转帖]ES集群开启X-pack认证

https://www.cnblogs.com/jclty/p/12913996.html 1.下载 1 # wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.

ElasticSearch 实现分词全文检索 - 概述

ES 是一个使用Java语言并且基于Lucene编写的搜索引擎框架,他提供了分布式的全文搜索功能,提供了一个统一的基于Restful风格的WEB接口,官方客户端也对多种语言都提供了相应的API。

ElasticSearch 实现分词全文检索 - Scroll 深分页

ES 对 from + size 有限制,两者之和不能超过1W Scroll查询方式,不适合做实时的查询,每次都是从数据文档中的ID去获取,效果高了,但文档中的ID(第二步)不是实时更新的,一般后台管理的方式用 Scroll 比较方便