typora-copy-images-to |
---|
Elasticsearch |
定义
节点
分片
一份分片是Lucene的索引
分片是把一个大的索引分成多份放到不同的节点上
索引由多个主分片以及零个或多个副本分片构成
主分片
运行时不可以进行添加和移除 2. 索引:get-together,分片为get-together0
副分片
副本分片用于搜索,或是在原有主分片丢失后成为新的主分片 3. 运行时可以进行添加和移除
在集群中分发分片
创建索引
创建索引比创建一篇文档要花费更多时间
Put:http://localhost:9200/new_index
结果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "new_index"
}
获取映射
映射是随着新文档而自动创建
http://地址/索引(get-together)/类型(group)/_mapping
http://localhost:9200/get-together/_mapping/group
结果:
{
"get-together": {
"mappings": {
"group": {
"properties": {
"name": { // 属性列表
"type": "text",
"fields": {
"keyword": {
"type": "keyword", // 属性选项
"ignore_above": 256
}
}
},
"organizer": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
http://localhost:9200/get-together/group/_search?q=elasticsearch
同时搜索
http://localhost:9200/get-together/group,event/_search?q=elasticsearch(用,分割)
http://localhost:9200/get-together/_search?q=elasticsearch(在某个索引的多个类型中搜索)
http://localhost:9200/get-together,other-index/_search?q=elasticsearch(在多个索引中搜索,并且,分割) // 如果other-index不存在,请求失败,可以设置ignore_unavailable
时间
{
"took":2, // 花了多久处理请求,单位毫秒
"timed_out":false // 搜索是否超时,默认永远不会超时,可以在地址中设置url/?xx=xx&timeout=3s,如果超时了,该值为true
}
分片
{
"_shards":{
"total":2, // 总数
"successful":2, // 成功数量
"failed":0 // 失败数量
}
}
命令统计数据
{
htis:{ // 包含了匹配文档的数组
"total":2, // 匹配总数
"max_score":0.9066504 // 匹配文档的最高分,默认通过TF-IDF(词频-逆文档频率)算法
}
}
结果文档
{
"hits":{
hits:[{
"_index":"get-together",
"_type":"group",
"_id":"3",
"_score":0.9066504,
"fields":{
"location":["San Francisco,California","USA"]
"name":["Elasticsearch San Francisco"]
}
}]
}
}
http://localhost:9200/get-together/_search/
{
"query":{
"query_string":{ // 运行一个类型为query_string的查询
"query":"Denver",
"default_field":"name", // 默认查询_all字段(即所有字段),指定字段
"default_operaotr":"AND" // 默认操作符是OR,AND匹配所有的关键词
}
// 结果同上
"query":"name:elasticsearch AND name:san AND name:francisco",
// 在name字段中只查找elasticsearch一个词
"trem":{
"name":"elasticsearch"
}
}
}
http://localhost:9200/get-together/_search/
{
"query":{
// 过滤
"filtered":{
"filter":{
"term":{
"name":"elasticsearch"
}
}
}
}
}
应用聚集
词条聚集(terms aggregation)
{
"aggregations":{
"organizers":{
"trems":{ // 类型是terms,并且查找organizer字段
"field":"organizer"
}
}
}
}
通过ID获取文档
// 通过ID获取文档要比搜索更快,所耗资源成本也更多
http://localhost:9200/get-together/group/1
在elasticsearch.yml中指定集群的名称
默认情况下,新的结点通过多播发现已有的集群,通过向所有主机发送ping请求,这些主机侦听某个特定的多播地址。如果发现新的集群而且有同样的集群名称,新的节点就会将加入他们。你需要定制化集群的名称,防止默认配置的实例加入到你的集群
在yml中解除cluster.name这注释。修改为
cluster.name:elasticsearch-in-action
// 重启过后没有任何数据了,因为数据存储的目录包含集群的名称,可以将集群名称改回去然后再次重启,找回之前索引的数据
通过logging.yml指定详细日志记录(使用log4j)
主要日志(cluster-name.log):运行时所发生一切的综合信息
慢搜索日志(cluster-name_index_search_showlog.log):当某个查询很慢时,Elasticsearch在这里进行记录。默认情况下,如果一个查询花费的时间多于半秒,将在这里写入一条记录
慢索引日志(cluster-name_index_indexing_slowlog.log):默认情况下,如果一个索引操作花费的时间多于半秒,将在这里写入一条记录。
默认为info
rootLogger:TRACE,console,file
调整JVM设置
Elasticsearch使用的大部分内存称为堆(heap)。默认的设置让Elasticsearch为堆分配了256MB初始内存,然后最多扩展到1GB,如果超过1GB,则操作失败,日志记录out-of-memory错误
// 在命令行上
SET ES_HEAP_SIZE=500m & bin\elasticsearch.bat
// 永久方式:在启动脚本上设置#!bin/sh ES_HEAP_SIZE=500m
在集群中加入节点
新建索引
PUT:http://localhost:9200/bolg // 不能为大写字母
设置初始化值
PUT:http://localhost:9200/bolg1
{
“settings”:{
"number_of_shards":3 // 分片数量,该参数只能在初始化时使用
"number_of_replicas":0 // 副本数量
}
}
更新副本
PUT:http://localhost:9200/bolg1/_settings
{
“settings”:{
"number_of_replicas":0 // 副本数量
}
}
读写权限
blocks.read_only:true
blocks.read:true
blocks.write:true // 禁止对当前索引的写操作
PUT:http://localhost:9200/bolg1/_settings
{
"settings":{
"blocks.write":true
}
}
查看索引
GET:http://localhost:9200/bolg/_settings
GET:http://localhost:9200/_all/_settings
删除索引
DELETE:http://localhost:9200/bolg1
索引的打开与关闭
POST:http://localhost:9200/bolg/_open
POST:http://localhost:9200/bolg/_close
POST:http://localhost:9200/_all/_close // 匹配所有索引
POST:http://localhost:9200/test*/_close // 匹配test开头的索引
POST:http://localhost:9200/bolg/_close?ignore_unavailable=true // 忽略不存在的索引
复制索引
POST:http://localhost:9200/bolg/reindex
{
"source":{
"index":"bolg",
"type":"article",
"query":{
"term":{"title":"git"}
}
},
"dest":{
"index":"bolg_news"
}
}
收缩索引
索引别名
POST:http://localhost:9200/_aliases
{
"actions":[
{
"add":{ // 增加别名
"index":"bolg",
"alias":"alias1"
}
},
{
"remove":{ // 移除别名
"index":"bolg",
"alias":"alias1"
}
}
],
"actions":[ // 另一种形式的写法
{"add":{
"indices":
["test1","test2"],
"alias","alias1"}}
]
}
GET:http://localhost:9200/blog/_aliases // 获取该索引别名
GET:http://localhost:9200/_aliases // 获取所有别名
新建文档
PUT:http://localhost:9200/blog/article/1
{
"title":"git简介",
"posttime":"2017-05-01",
"content":"git是一款免费开源的软件"
}
POST:http://localhost:9200/blog/article // 不指定id则自动生成
获取文档
GET:http://localhost:9200/blog/article/1
HEAD:http://localhost:9200/blog/article/1 // 判断文档是否存在,如果存在则返回200
更新文档
PUT:http://localhost:9200/blog/article/1
{
"counter":1,
"tags":["red"]
}
POST:http://localhost:9200/blog/article/1/_update // 将counter值变为5
{
"script":{
"inline":"ctx._source.counter += params.count", // 执行的脚本,ctx是脚本语言中的一个执行对象,ctx获取_source在修改counter字段
"lang":"painless", //更新文档
"params":{
"count":4
}
}
}
POST:http://localhost:9200/blog/article/1/_update
{
"script":"ctx._source.new_fiel = 'value_of_new_field'" // 新添一个字段
"script":"ctx._source.remove('value_of_new_field')" // 移除一个字段
}
查询更新
POST:http://localhost:9200/blog/_update_by_query
{
"script":{
"inline":"ctx._source.category = params.category",
"lang":"painless",
"params":{"category":"git"}
},
"query":{
"term":{"title":"git"}
}
}
删除文档
DELETE:http://localhost:9200/blog/article/1
POST:http://localhost:9200/blog/_delete_by_query //查询删除
{
"query":{
"term":{
"title":"git"
}
}
}
{
"query":{
"match_all":{} // 删除一个type下的所有文档
}
}
批量操作
通过Bulk API可以换行批量索引、批量删除、批量更新等操作
创建一个JSON文件
文件中写入多个请求操作,格式如下:
action_and_meta_data\n // 指定了将要在哪个文档中执行什么操作(action必须是index、create、update或者delete。metadata需要指明需要被操作文档的_index、_type以及_id)
optional_source\n
action_and_meta_data\n
optional_source\n
执行操作
POST:localhost:9200/indexname/_bulk?pretty --data-binary@accounts.json
{
"index":{
"_index":"blog",
"_type":"article",
"_id":"1"
},
"create":{
"_index":"blog",
"_type":"article",
"_id":"1"
},
"title":"blog title"
}
版本控制
路由机制
POST:http://localhost:9200/website/blog/1?routing=user123
{
"title":"xxx",
"name":"xxx"
}
GET:http://localhost:9200/website/blog/1?routing=user123
定义
动态映射
文档中字段的类型是Elasticsearch自动识别的,不需要在创建索引的时候设置字段的类型。
http://localhost:9200/books/_mapping
mapping设置
true:默认值为true,自动添加字段
false:忽略新的字段
strict:严格模式,发现新的字段抛出异常
{
"mapping":{
"my_type":{
"data_detection":false // 忽略日期设置,总会被当做string类型,如要需要新增一个date类型的字段,需要手动添加
},
"it":{
"dynamic":"strict",
"properties":{
"title":{
"type":"text"
},
"publish_date":{
"type":"date"
}
}
}
}
}
日期检测
静态映射
创建索引时手工指定索引映射
{ // 手动指定类型
"mappings":{
"user":{
"_all":{"enable":false},
"properties":{
"title":{
"type":"text"
},
"name":{
"type":"text"
},
"age":{
"type":"integer"
}
},
"blogpost":{
"_all":{"enable":false},
"properties":{
"title":{
"type":"text"
},
"body":{
"type":"text"
},
"user_id":{
"type":"keyword"
},
"create":{
"type":"date",
"format":"stirict_date_optional_time||epoch_millis"
}
}
}
}
}
}
string:Elasticsearch 5.X之后的字段类型不再支持string,由text或keyword取代
text:如果一个字段要被全文搜索,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分词器分成一个一个词项。text类型的字段不用于排序,很少用于聚合(termsAggregation除外)
keyword:适用于索引结构化的字段。通常用于过滤、排序、聚合。只能通过精确值搜索到
数字类型:
date
boolean:5.4版本之后可接受的值为true、false、"true"、"false"
binary:接受base64编码的字符串,默认不存储(这里的存储是指store属性取值为false)
array:默认情况下任何字段都可以包含一个或者多个值,但是一个数组中的字必须是同一类型
{
"tags":["elasticsearch"."wow"],
"list":[
{
"name":"prog_list",
"description":"cool stff list"
},
{
"name":"prog_list",
"description":"cool stff list"
}
]
}
object:对象中可以包含对象(扁平化)
{
"region":"CHINA",
"manager.age":30
}
{
"my_type":{
"properties":{
"region":{
"type":"keyword"
}
},
"manager":{
"properties":{
"age":{"type":"interger"}
}
}
}
}
nested:Object类型的一个特例,可以让对象数组独立索引和查询
{
"user":[
{
"first":"Jhon",
"last":"Smith"
},
{
"first":"Alice",
"last":"White"
}
]
}
{
"user.first":["alice","Jhon"] // 扁平化
}
geo point
存储地理位置信息的经纬度
查找一定范围内的地理位置
通过地理位置或者相对中心店的距离来聚合文档
把距离因素整合到文档的评分中
通过距离对文档排序
{
"location":{
"lat":41.12,
"lon":-71.34
}
}
ip
存储IPv4或者IPv6,在映射中指定字段为ip类型的迎合和查询语句如下
{
"ip_addr":{"type":"ip"}
}
range
{
"type":"date_range"
}
{
"get":"2015-10-31 12:00:00"
}
token_count
元字段
_field_names
{
"query":{
"terms":{
"_field_names":{"body"}
}
}
}
_parent:用于指定同一索引中文档的父子关系。
{
"mappings":{
"my_parent":{},
"my_child":{
"_parent":{
"type":"my_parent"
}
}
}
}
PUT:my_index/my_child_3?parent:1&refresh=true
_routing
analyzer(索引分词器)
用于指定文本字段的分词器,对索引和查询都有效。分词器会把文本类型的内容转换为若干个词项,查询时分词器同样把查询字符串通过和索引时期相同的分词器或者其他分词器进行解析
对于content字段,analyzer参数的取值为ik_max_word,意味着content字段内容索引时和查询时搜使用ik_max_word分词
{
"mappings":{
"my_type":{
"properties":{
"content":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
}
search_analyzer(搜索分词器)
{
"search_analyzer":"strandard" // 一般索引和分词用相同的分词器
}
normalizer(标准化配置)
{
"settings":{
"analysis":{
"normalizer":{
"my_normalizer":{
"type":"custom",
"char_filter":[],
"filter":["lowercase","asciifolding"]
}
}
}
}
}
boost(用于设置字段权重)
{
"title":{
"type":"text",
"boost":2
},
"query":{
"match":{
"title":{
"query":"quick brown fox",
"boost":2
}
}
}
coerce(清除脏数据,默认为true)
copy_to(用于自定义_all,可以把多个字段的值复制到一个超级字段)
{
"copy_to":"full_content",
"full_content":{
"type":"text"
}
}
doc_values(加快排序、聚合操作)
{
"properties":{
"status_code":{
"type":;"keyword",
"doc_values":false
}
}
}
dynamic(检测新发现的字段)
enable(默认索引所有的字段)
{
"mappings":{
"my_type":{
"enable":false, // 禁用映射
"properties":{
"name":{
"enable":false // 禁用索引
}
}
}
}
}
fielddata
format(指定日期格式)
{
"formate":"yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
ignore_above
ignore_malformed
include_in_all
index
index_options
fields
norms
null_value
postion_increment_gap
properties
similarity
store
term_vector
创建索引
{
"settings":{
"number_of_replicas":1,
"number_of_shards":3
},
"mappings":{
"IT":{
"properties":{
"id":{
"type":"long"
},
"title":{
"type":"text",
"analyzer":"ik_max_word"
},
"language":{
"type":"keyword"
},
"author":{
"type":"keyword"
},
"price":{
"type":"double"
}, "year":{
"type":"date",
"format":"yyy-MM-dd"
}, "description":{
"type":"text",
"analyzer":"ik_max_word"
}
}
}
}
}
match_all_query
term
查询字段中包含给定单词的文档,term查询不被解析,只有查询词和文档中的词精确匹配才会被搜索到
查询的是词项,例如:java编程思想,分割为java,"编程","思想",java编程则无法查出
应用场景为查询人名、地名等精确匹配需求
from:指定返回结果的开始位置,size:指定返回结果包含的最大文档数
{
"from":0,
"size":100,
"_source":["title","author"], // 只返回某些字段
"version":true, // 默认不返回版本号,需要指定
"min_score"0.6, // 指定最小评分
"query":{
"term":{
"title":"思想" // 查询 title中含有“思想”关键字的书籍
}
},
"highlight":{ // 高亮查询关键字
"fields":{
"title":{}
}
}
}
match
{
"query":{
"match":{
"title":{
"query":"java编程思想",
"operator":"or"
}
}
}
}
match_phrase
首先会把query内容分词,分词器可以自定义,同时需要满足以下两个条件才会被搜索到
分词后所有词项都要出出现在该字段中
字段中的词顺序要一致
{
"query":{
"match_phrase":{
"foo":"hello world"
}
}
}
multi_match
用于搜索多个字段
{
"query":{
"multi_match":{
"query":"java编程",
"fields":["titile","description"]
},
"multi_match":{
"query":"java编程",
"fields":["titile","*_name"] // 支持通配符匹配
},
"multi_match":{
"query":"java编程",
"fields":["titile^3","description"] // ^:声明权重
}
}
}
common_terms_query
query_string_query
simple_query_string
terms query
term的升级,可以用来查询文档中包含多个词的文档,比如想查询title字段中包含关键词"java"或"python"的文档
{
"query":{
"terms":{
"title":["java","python"]
}
}
}
range query
用来匹配在某一范围内的数值型、日期类型或字符串字段的文档
gt,gte:大于等于,lt,lte:小于等于
{
"query":{
"range":{
"price":{
"gt":50,
"lte":70
}
}
}
}
exists query
查询会返回字段中至少有一个非空值的文档
{
"query":{
"exists":{
"field":"user"
}
}
}
// 以下文档会匹配
{"user":"jane"}
{"user":""}
{"user":"-"}
{"user":["jane"]}
{"user":["jane",null]}
// 以下不会匹配
{"user":null}
{"user":[jane]}
{"user":[null]}
prefix query
查询某个字段中以给定前缀开始的文档,比如查询title中含有以java为前缀的关键词的文档
{
"query":{
"prefix":{
"description":"win"
}
}
}
wildcard query
regexp query
fuzzy query
type query
查询具有指定类型的文档
{
"query":{
"type":{
"value":"IT"
}
}
}
ids query
用户查询具有指定id的文档,可以接受数组,如果未指定类型,则会搜索索引中的所有类型
{
"query":{
"ids":{
"type":"IT",
"values":["1","3","5"]
}
}
}
constant_score query
可以包装一个其他类型的查询,并返回匹配过滤器中的查询条件且具有相同评分的文档
{
"query":{
"constant_score":{
"filter":{ // 返回titile字段中含有关键词java的文档
"term":{"title":"java"}
},
"boost":1.2 // 所有文档的评分都是1.2
}
}
}
bool query
任意多个简单查询组合在一起,使用must,should,must_not,filter选项来表示简单查询之间的逻辑,每个选项都可以出现0次到多次
{
"query":{
"bool":{
"minimum_should_match":1,
"must":{
"match":{"title":"java"}
},
"should":{
"match":{"title":"java"}
},
"must_not":{
"range":{"price":{"gte":70}}
}
}
}
}
dis_max_query
支持多并发查询,可以返回与任意条件子句匹配的任何文档类型,与bool查询可以将所有匹配查询的分数相结合使的方式不同,dis_max查询只使用最佳匹配查询条件的分数
{
"query":{
"dis_max":{
"tie_breaker":0.7,
"boost":1.2,
"queries":[
{"term":{"age":34}}
]
}
}
}
functino_score_query
可以修改查询的文档得分,这个查询在有些情况下非常有用,比如通过评分函数计算文档得分代价较高,可以改用过滤器加自定义评分函数的方式来取代传统的评分方法
需要定义个查询和一至多个评分函数,评分函数会对查询到的每个文档分别计算得分
{
"query":{
"function_score":{
"query":{"match_all":{}},
"boost":"5",
"random_score":{},
"boost_mode":"multipay"
}
}
}
boosting query
对两个查询的评分进行调整的场景,boosting查询会把两个查询封装在一起并降低其中一个查询的评分,
{
"query":{
"boosting":{
"positive":{
"match":{
"title":"python"
}
}
}
}
}
indices query
nested query(嵌套查询)
文档中可能包含嵌套类型的字段,这些字段用来索引一些数组对象,每个对象都可以作为一条独立的文档被查询出来
{
"mappings":{
"type1":{
"properties":{
"obj1":{
"type":"nested"
}
}
}
}
}
has_child query(有子查询)
文档的父子关系创建索引时再映射中声明
{
"mappings":{
"branch":{},
"emplyee":{
"_parent":{
"type":"branch"
}
}
}
}
}
has_parent query(有父查询)
//创建索引
put geo{
"mappings":{
"properties":{
"name":{
"type":"keyword"
}
},
"location":{
"type":"geo_point"
}
}
}
按照查询和文档的相关度进行排序,默认按评分降序排序
{
"query":{
"term":{
"title":"java"
}
},
"sort":[
{"_score":{"order":"asc"}}
]
}
指标聚合
max aggregation:统计最大值
{
"size":0,
"aggs":{
"max_price":{
"max":{
"field":"price"
}
}
}
}
min aggregation
avg aggregation
sum aggregation
cardinality aggregation
stats aggregation
Extended stats aggregation
percentiles aggregation
value count aggregation
terms aggregation
filter aggregation
filters aggregation
range aggregation
Date Range aggregation
Date Histogram aggregation
Missing aggregation
Children aggregation
Geo Distance aggregation
IP Range Aggregation
连接测试
package com.zxw.Test;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
public class TestClient {
public static String CLUSTER_NAME = "elasticsearch";
public static String HOST_IP = "127.0.0.1";
public static int TCP_PORT = 9300;
public static void main(String[] args) throws UnknownHostException {
// 指定集群名称
Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
//
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(HOST_IP), TCP_PORT));
// 读取文档
GetResponse response = client.prepareGet("books", "IT", "1").get();
System.out.println(response.getSourceAsString());
}
}
索引
package com.zxw.index;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 索引API
*/
public class TextIndices {
public static String CLUSTER_NAME = "elasticsearch";
public static String HOST_IP = "127.0.0.1";
public static int TCP_PORT = 9300;
public static void main(String[] args) throws UnknownHostException {
// 指定集群名称
Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
//
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(HOST_IP), TCP_PORT));
IndicesAdminClient indices = client.admin().indices();
// 判断索引是否存在
IndicesExistsResponse books = indices.prepareExists("books").get();
System.out.println(books.isExists());
// 判断type是否存在
TypesExistsResponse response = indices.prepareTypesExists("books").setTypes("type1", "type2").get();
System.out.println(response.isExists());
// 创建索引
// createIndex(indices);
// 更新settings
// updateSettings(settings, indices);
// 获取settings
// getSettings(settings, indices);
// 设置mapping
AcknowledgedResponse response1 = client.admin().indices().preparePutMapping("twitter").setType("tweet").setSource("{\"properties\":{\"name\":{" +
"\"type\":\"keyword\"}}}").get();
System.out.println(response1);
}
private static void createIndex(IndicesAdminClient indices) {
indices.prepareCreate("twitter").setSettings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2)).get();
}
private static void updateSettings(Settings settings, IndicesAdminClient indices) {
// 更新副本
indices.prepareUpdateSettings("twitter").setSettings(Settings.builder().put("index.number_of_replicas", 0)).get();
}
private static void getSettings(Settings settings, IndicesAdminClient indices) {
// 获取Settings
GetSettingsResponse getSettingsResponse = indices.prepareGetSettings("twitter").get();
for (ObjectObjectCursor<String, Settings> indexToSetting : getSettingsResponse.getIndexToSettings()) {
String index = indexToSetting.key;
Settings value = indexToSetting.value;
Integer shards = settings.getAsInt("index.number_of_shards", null);
Integer replicas = settings.getAsInt("index.number_of_replicas", null);
System.out.println(index);
System.out.println(value);
System.out.println(shards);
System.out.println(replicas);
}
}
}
文档
package com.zxw.docu;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
public class TextDocument {
public static String CLUSTER_NAME = "elasticsearch";
public static String HOST_IP = "127.0.0.1";
public static int TCP_PORT = 9300;
public static void main(String[] args) throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(HOST_IP), TCP_PORT));
// 新建文档
// createDoc(client);
GetResponse response = client.prepareGet("books", "_search",null).get();
System.out.println(response.getSourceAsString());
}
private static void createDoc(TransportClient client) {
Map<String, Object> doc2 = new HashMap<>();
doc2.put("user", "kumchy");
doc2.put("postDate", "2013-01-30");
doc2.put("message", "trying out Elasticsearch");
IndexResponse response = client.prepareIndex("books","it").setSource(doc2).get();
System.out.println(response.status());
}
}
cat aliases:显示索引别名,也包括过滤器和路由信息
localhost:9200/_cat/aliases?v
cat allocation:查看每个节点分片的分配数量以及它们所使用的硬盘空间大小
localhost:9200/_cat/allocation?v
cat count:查询整个集群或者单个索引的文档数量
localhost:9200/_cat/count?v
cat fielddata:查看当前集群中每个数据节点上
cat health:显示集群的健康信息
cat indices:查看索引信息,包括索引健康状态、索引开关状态、分片数、副本数、文档数量、标记为删除的文档数量、占用的存储空间等信息
cat master:显示master节点的节点ID、绑定的IP和节点名。
cat nodeattrs:显示指定节点的属性信息
cat nodes:查看集群拓扑结构
cat pending tasks:查看正在执行的任务列表
cat plugins:查看每一个节点所运行插件的信息
cat recovery:一个索引分片恢复的视图,包括恢复中的和前线已完成的
cat repositories:展示集群中注册的快照库
cat thread pool:展示集群中每一个节点线程池的统计信息。默认情况下返回所有线程池的active、queue和rejected的统计信息
cat shards:查看节点包含的分片信息,包括一个分片是主分片还是一个副本分片、文档的数量、硬盘上占用的字节数、节点所在的位置等信息
cat segments:查看索引的段信息
cat templates:查看集群中的模板
cluster health:查看当前集群的健康信息
localhost:9200/_cluster/health
{
"cluster_name": "elasticsearch",
"status": "yellow",
"timed_out": false,
"number_of_nodes": 1,
"number_of_data_nodes": 1,// data节点数
"active_primary_shards": 43,// 活动的主分片
"active_shards": 43,// 所有活动的分片数
"relocating_shards": 0,// 正在发生迁移的分片
"initializing_shards": 0,// 正在初始化的分片
"unassigned_shards": 45,// 没有被分配的分片
"delayed_unassigned_shards": 0,// 延迟未被分配的分片
"number_of_pending_tasks": 0,// 节点任务队列中的任务书
"number_of_in_flight_fetch": 0,// 正在进行迁移的分片数量
"task_max_waiting_in_queue_millis": 0,// 队列中任务的最大等待时间
"active_shards_percent_as_number": 48.86363636363637 // 活动分片的百分比
}
cluster state:对整个集群的信息进行一个全面的了解,包括集群信息、集群中每个节点的信息、元数据、路由表等
cluster pending tasks:返回一个正在添加到更新集群状态的任务列表。集群中的变化通常是很快的,通常这个操作会返回一个空的列表
GET /_cluster/pending_tasks
cluster reroute:执行集群重新路由分配命令。例如:把一个分片从一个节点移动到另一个节点,把未分配的分片移动到一个指定的节点
cluster update settings:更新集群中的配置,如果是永久配置,就需要重启集群;如果是瞬时配置,就不需要重启集群
put /_cluster/settings
Nodes stats:可以获取集群中一个或多个节点的统计信息。
get /_nodes/stats
get /_nodes/nodeId1,nodeId2/stats
Nodes Info:获取集群中一个或多个节点的信息,包括设置、操作系统、虚拟机、线程池等信息
get /_nodes
// 也可以添加参数(settings、os、process、jvm、thread_pool、transport、http、plugins、ingest和indices)返回指定信息。
get/_nodes/os,jvm
Task Management API:可用于获取Elasticsearch集群中一个或多个节点正在执行中的任务信息
get /_tasks
cluster allocation explain api:解释分片没有被分配的原因
get /_cluster_allocation/explian
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。