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
}
相关推荐
java1234_小锋4 分钟前
Elasticsearch中的节点(比如共20个),其中的10个选了一个master,另外10个选了另一个master,怎么办?
大数据·elasticsearch·jenkins
Elastic 中国社区官方博客5 分钟前
Elasticsearch 开放推理 API 增加了对 IBM watsonx.ai Slate 嵌入模型的支持
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
我的运维人生6 分钟前
Elasticsearch实战应用:构建高效搜索与分析平台
大数据·elasticsearch·jenkins·运维开发·技术共享
Mephisto.java5 小时前
【大数据学习 | Spark】Spark的改变分区的算子
大数据·elasticsearch·oracle·spark·kafka·memcache
mqiqe5 小时前
Elasticsearch 分词器
python·elasticsearch
小马爱打代码5 小时前
Elasticsearch简介与实操
大数据·elasticsearch·搜索引擎
java1234_小锋14 小时前
Elasticsearch是如何实现Master选举的?
大数据·elasticsearch·搜索引擎
梦幻通灵20 小时前
ES分词环境实战
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客20 小时前
Elasticsearch 中的热点以及如何使用 AutoOps 解决它们
大数据·运维·elasticsearch·搜索引擎·全文检索
小黑屋说YYDS1 天前
ElasticSearch7.x入门教程之索引概念和基础操作(三)
elasticsearch