ElasticSearch基础篇-安装与基本操作

ElasticSearch基础篇

安装

官网

下载地址

下载完成后对文件进行解压,项目结构如下

  • 进入bin目录点击elasticsearch.bat启动服务

  • 9300 端口为 Elasticsearch 集群间组件的通信端口, 9200 端口为浏览器访问的 http协议 RESTful 端口

  • 打开浏览器,输入地址: http://localhost:9200,返回结果如下

    json 复制代码
    {
      "name" : "暗影精灵8",
      "cluster_name" : "elasticsearch",
      "cluster_uuid" : "9oaD9__3R_6WxskWlWe4iA",
      "version" : {
        "number" : "7.8.0",
        "build_flavor" : "default",
        "build_type" : "zip",
        "build_hash" : "757314695644ea9a1dc2fecd26d1a43856725e65",
        "build_date" : "2020-06-14T19:35:50.234439Z",
        "build_snapshot" : false,
        "lucene_version" : "8.5.1",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
倒排索引

什么是倒排索引?

倒排索引(英语:Inverted index),也常被称为反向索引、置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射。它是文档检索系统中最常用的数据结构。通过倒排索引,可以根据单词快速获取包含这个单词的文档列表。倒排索引主要由两个部分组成:"单词词典"和"倒排文件"

正排索引

id content
1001 my name is zhang san
1002 my name is li si
  • 对id创建索引,通过id查询对应的内容,如果通过id查询匹配关键字效率极低

倒排索引

keyword id
name 1001,1002
zhang 1001
  • 根据关键字查询对应的id

简单理解:正排索引是根据存储位置获取对应的内容,倒排索引是通过对应的内容获取存储位置

MySQL与ES结构关系

ES 里的 Index 可以看做一个库,而 Types 相当于表, Documents 则相当于表的行。这里 Types 的概念已经被逐渐弱化, Elasticsearch 6.X 中,一个 index 下已经只能包含一个type, Elasticsearch 7.X 中, Type 的概念已经被删除了

基本操作
创建索引
sh 复制代码
PUT http://localhost:9200/index_name

创建成功

json 复制代码
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "shopping"
}

创建失败:索引已存在

json 复制代码
{
    "error": {
        "root_cause": [
            {
                "type": "resource_already_exists_exception",
                "reason": "index [shopping/8ra6Z1w3RiudhIMMlYkkNw] already exists",
                "index_uuid": "8ra6Z1w3RiudhIMMlYkkNw",
                "index": "shopping"
            }
        ],
        "type": "resource_already_exists_exception",
        "reason": "index [shopping/8ra6Z1w3RiudhIMMlYkkNw] already exists",
        "index_uuid": "8ra6Z1w3RiudhIMMlYkkNw",
        "index": "shopping"
    },
    "status": 400
}
查询索引
GET http://localhost:9200/shopping

响应结果

json 复制代码
{
    "shopping": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "creation_date": "1690355410054",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "8ra6Z1w3RiudhIMMlYkkNw",
                "version": {
                    "created": "7080099"
                },
                "provided_name": "shopping"
            }
        }
    }
}
查询所有索引
GET http://localhost:9200/_cat/indices?v

响应结果

tex 复制代码
health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping 8ra6Z1w3RiudhIMMlYkkNw   1   1          0            0       208b           208b
删除索引
DELETE http://localhost:9200/shopping

响应结果

json 复制代码
{
    "acknowledged": true
}
创建文档
POST http://127.0.0.1:9200/shopping/_doc

body type:json

{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

响应结果

json 复制代码
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "44MZkYkBpGe0S7oK7CQ2",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
主键查询

通过指定id查询

GET http://127.0.0.1:9200/shopping/_doc/1001

响应结果

json 复制代码
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999.00
    }
}
全查询
GET http://127.0.0.1:9200/shopping/_search

响应结果

json 复制代码
{
    "took": 122,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "44MZkYkBpGe0S7oK7CQ2",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999.00
                }
            }
        ]
    }
}
全量修改
PUT http://127.0.0.1:9200/shopping/_doc/1001

body type:json
{
    "title":"华为手机",
    "category":"华为",
    "images":"http://www.gulixueyuan.com/hw.jpg",
    "price":1999.00
}

响应结果

json 复制代码
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}
局部修改
POST http://127.0.0.1:9200/shopping/_update/1001

body type:json
{
    "doc":{
        "title":"苹果手机"
    }
}

响应结果

json 复制代码
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}
删除文档
DELETE http://127.0.0.1:9200/shopping/_doc/1001

响应结果

json 复制代码
{
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "_version": 4,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}
相关推荐
晨欣2 小时前
Elasticsearch和Lucene之间是什么关系?(ChatGPT回答)
elasticsearch·chatgpt·lucene
筱源源8 小时前
Elasticsearch-linux环境部署
linux·elasticsearch
Elastic 中国社区官方博客18 小时前
释放专利力量:Patently 如何利用向量搜索和 NLP 简化协作
大数据·数据库·人工智能·elasticsearch·搜索引擎·自然语言处理
Shenqi Lotus1 天前
ELK-ELK基本概念_ElasticSearch的配置
elk·elasticsearch
yeye198912241 天前
10-Query & Filtering 与多字符串多字段查询
elasticsearch
Narutolxy1 天前
精准优化Elasticsearch:磁盘空间管理与性能提升技巧20241106
大数据·elasticsearch·jenkins
谢小涛2 天前
ES管理工具Cerebro 0.8.5 Windows版本安装及启动
elasticsearch·es·cerebro
LKID体2 天前
Elasticsearch核心概念
大数据·elasticsearch·搜索引擎
晨欣2 天前
Elasticsearch里的索引index是什么概念?(ChatGPT回答)
大数据·elasticsearch·jenkins
许苑向上2 天前
最详细【Elasticsearch】Elasticsearch Java API + Spring Boot集成 实战入门(基础篇)
java·数据库·spring boot·elasticsearch