Elasticsearch 索引

索引删除,更新

#查看全部索引
GET _cat/indices

#查看索引结构
GET /inspection

#删除索引
DELETE /lib

# 原子修改别名
#索引 my_index_v1 移除别名 my_index
#索引 my_index_v2 添加别名 my_index
POST /_aliases
{
    "actions": [
        { "remove": { "index": "my_index_v1", "alias": "my_index" }},
        { "add":    { "index": "my_index_v2", "alias": "my_index" }}
    ]
}

#索引 my_index_v1 添加别名 my_index
PUT /my_index_v1/_alias/my_index

#数据在索引之间迁移
POST _reindex
{
    "source": {
        "index": "my_index_v1"
    },
    "dest": {
        "index": "my_index_v2"
    }
}

 

mappings 创建索引

#创建索引
/*
settings.number_of_shards 分片数
settings.number_of_replicas 副本
mappings.[?] 文档名?
mappings.[].properties 字段属性
*/

PUT /lib
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 0
    },
    "mappings": {
        "user": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "address": {
                    "type": "text"
                }
            }
        }
    }
}

#定义字段
long类型
"administrationId" : {
    "type" : "long"
},
nalyzer 指定ik分词器
"administrationName" : {
    "type" : "text",
    "analyzer" : "ik_max_word"
},
keyword  不分词,搜索时需要匹配完整的值
"categoryAlias" : {
    "type" : "keyword"
},
keyword不分词,ik分词器
"firstCateName" : {
    "type" : "text",
    "fields" : {
        "keyword" : {
            "type" : "keyword",
            "ignore_above" : 256
        }
    },
    "analyzer" : "ik_max_word",
    "fielddata" : true
},
text ⽤于全⽂索引,搜索时会自动使用分词器进⾏分词再匹配
"administrationName" : {
    "type" : "text"
}
/*
city 字段用于全文本检索;city.raw用于排序与聚合
ignore_above:超过256的字符会被存储,但不会被索引,所以查询内容超过上限不查询
fielddata:text字段查询时内存中的数据结构,如果对text类型字段聚合和排序就需要开启
*/
"city" : {
    "type" : "text",
    "fields" : {
        "raw" : {
            "type" : "keyword",
            "ignore_above" : 256
        }
    },
    "analyzer" : "ik_max_word",
    "fielddata" : true
}

使用
{
    "query": {
        "match": {
            "city": "york" 
        }
    },
    "sort": {
        "city.raw": "asc" 
    },
    "aggs": {
        "Cities": {
            "terms": {
                "field": "city.raw" 
            }
        }
    }
}

时间类型
"notificationDate" : {
    "type" : "date",
    "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
#新增字段
PUT /twitter/_mapping/my_doc 
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}

#新增字段
PUT /my_index/_mapping/my_doc
{
  "properties": {
    "name": {
      "properties": {
        "last": {
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 
    }
  }
}

 

settings 分词器


停用词
"filter" : {
    "jt_tfr" : {
        "type" : "stop",
        "stopwords" : [
            " "
        ]
    }
},


{
    "settings": {
        "index": {
            "number_of_shards": "3",
            "number_of_replicas": "0",
            "max_result_window" : "1000000",
            "analysis" : {
                "filter" : {
                    "jt_tfr" : {
                        "type" : "stop",
                        "stopwords" : [
                            " "
                        ]
                    },
                    "jt_sfr" : {
                        "type" : "synonym",
                        "synonyms_path" : "analysis/synonyms.txt"
                    }
                },
                "analyzer" : {
                    "ik_smart" : {
                        "type" : "ik_smart",
                        "use_smart" : "true"
                    },
                    "ik_max_word" : {
                        "type" : "ik_max_word",
                        "use_smart" : "false"
                    },
                    "jt_cn" : {
                        "filter" : [
                            "jt_tfr",
                            "jt_sfr"
                        ],
                        "char_filter" : [
                            "jt_cfr"
                        ],
                        "type" : "custom",
                        "use_smart" : "true",
                        "tokenizer" : "ik_smart"
                    }
                },
                "char_filter" : {
                    "jt_cfr" : {
                        "type" : "mapping",
                        "mappings" : [
                            "| => \\|"
                        ]
                    }
                }
            }
        }
    },
    "mappings": {}
}

 

keyword

#创建索引,排序字段为keyword,使用keyword 不分词,可排序可聚合
PUT /lib
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 0
    },
    "mappings": {
        "user": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "address": {
                    "type": "text"
                },
                "age": {
                    "type": "integer"
                },
                "interests": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    },
                    "fielddata": true
                },
                "birthday": {
                    "type": "date"
                }
            }
        }
    }
}

# 排序
GET lib/user/_search
{
    "sort": [
        {
            "interests.raw": {
                "order": "desc"
            }
        }
    ]
}

#聚合
POST /lib/user/_search
{
  "size": 0,
  "aggs": {
    "agg_age": {
      "terms": {
        "field": "interests.raw"
      }
    }
  }
}

#分词分析
GET /lib/_analyze
{
  "field": "interests",
  "text": "喝水跑步"
}
#keyword 类型
GET /lib/_analyze
{
  "field": "interests.raw",
  "text": "喝水跑步"
}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值