zoukankan      html  css  js  c++  java
  • Elasticsearch 入门,基本概念和操作

    基本概念

    Node 与 Cluster

    Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例。

    单个 Elastic 实例称为一个节点(node)。一组节点构成一个集群(cluster)。

    Index ( 对应数据库的表 )

    Elastic 会索引所有字段,经过处理后写入一个反向索引(Inverted Index)。查找数据的时候,直接查找该索引。

    所以,Elastic 数据管理的顶层单位就叫做 Index(索引)。它是表的同义词。每个 Index 的名字必须是小写。

    下面的命令可以查看当前节点的所有 Index。

    curl -X GET 'http://localhost:9200/_cat/indices?v'
    

     ElasticSearch 默认开启9200、9300端口,9200端口供http访问,9300供tcp访问,ElasticSearch通过9300端口通信,

    可以直接通过 http://localhost:9200 访问 ElasticSearch,为简化示例,后续都是通过curl方式演示相关操作

    Document (对应数据库表的行)

    Index 里面单条的记录称为 Document(文档)。许多条 Document 构成了一个 Index。

    Document 使用 JSON 格式表示,下面是一个例子。

    同一个 Index 里面的 Document,不要求有相同的结构(scheme),但是最好保持相同,这样有利于提高搜索效率。

    {
      "user": "张三",
      "title": "工程师",
      "desc": "数据库管理"
    }
    

    Type(分类,elasticsearch 7.0之后弃用)

    Document 可以分组,比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天)。这种分组就叫做 Type,它是虚拟的逻辑分组,用来过滤 Document。

    不同的 Type 应该有相似的结构(schema),举例来说,id字段不能在这个组是字符串,在另一个组是数值。这是与关系型数据库的表的一个区别。性质完全不同的数据(比如productslogs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。

    下面的命令可以列出每个 Index 所包含的 Type。

    curl 'localhost:9200/_mapping?pretty=true'
    

    Mapping(类似数据库中的表结构)

    查看当前所有mapping

    curl 'localhost:9200/_mapping?pretty=true'
    

     

    基本操作

    查看当前节点的所有Index

    curl -X GET 'http://localhost:9200/_cat/indices?v'

    列出每个 Index 所包含的 Type

    curl 'localhost:9200/_mapping?pretty=true'

    新建Index,下面命令,首先新建一个名称为news的 Index,里面有一个名称为newscontent的 Type。news有一个字段 content 。content字段都是中文,而且类型都是文本(text),所以需要指定中文分词器,不能使用默认的英文分词器。

    创建index同时创建mapping

    curl -X PUT http://localhost:9200/news -H 'Content-Type:application/json' -d'
    {
    "mappings": {
    "newscontent": {
    "properties": {
    "content": {
    "type": "text",
    "analyzer": "ik_max_word",
    "search_analyzer": "ik_max_word"
    }
    }
    }
    }
    }'
    

      

    删除Index

    curl -X DELETE 'localhost:9200/news'

    新增数据

    curl -XPOST http://localhost:9200/news/newscontent/1 -H 'Content-Type:application/json' -d'
    {"content":"美国留给伊拉克的是个烂摊子吗"}
    '
     
    curl -XPOST http://localhost:9200/news/newscontent/2 -H 'Content-Type:application/json' -d'
    {"content":"公安部:各地校车将享最高路权"}
    '
     
    curl -XPOST http://localhost:9200/news/newscontent/3 -H 'Content-Type:application/json' -d'
    {"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
    '
     
    curl -XPOST http://localhost:9200/news/newscontent/4 -H 'Content-Type:application/json' -d'
    {"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
    '
    

      

    查看某ID的数据

    curl 'localhost:9200/news/newscontent/1?pretty=true'

    返回所有记录

    curl -XPOST http://localhost:9200/news/_search

    返回搜索记录,并高亮显示 hightlight

    curl -XPOST http://localhost:9200/news/_search  -H 'Content-Type:application/json' -d'
    {
        "query" : { "match" : { "content" : "中国" }},
        "highlight" : {
            "pre_tags" : ["<red>"],
            "post_tags" : ["</red>"],
            "fields" : {
                "content" : {}
            }
        }
    }
    '
    
    返回结果示例
    {
        "took": 14,
        "timed_out": false,
        "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
        },
        "hits": {
            "total": 2,
            "max_score": 2,
            "hits": [
                {
                    "_index": "news",
                    "_type": "newscontent",
                    "_id": "4",
                    "_score": 2,
                    "_source": {
                        "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
                    },
                    "highlight": {
                        "content": [
                            "<red>中国</red>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
                        ]
                    }
                },
                {
                    "_index": "news",
                    "_type": "newscontent",
                    "_id": "3",
                    "_score": 2,
                    "_source": {
                        "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
                    },
                    "highlight": {
                        "content": [
                            "均每天扣1艘<red>中国</red>渔船 "
                        ]
                    }
                }
            ]
        }
    }
    

      

    参考资料

    全文搜索引擎 Elasticsearch 入门教程

    作者: 阮一峰

    http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html

    中文分词 elasticsearch-analysis-ik

    https://github.com/medcl/elasticsearch-analysis-ik

    CentOS7安装Elasticsearch

    腾讯云实验室

    https://www.cnblogs.com/gezifeiyang/p/11007727.html

    Elasticsearch: 权威指南(官方文档) 

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html

  • 相关阅读:
    java spring Validator
    jwt生成token
    java最大子序列
    在chrome下让网页显示12号以下的字体
    父元素position: relative;子元素position: absolute;子元素要超出父元素,但宽度不确定(自适应)
    行内元素和块级元素
    js打开新窗口方法整理
    浏览器加载、解析、渲染的过程
    Web前端优化,提高加载速度
    HTMl5的sessionStorage和localStorage
  • 原文地址:https://www.cnblogs.com/gezifeiyang/p/10998053.html
Copyright © 2011-2022 走看看