GET /_cat?health?v
status
默认配置是给每个index分配5个primary shard和5个replica shard
快速查看集群中有哪些索引
GET /_CAT?indices?v
query string search
GET /index/type/_search
GET /index/type/_search?q=name:value&sort=price:desc
{
"query":{
"bool":{
"must":{
"match":{
"field":"value"
}
},
"filter":{
"range":{
"price":{
"gt":25
}
}
}
}
}
}
full-text search
{
"query":{
"match":{
"producer":"yagao producer"
}
}
}
//将存储的数据进行拆解
special 4
yagao 4
producer 1,2,3,4
gaolujie 1
zhonghua 3
yagao producer ====》 yagao 和 producer
//再去匹配,根据分数排序
phrase search
highlight search
#terms是否有其他字段
{
#不返回数据
"size":0,
"query":{
"group_by_tags":{
"terms":{
"field":"tags"
}
}
}
}
{
"aggregations":{
"group_by_tags":{
"buckets":[
{
"key":"防蛀牙",
"doc_count":2
#每个分组的数量
}
]
}
}
}
{
"size":0,
"aggs":{
"group_by_tags":{
"terms":{
"field":"tags",
"order":{
"avg_price":"desc"
}
},
"aggs":{
"avg_price":{
"avg":{
"field":"price"
}
}
}
}
}
}
{
"size":0,
"aggs":{
"group_by_price":{
"range":{
"field":"price",
"ranges":[
{"from":0,"to":20},
{"from":20,"to":40}
]
},
"aggs":{
"group_by_tags":{
"terms":{
"field":"tags"
},
"aggs":{
"average_price":{
"avg":{
"field":"price"
}
}
}
}
}
}
}
}
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
}
}
_index
_type
_id
4. 手动生成id:路径后面带id
5. 自动生成id:
_source
_document
put /index/type/id?op_type=create
put /index/type/id/_create
PUT /test_index/test_type/8?version=3&version_type=external
POST /INDEX/type/id?_update
{
"doc":{
"field":"value"
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MjWCZ8CP-1571677387066)(F:\工作文档\个人技术框架笔记\ElasticSearch\images\partialUpdate.png)]
POST /test_index/test_type/11/_update
{
"script":"ctx._source.num+=1"
}
#在es的config的script中创建groovy文件 ctx._source.tags+=new_tags
POST /index/type/11/_update
{
"script":{
"lang":"groovy",
"file":"test-add-grooy",
"params":{
"new_tag":"tag1"
}
}
}
#test-delete-groovy: ctx.op = ctx._source.num ==count? 'delete':'none'
#POST /index/type/11/_update
{
"script":{
"lang":"groovy",
"file":"test-delete-grooy",
"params":{
"count":1
}
}
}
upsert操作
#POST /index/type/11/_update
{
"script":"ctx._source.num+=1",
"upsert":{
"num":0,
"tags":[]
}
}
当读到版本号为1,准备写入过程中被其他线程提前修改了,此时partial update会失败
可通过retry_on_confilct
retry_on_confilct
post /index/type/id/_update?retry_on_conflict=5&version=6
#GET /_mget
{
"docs":[
{
"_index":"index",
"_type":"type",
"_id":2
}
]
}
#POST /_bulk
{"create":{"_index":"index","_type":"type","_id":"id"}}
#附带新增的数据
{"field":"greate"}
#附带修改的数据
{"doc":{"field":"greate"}}
路由算法: shard = hash(routing) % number_of_primary_shards
可以手动指定routing的值
put /index/type/id?routing=user_id
primary shard不可变的谜底
在进行写操作的时候,加一个参数 consistency
put /index/type/id?consistency=quorum
可选的值
quorum的大部分计算公式
总共有60000条数据,每个shard上有20000条数据,每页10条数据。
#GET /INDEX/TYPE/_validate/query?explain
{
"query":{
"match":{
}
}
}
#PUT /index
{
"mapping":{
"article":{
"properties":{
"title":{
"type":"text",
"fields":{
"raw":{
"type":"string",
"index":"not_analyzed"
}
},
"fielddata":true
},
"content":{
"type":"text"
}
}
}
}
}
#GET /index/type/_search
{
"query":{
"match_all":{
}
},
"sort":{
"title.raw":{
"order":"desc"
}
}
}
对文档进行相关度评分计算
分数是如何被影响的?
TF&IDF:Term Frequency/inverse document frequency
搜索请求: hello world
doc1: hello,today is very good
doc2: hi world,how are you
搜索请求: hello world
doc1:{“title":"hello article","content":"babbababa"}
doc2:{“title":"my article","content":"babbababa,hi,world"}
get /index/type/_search/explain
get /index/type/1/_explain
doc1: hello world you and me
doc2: hi, world, how are you
word doc1 doc2
hello *
world * *
you * *
and *
me *
hi *
how *
hello you --> hello,you
hello--> doc1
you --> doc1,doc2
doc1: { "name": "jack", "age": 27 }
doc2: { "name": "tom", "age": 30 }
document name age
doc1 jack 27
doc2 tom 30
#GET /index/type/_search?scroll=1m
{
"query":{
"match_all":{}
},
"sort":{"_doc"},
"size":3
}
#GET /_search?scroll
{
"scroll":"1m",
"scroll_id":""
}
#PUT /INDEX
{
"settings":{},
"mappings":{}
}
PUT /index/_settings
{
"number_of_replicas":1
}
#PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"es_std": {
"type": "standard",
"stopwords": "_english_"
}
}
}
}
}
#GET /my_index/_analyze
{
"analyzer": "standard",
"text": "a dog is in the house"
}
#GET /my_index/_analyze
{
"analyzer": "es_std",
"text":"a dog is in the house"
}
#PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"&_to_and": {
"type": "mapping",
"mappings": ["&=> and"]
}
},
"filter": {
"my_stopwords": {
"type": "stop",
"stopwords": ["the", "a"]
}
},
"analyzer": {
"my_analyzer": {
"type": "custom",
"char_filter": ["html_strip", "&_to_and"],
"tokenizer": "standard",
"filter": ["lowercase", "my_stopwords"]
}
}
}
}
}
#GET /my_index/_analyze
{
"text": "tom&jerry are a friend in the house, , HAHA!!",
"analyzer": "my_analyzer"
}
#PUT /my_index/_mapping/my_type
{
"properties": {
"content": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
PUT /index
{
"mappings":{
"my_types":{
"_source":{
"enabled":false
}
}
}
}
#PUT /source_index
{
"mappings": {
"source_type":{
"_source":{
"includes":["content"]
},
"properties": {
"title":{
"type": "text"
},
"content":{
"type": "text"
}
}
}
}
}
将所有field打包在一起,作为一个_all field,建立索引。
没有指定任何field进行搜索时,就是使用_all field在搜索
也可以在field级别设置,对每个字段的映射设置include_in_all参数 ,设置是否要将field值包含在_all field中
\ _all字段默认是关闭的,如果要开启_all字段,索引增大是不言而喻的。_all字段开启适用于不指定搜索某一个字段,根据关键词,搜索整个文档内容
#PUT /index/_mapping/type
{
"date_detection":false
}
PUT /my_index
{
"mappings": {
"my_type": {
"dynamic_templates": [
{ "en": {
"match": "*_en",
"match_mapping_type":"string",
"mapping": {
"type": "string",
"analyzer": "english"
}
}
}
]
}}}
PUT /my_index/my_type/1
{
"title": "this is my first article"
}
PUT /my_index/my_type/2
{
"title_en": "this is my first article"
}
POST /_aliases
{
"actions": [
{ "remove": { "index": "my_index", "alias": "goods_index" }},
{ "add": { "index": "my_index_new", "alias": "goods_index" }}
]
}
TermsAggregationBuilder可根据聚合的情况根据不同条件进行排序
_count
按文档数排序。对 terms
、 histogram
、 date_histogram
有效。
_term
按词项的字符串值的字母顺序排序。只在 terms
内使用。
_key
按每个桶的键值数值排序(理论上与 _term
类似)。 只在 histogram
和 date_histogram
内使用。