Elasticsearch入门笔记(一)

这篇具有很好参考价值的文章主要介绍了Elasticsearch入门笔记(一)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

环境搭建

  Elasticsearch是搜索引擎,是常见的搜索工具之一。

  Kibana 是一个开源的分析和可视化平台,旨在与 Elasticsearch 合作。Kibana 提供搜索、查看和与存储在 Elasticsearch 索引中的数据进行交互的功能。开发者或运维人员可以轻松地执行高级数据分析,并在各种图表、表格和地图中可视化数据。

  其它可视化还有elasticsearch-head(轻量级,有对应的Chrome插件),本文不会详细介绍。

  Elasticsearch和Kibana的版本采用7.17.0,环境搭建采用Docker,docker-compose.yml文件如下:

version: "3.1"
# 服务配置
services:
  elasticsearch:
    container_name: elasticsearch-7.17.0
    image: elasticsearch:7.17.0
    environment:
      - "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
      - "http.host=0.0.0.0"
      - "node.name=elastic01"
      - "cluster.name=cluster_elasticsearch"
      - "discovery.type=single-node"
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - ./es/plugins:/usr/share/elasticsearch/plugins
      - ./es/data:/usr/share/elasticsearch/data
    networks:
      - elastic_net

  kibana:
    container_name: kibana-7.17.0
    image: kibana:7.17.0
    ports:
      - "5601:5601"
    networks:
      - elastic_net

# 网络配置
networks:
  elastic_net:
    driver: bridge

基础命令

  • 查看ElasticSearch是否启动成功:
curl http://IP:9200
  • 查看集群是否健康
curl http://IP:9200/_cat/health?v
  • 查看ElasticSearch所有的index
curl http://IP:9200/_cat/indices
  • 查看ElasticSearch所有indices或者某个index的文档数量
curl http://IP:9200/_cat/count?v
curl http://IP:9200/_cat/count/some_index_name?v
  • 查看每个节点正在运行的插件信息
curl http://IP:9200/_cat/plugins?v&s=component&h=name,component,version,description
  • 查看ik插件的分词结果
curl -H 'Content-Type: application/json'  -XGET 'http://IP:9200/_analyze?pretty' -d '{"analyzer":"ik_max_word","text":"美国留给伊拉克的是个烂摊子吗"}'

index操作

  • 查看某个index的mapping
curl http://IP:9200/some_index_name/_mapping
  • 查看某个index的所有数据
curl http://IP:9200/some_index_name/_search
  • 按ID进行查询
curl -X GET http://IP:9200/索引名称/文档类型/ID
  • 检索某个index的全部数据
curl http://IP:9200/索引名称/_search?pretty
curl -X POST http://IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }}"
  • 检索某个index的前几条数据(如果不指定size,则默认为10条)
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"size\" : 2}"
  • 检索某个index的中间几条数据(比如第11-20条数据)
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"from\" : 10, \"size\" : 10}}"
  • 检索某个index, 只返回context字段
curl -XPOST IP:9200/索引名称/_search?pretty -d "{\"query\": {\"match_all\": {} }, \"_source\": [\"context\"]}"
  • 删除某个index
curl -XDELETE 'IP:9200/index_name'

ES搜索

  1. 如果有多个搜索关键字, Elastic 认为它们是or关系。
  2. 如果要执行多个关键词的and搜索,必须使用布尔查询。
$ curl 'localhost:9200/索引名称/文档类型/_search'  -d '
{
  "query": {
    "bool": {
      "must": [
        { "match": { "content": "软件" } },
        { "match": { "content": "系统" } }
      ]
    }
  }
}'
  1. 复杂搜索:

SQL语句:

select * from test_index where name='tom' or (hired =true and (personality ='good' and rude != true ))

DSL语句:

