Elasticsearch之详细基础语法

Elasticsearch之详细基础语法

    • 优化
      • 配置
      • 创建
      • 查询
    • 命令
    • 字段类型
    • 创建
    • 添加数据
    • 修改索引
    • 打开/关闭索引
    • 删除
    • 自定义分词器
    • 查询
      • 命令说明
      • 查询索引Mapping(结构)
      • 查全部 match_all
      • 单条件查询
      • Bool多条件查询
      • 聚合查询 Aggregation

优化

配置

  • 当机器内存小于64G时,遵循通用的原则,50%给ES,50%留给lucene。

  • 当机器内存大于64G时,遵循以下原则:

    • a. 如果主要的使用场景是全文检索, 那么建议给ES Heap分配 4~32G的内存即可;其它内存留给操作系统, 供lucene使用(segments cache), 以提供更快的查询性能。

    • b. 如果主要的使用场景是聚合或排序, 并且大多数是numerics, dates, geo_points 以及not_analyzed的字符类型, 建议分配给ES Heap分配 4~32G的内存即可,其它内存留给操作系统,供lucene使用(doc values cache),提供快速的基于文档的聚类、排序性能。

    • c. 如果使用场景是聚合或排序,并且都是基于analyzed 字符数据,这时需要更多的 heap size, 建议机器上运行多ES实例,每个实例保持不超过50%的ES heap设置(但不超过32G,堆内存设置32G以下时,JVM使用对象指标压缩技巧节省空间),50%以上留给lucene。

  • 禁止swap,一旦允许内存与磁盘的交换,会引起致命的性能问题。 通过: 在elasticsearch.yml 中 bootstrap.memory_lock: true, 以保持JVM锁定内存,保证ES的性能。

创建

  • 不需要分词的字段,统一设置成keyword,降低索引存储大小

查询

  • 尽量使用term、filter等精确查询
  • 尽量不使用聚合查询aggs

命令

-7.x中的type已经过时,默认设置为_doc

所有的命令都为大写

命令 url 解释
PUT /索引名称/类型名称/文档ID 创建文档(指定文档ID)
POST /索引名称/索引类型 创建文档(随机文档ID)
POST /索引名称/类型名称/文档id/_update 修改文档
POST /索引名称/类型名称/_search 查询数据
DELETE /索引名称/类型名称/文档id 删除文档/或者索引
GET /索引名称/类型名称/文档id 查询文档通过文档ID

字段类型

类型 对应类型 说明
字符串 text keyword text自动分词,keyword全文匹配
整型 byte short integer long
浮点型 float double half_float scaled_float
日期 date
布尔 boolean
二进制 binary
范围 range
数组 array
对象 object
嵌套 nested
ip ip (IPv4 和 IPv6 地址)

创建

添加数据可以在自动索引生成之后,先查询Mapping,在Mapping的基础上进行修改索引的字段

规范:数据中String不需要进行分词的,type必须设置为keyword

  • settings: 用来设置分片,副本等配置信息(自定义分词器)
  • mappings: 字段映射,类型等
    • properties: 由于type在后续版本中会被Deprecated, 所以无需被type嵌套
POST index_name
{
  "settings": { ... any settings ... },
  "mappings" : {
      "properties" : {
        "field1" : {
          "type" : "keyword"
        },
        "field2" : {
          "type" : "long"
        },
        "field3" : {
          "type" : "text"
        },
        "field4" : {
          "type" : "date"
        },
        "field5" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
}

添加数据

POST index_name/_doc
{
  "name" : "姓名" ,
  "age" : "年龄" ,
  "sex" : "性别" 
}

禁止自动创建索引

可以通过在 config/elasticsearch.yml 的每个节点下添加下面的配置:

action.auto_create_index: false

修改索引

修改副本数量为0

PUT /test-index-users/_settings
{
  "settings": {
    "number_of_replicas": 0
  }
}

打开/关闭索引

  • 关闭索引

一旦索引被关闭,那么这个索引只能显示元数据信息,不能够进行读写操作

POST index_name/_close
  • 打开索引
POST index_name/_open

删除

DELETE index_name

自定义分词器

PUT /es_medias_test2
{
  "settings": {
    "analysis": {
      "analyzer": {
        "comma": {  //自定义分词器名称
          "type": "pattern",
          "pattern": ","
        }
      }
    }
  },
  "mappings": {
    "esmedias": {
      "properties": {
        "mediaTag": {
          "type": "text",
          "analyzer": "comma"
        }
      }
    }
  }
}

自定义分词器

PUT test_index2
{
"settings":{
  "analysis":{
    "analyzer":{
        "symbol_smart":{
            "type":"custom",
            "char_filter":[],
            "tokenizer":"punctuation",
            "filter":[]
        }
    },
    "tokenizer":{
        "punctuation":{
            "type":"pattern",
            "pattern":"[.,!?]"
        }
    },
    "char_filter":{},
    "filter":{}
    }
  }
}

测试分词器

POST test_index2/_analyze
{
   "analyzer": "symbol_smart",
   "text": "心内科,心外科"
}

参考:

https://zhuanlan.zhihu.com/p/43437056

查询

查询语句按照JSON格式映射

命令说明

命令 说明
from, size 分页
match 模糊查询
match_phrase 段落匹配
term 精确查询
terms 多字段精确查询
bool 多条件查询
query 查询
filter 过滤 必须满足 不计入得分
sort 排序
should 任意满足一个

查询索引Mapping(结构)

GET index_name

查全部 match_all

GET index_name/_search
OR
GET index_name/_search
{
  "query": {
    "match_all": {}
  }
}

单条件查询

每次只能有一个查询

  • from size 指定查询条数,类似于sql的分页
  • _source 限制返回的字段 _source
GET /atlas_cloud_logs/_search
{
  "from": 0, 
  "size": 2, 
  "_source": ["字段"],
  "query": {
    "match": {
      "字段": "条件内容"
    }
  }
}

Bool多条件查询

query 上下文的条件是用来给文档打分的,匹配越好 _score 越高;

filter 的条件只产生两种结果:符合与不符合,后者被过滤掉

  • query 查询 计入得分

  • filter 必须满足 不计入得分

  • must 必须

  • should 或者

  • must_not 不等于

  • sort 排序 _score 查询分数

  • minimum_should_match 至少匹配多少should 字段

  • match 模糊查询

  • match_phrase 段落匹配 查询字段中包含查询内容mill

  • range 范围查询

  • gte 大于等于

  • lte 小于等于

  • e 等于

GET index_name/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "fieldName": {
            "gte": 10, 
            "lte": 200
          }
        }
      },
      "match_phrase": { "address": "mill" }
    }
  },
  "sort": [
    { "_score": "asc" }
  ],
  "minimum_should_match": 1
}

聚合查询 Aggregation

GET /atlas_cloud_logs/_search
{
  "size": 0, 
  "query": {
   "match_all": {
   }
  },
  "aggs": {
    "聚合后返回的关键字": {
      "terms": {
        "field": "聚合字段.keyword"
      }
    }
  }
}

你可能感兴趣的:(Tools,es,elasticsearch)