关于elasticsearch:Elasticsearch-基础入门教程

5次阅读

共计 3857 个字符,预计需要花费 10 分钟才能阅读完成。

Elasticsearch 根底入门教程,罕用的命令语句,可间接复制到 kibana 上应用,实用于 6.x 7.x。


1 集群 / 索引相干

1.1 查看集群状态

GET /_cat/health?v&pretty

1.2 查看集群的索引状态

GET /_cluster/health?pretty&level=indices

1.3 查看索引信息

GET /_cat/indices?v&pretty

1.4 查看分片信息

GET /_cat/shards?v&pretty

1.5 查看各节点的容量应用状况

GET /_cat/allocation?v&pretty

1.6 查看各节点信息

GET /_nodes/process?pretty

GET /_cat/nodes?v

1.7 查看某个节点信息

GET /_nodes/node1/process?pretty

1.8 查看某个索引分片信息

GET /index/_search_shards

1.9 查看某个索引的 fielddata

GET /_stats/fielddata?fields=*&index=xx

1.10 查看 es 分词器的分词后果

GET /_analyze/?pretty
{
  "analyzer": "ik_max_word",
  "text": "测试用例"
}

1.11 查看索引中某个文档具体字段的分词后果

GET /index/type/id/_termvectors?fields=title

1.12 设置搜寻的最大返回数(三种形式)

1.12.1 全局配置

PUT /_settings
{
  "index": {"max_result_window": 100000000}
}

1.12.2 针对某个索引配置

PUT /index/_settings
{
  "index": {"max_result_window": 100000000}
}

1.12.3 配置文件

在 config/elasticsearch.yml 文件,加上 index.max_result_window: 100000000

2 搜寻相干

2.1 match_all

匹配所有

GET /index/_search
{
  "query": {"match_all": {}
  }
}

2.2 match_phrase

短语匹配,要求所有的分词必须同时呈现在文档中,同时地位必须紧邻统一。

GET /index/_search
{
  "query": {
    "match_phrase": {"name": "quick brown fox"}
  }
}

2.3 match

match 在匹配时会对所查找的关键词进行分词,而后按分词匹配查找。
match 会将关键词进行分词分成“my”和“cat”,查找时蕴含其中任一均可被匹配到。

GET /index/_search
{
  "query": {
    "match": {"name": "my cat"}
  },
  "sort": [
    {"age": "desc"}
  ]
}

2.4 multi_match

容许在 match 查问的根底上同时搜寻多个字段,在多个字段中同时查一个。

GET /index/_search
{
  "query": {
    "multi_match": {
      "query": "full text search",
      "fields": [
        "title",
        "body"
      ]
    }
  }
}

2.5 分页查问

from 为分页偏移量,默认第一页为 0,第 n 页为 n *size。

GET /index/_search
{
  "query": {"match_all": {}
  },
  "from": 0,
  "size": 2
}

2.6 指定查问后果的字段

GET /index/_search
{
  "query": {"match_all": {}
  },
  "_source": [
    "name",
    "age"
  ]
}

2.7 高亮显示查问后果

在每个搜寻后果中,高亮局部文本片段,以便让用户晓得为何该文档合乎查问条件。

GET /index/_search
{
  "query": {
    "match_phrase": {"name": "zhangsan"}
  },
  "highlight": {
    "fields": {"name": {}
    }
  }
}

2.8 term 与 terms

term 不分词检索,搜寻不会对搜索词进行分词拆解,次要用于准确匹配哪些值。
terms 容许指定多个匹配条件,多个 term,相似于 in 查问。

GET /index/_search
{
  "query": {
    "term": {"name": "xxx"}
  }
}

{
  "query": {
    "terms": {
      "tag": [
        "search",
        "full_text",
        "nosql"
      ]
    }
  }
}

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {"tag": "search"}
        },
        {
          "term": {"tag": "nosql"}
        }
      ]
    }
  }
}

2.9 range 过滤

gt:大于 gte:大于等于 lt:小于 lte:小于等于

GET /index/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lt": 30
      }
    }
  }
}

2.10 bool

bool 过滤能够用来合并多个过滤条件查问后果的布尔逻辑
must:多个查问条件的齐全匹配, 相当于 and。
must_not:多个查问条件的相同匹配,相当于 not。
should:至多有一个查问条件匹配, 相当于 or。
filter:必须匹配,依据过滤规范来排除或蕴含文档。

GET /index/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {"name": "zhangsan"}
      },
      "filter": {
        "range": {
          "age": {"gt": 25}
        }
      }
    }
  }
}

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {"title": "IBM"}
        },
        {
          "match": {"cluster_category": "其余文章"}
        }
      ]
    }
  }
}

2.11 多索引查问

留神:最初一个索引申请体前面需换行

GET /_msearch
{"index":"index1"}
{"query" : {"match_all" : {}}}
{"index":"index2"}
{"query" : {"match_all" : {}}}

2.12 多索引查问

留神:最初一个索引申请体前面需换行

GET /_msearch
{"index":"index1"}
{"query" : {"match_all" : {}}}
{"index":"index2"}
{"query" : {"match_all" : {}}}

2.13 搜寻后果返回总数

GET /index/_search
{
    "track_total_hits": true,
    "query":{"match_all":{}}
}

3 删除数据

  • 删除索引数据只能应用:deletebyquery,相比删除索引,deletebyquery 删除数据只是逻辑删除;
  • 真正的删除理论是段合并后的物理删除分段,也就是 deletebyquery 后,有一段时间磁盘空间不降反升。
POST /index/_delete_by_query
{
  "query": {"match_all": {}
  }
}

4 reindex

reindex 会将一个索引的数据复制到另一个已存在的索引,然而并不会复制原索引的 mapping(映射)、shard(分片)、replicas(正本)等配置信息,可用于数据迁徙。

  • 参数:

size,可选,批量提交条数,能够提高效率,倡议每批提交 5 -15M 的数据
type,可选,索引类型
query,可选,增加查问来过滤文档
sort,可选,排序
_source,可选,指定字段
remote,可选,es 连贯配置

POST _reindex
{
  "source": {
    "index": "index1",
    "size": 1000,
    "type": "tweet",
    "query": {
      "term": {"xx": "xx"}
    },
    "sort": {"date": "desc"},
    "_source": ["xx"],
    "remote": {
      "host": "http://xxx.xxx.xxx:9200",
      "username": "xxx",
      "password": "xxx",
      "socket_timeout": "1m",
      "connect_timeout": "30s"
    }
  },
  "dest": {"index": "index2"}
}

5 Nested 嵌套类型

嵌套对象将数组中的每个对象索引为独自的暗藏文档,这意味着能够独立于其余对象查问每个嵌套对象。

5.1 Nested 类型 删除操作

POST /indexName/_update/id
{
 "script": {
    "lang": "painless",
    "source": "ctx._source.comments.removeIf(it -> it.name =='John');"
  }
}

5.2 Nested 类型 批改操作

POST /indexName/_update/id
{
  "script": {"source": "for(e in ctx._source.comments){if (e.name =='steve') {e.age = 25; e.comment='good article...';}}" 
  }
}

5.3 Nested 类型 查问操作

POST /index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "comments",
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {"comments.name": "William"}
                  },
                  {
                    "match": {"comments.age": 34}
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
正文完
 0