GET /test_index/_search
{
    "query": {
            "bool": {
                "must": { "match":{ "name": "tom" }},
                "should": [
                    { "match":{ "hired": true }},
                    { "bool": {
                        "must":{ "match": { "personality": "good" }},
                        "must_not": { "match": { "rude": true }}
                    }}
                ],
                "minimum_should_match": 1
            }
    }
}

ik分词器

  ik分词器是Elasticsearch的中文分词器插件,对中文分词支持较好。ik版本要与Elasticsearch保持一致。

  ik 7.17.0下载地址为:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.17.0 ,下载后将其重名为ik,将其放至Elasticsearch的plugins文件夹下。

  ik分词器的使用命令(Kibana环境):

POST _analyze
{
  "text": "戚发轫是哪里人",
  "analyzer": "ik_smart"
}

输出结果为:

{
  "tokens" : [
    {
      "token" : "戚",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "发轫",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "是",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "哪里人",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 3
    }
  ]
}

  ik支持加载用户词典和停用词。ik 提供了配置文件 IKAnalyzer.cfg.xml(将其放在ik/config路径下),可以用来配置自己的扩展用户词典、停用词词典和远程扩展用户词典,都可以配置多个。

  配置完扩展用户词典和远程扩展用户词典都需要重启ES,后续对用户词典进行更新的话,需要重启ES,远程扩展用户词典配置完后支持热更新,每60秒检查更新。两个扩展词典都是添加到ik的主词典中,对所有索引生效。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<comment>IK Analyzer 扩展配置</comment>
	<!--用户可以在这里配置自己的扩展字典 -->
	<entry key="ext_dict">custom/mydict.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
	<!--用户可以在这里配置远程扩展字典 -->
	<!-- <entry key="remote_ext_dict">words_location</entry> -->
	<!--用户可以在这里配置远程扩展停止词字典-->
	<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

  用户词典文件路径为:custom/mydict.dic,停用词词典路径为:custom/ext_stopword.dic,将它们放在ik/config/custom路径下。

  用户词典文件中加入’戚发轫’,停用词词典加入’是’,对原来文本进行分词:

POST _analyze
{
  "text": "戚发轫是哪里人",
  "analyzer": "ik_smart"
}

输出结果如下:

{
  "tokens" : [
    {
      "token" : "戚发轫",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "哪里人",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 1
    }
  ]
}

  如果’analyzer’选择ik_smart,则会将文本做最粗粒度的拆分;选择ik_max_word,则会将文本做最细粒度的拆分。测试如下:

POST _analyze
{
  "text": "戚发轫是哪里人",
  "analyzer": "ik_max_word"
}

输出结果如下:

{
  "tokens" : [
    {
      "token" : "戚发轫",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "发轫",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "哪里人",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "哪里",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "里人",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 4
    }
  ]
}

总结

  本文主要介绍了Elasticsearch一些基础命令和用法,是笔者的Elasticsearch学习笔记第一篇,后续将持续更新。

  本文代码已放至Github,网址为:https://github.com/percent4/ES_Learning .文章来源地址https://www.toymoban.com/news/detail-617546.html

