• elasticsearch随笔 踏雪扬尘


    安装

    下载安装包,进入目录 以下命令启动 ./bin/elasticsearch 。
    如果这时报错"max virtual memory areas vm.maxmapcount [65530] is too low",要运行下面的命令。
    sudo sysctl -w vm.max_map_count=262144

    curl localhost:9200
    出现如下命令:则说明启动成功。

    基本概念

    Node 与 Cluster
    一个ES实例就叫做Node , 一组节点 构成一个集群,叫做 Cluster

    Index:
    ES 所有数据都会写入 反向索引(Inverted Index) , 每个index就相当于关系数据库的数据库的概念。
    以下命令看到所有的index:
    curl -X GET 'http://localhost:9200/_cat/indices?v'

    Document:
    index中的单条记录就叫做 Document, document 就是 JSON构成的具体数据。

    Type:
    index中的 document 可以分组, 逻辑分组就是 Type,它是虚拟的逻辑分组,用来过滤 document。 类似于 关系数据库表的概念。
    但是相同type中document 必须拥有相似的结构,比如ID 不能在一个type中是字符串,在另一个type中是数字,不同结构的document应该属于不同的index。
    列出每个 Index 所包含的 Type:
    curl 'localhost:9200/_mapping?pretty=true'

    DataNode:ES的数据存储与计算节点

    MasterNode:ES的Master节点,管理元数据、节点、数据分布等

    scroll:ES内置的数据集游标特性,用来对数据进行流式扫描和过滤

    _source: 导入时传入的原始JSON格式文档内容

    doc_values: ES/Lucene 中字段的列式存储定义

    keyword: 字符串类型字段,ES/Lucene不会对文本内容进行分词处理

    text: 字符串类型字段,ES/Lucene会对文本内容进行分词处理,分词器需要用户指定,默认为standard英文分词器

    实操

    新建 Index
    curl -X PUT 'localhost:9200/weather'
    删除这个 Index
    curl -X DELETE 'localhost:9200/weather'

    中文分词设置
    ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip

    重新启动 Elastic,就会自动加载这个新安装的插件

    新增记录
    curl -X PUT 'localhost:9200/accounts/person/1' -d '
    {
    "user": "张三",
    "title": "工程师",
    "desc": "数据库管理"
    }'
    以下添加一条记录,随机生成一个ID
    curl -X POST 'localhost:9200/accounts/person' -d '
    {
    "user": "李四",
    "title": "工程师",
    "desc": "系统管理"
    }'

    查看记录:
    curl 'localhost:9200/accounts/person/1?pretty=true'

    删除记录:
    curl -X DELETE 'localhost:9200/accounts/person/1'

    更新记录: 就是使用 pUT重新发一次请求
    curl -X PUT 'localhost:9200/accounts/person/1' -d '
    {
    "user" : "张三",
    "title" : "工程师",
    "desc" : "数据库管理,软件开发"
    }'
    修改之后 再查看发现版本号发生了变化 如下: "_version" : 2

    返回所有记录:

    curl 'localhost:9200/accounts/person/_search'

    全文搜索

    Elastic 的查询非常特别,使用自己的查询语法,要求 GET 请求带有数据体。
    匹配条件是desc字段里面包含"软件"这个词

    curl 'localhost:9200/accounts/person/_search' -H "Content-Type: application/json;charset=UTF-8" -d '
    {
    "query" : { "match" : { "desc" : "软件" }}
    }'

    从位置1开始(默认是从位置0开始),只返回一条结果
    curl 'localhost:9200/accounts/person/_search' -H "Content-Type: application/json;charset=UTF-8" -d '
    {
    "query" : { "match" : { "desc" : "软件" }},
    "from": 0,
    "size": 1
    }'

    逻辑运算

    如果有多个搜索关键字, Elastic 认为它们是or关系。

    curl 'localhost:9200/accounts/person/_search' -H "Content-Type: application/json;charset=UTF-8" -d '
    {
    "query" : { "match" : { "desc" : "软件 系统" }}
    }'

    如果要执行多个关键词的and搜索,必须使用布尔查询
    curl 'localhost:9200/accounts/person/_search' -H "Content-Type: application/json;charset=UTF-8" -d '
    {
    "query": {
    "bool": {
    "must": [
    { "match": { "desc": "软件" } },
    { "match": { "desc": "开发" } }
    ]
    }
    }
    }'

  • 相关阅读:
    前端开发Code Review内容【vue记录】
    Blue Jeans
    Arbitrage
    Common Subsequence
    Palindrome
    Stockbroker Grapevine
    Asteroids
    Frogger
    All in All
    Highways
  • 原文地址:https://www.cnblogs.com/yyystar/p/15581050.html
Copyright © 2020-2023  润新知