到了这里,关于Elasticsearch入门笔记(一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【ElasticSearch01】ElasticSearch入门

    结构化数据 二维表等,保存到关系型数据库中例如mysql 非结构化数据 图像、视频、工作日志等,保存到Nosql数据库中,比如redis、mongodb中 半结构化数据 html、xml等保存到Nosql数据库中,比如redis、mongodb中 The Elastic Stack, 包括 Elasticsearch、 Kibana、 Beats 和 Logstash(也称为 ELK Stac

    2024年02月05日
    浏览(32)
  • ElasticSearch学习之ElasticSearch快速入门实战

    1.先“分词” 2.倒排索引(前提是分词) ElasticSearch官网地址: 欢迎来到 Elastic — Elasticsearch 和 Kibana 的开发者 | Elastic https://www.elastic.co/cn/ 下载地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch 我在本地下载的是7.17.3版本  解压: 启动es之前别忘了配置环境变量:ES_JA

    2024年02月14日
    浏览(27)
  • ElasticSearch笔记03-ElasticSearch环境

    单台Elasticsearch服务器提供服务,往往都有最大的负载能力,超过这个阈值,服务器性能就会大大降低甚至不可用,所以生产环境中,一般都是运行在指定服务器集群中。 除了负载能力,单点服务器也存在其他问题: 单台服务器存储容量有限 单服务器容易出现单点故障,无法

    2024年02月12日
    浏览(75)
  • 【Elasticsearch】Elasticsearch 从入门到精通(二):基础使用

    《 Elasticsearch 从入门到精通 》共包含以下 2 2 2 篇文章: Elasticsearch 从入门到精通(一):基本介绍 Elasticsearch 从入门到精通(二):基础使用 😊 如果您觉得这篇文章有用 ✔️ 的话,请给博主一个一键三连 🚀🚀🚀 吧 (点赞 🧡、关注 💛、收藏 💚)!!!您的支持 💖

    2024年04月25日
    浏览(26)
  • 【Elasticsearch】Elasticsearch 从入门到精通(一):基本介绍

    《 Elasticsearch 从入门到精通 》共包含以下 2 2 2 篇文章: Elasticsearch 从入门到精通(一):基本介绍 Elasticsearch 从入门到精通(二):基础使用 😊 如果您觉得这篇文章有用 ✔️ 的话,请给博主一个一键三连 🚀🚀🚀 吧 (点赞 🧡、关注 💛、收藏 💚)!!!您的支持 💖

    2024年04月23日
    浏览(24)
  • 九.全文检索ElasticSearch经典入门-ElasticSearch映射修改

    这篇文章的内容是ElasticSearch映射修改,写这篇文章是有水友公司里面遇到了映射修改问题,我这里做了一个整理,希望对你有所帮助。 在ElasticSearch中一旦创建了映射想要进行修改是不被允许的。比如我这里有一个案例 上面创建了索引employee ,同时为其创建映射,指定了id和

    2024年02月05日
    浏览(49)
  • ElasticSearch-学习笔记02【ElasticSearch索引库维护】

    Java后端-学习路线-笔记汇总表【黑马程序员】 ElasticSearch-学习笔记01【ElasticSearch基本介绍】 【day01】 ElasticSearch-学习笔记02【ElasticSearch索引库维护】 ElasticSearch-学习笔记03【ElasticSearch集群】 ElasticSearch-学习笔记04【Java客户端操作索引库】 【day02】 ElasticSearch-学习笔记05【Spri

    2024年02月04日
    浏览(34)
  • Elasticsearch7.8.0版本入门——Elasticsearch指定JDK11版本

    本地安装jdk1.8版本后,安装Elasticsearch7.8.0需要jdk11及以上版本支持 下载jdk11解压即用安装包,解压到具体磁盘目录 在系统变量中增加了一个 ES_JDK, 指向了刚刚JDK11 的目录 ,如下图: 进入elasticsearch/bin目录下,找到 elasticsearch-env 这个文件,把【JAVA_HOME】换成刚刚系统变量中配

    2024年02月12日
    浏览(37)
  • 【大数据入门核心技术-ElasticSearch】(二)ElasticSearch整体架构和重要工作原理

    目录 一、整体架构图 二、重要工作原理 1、文档写入原理 2、文档检索原理

    2024年02月05日
    浏览(32)
  • Elasticsearch教程—Elasticsearch Java API Client [8.6]开发入门(官方原版)

    大家好,我是Doker! Java 8 或更高版本。 一个 JSON 对象映射库,允许无缝集成 您的应用程序类与 Elasticsearch API 一起。Java 客户端具有 支持 Jackson 或 Eclipse Yasson 等 JSON-B 库。 Java API客户端由三个主要组件组成: API客户端类。这些为Elasticsearch API提供了强类型的数据结构和方法。

    2024年02月13日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包