常用Rest Api
查询文档
POST /twitter/_search
{
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "some query string here"
}
},
"filter" : {
"term" : { "user" : "kimchy" }
}
}
}
}
查看mapping
GET /twitter/_mapping
删除索引
DELETE /twitter
根据ID删除索引
DELETE /twitter/_doc/123
根据查询条件删除索引
POST /twitter/_delete_by_query
{
"query": {
"bool" : {
"must" : {
"query_string" : {
"query" : "some query string here"
}
},
"filter" : {
"term" : { "user" : "kimchy" }
}
}
}
}
中文分词
参考:https://www.imooc.com/article/20637
测试
POST /_analyze
{
"analyzer":"ik_smart",
"text":"手机充值"
}
响应:
{
"tokens": [
{
"token": "手机",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"token": "充值",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
}
]
}
不停服修改Mapping
参考:http://blog.51yip.com/server/1915.html
1、创建新索引和新mapping
PUT /twitter_v2
{
"mappings": {}
}
2、使用 reindex 将原来的索引重建到新的索引上
POST /_reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "twitter_v2"
}
}
3、创建alias 别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "twitter_v2",
"alias": "twitter"
}
},
{
"remove_index": {
"index": "twitter"
}
}
]
}
4、同步更新后的mapping到项目实体类中,发布项目
@Document(indexName = "twitter_v2")
4、删除原索引
DELETE /twitter
遇到的一些问题
解决ElasticSearch深度分页机制中Result window is too large问题
参考:https://blog.csdn.net/lisongjia123/article/details/79041402
用id可以查出,businessId查不出来
解决:将字段类型改成@Field(type = FieldType.Keyword),默认是Text,对于“-”“/”不能准备匹配
POST /twitter/_search?from=0&size=10
{
"query" : {
"term" : {
"id" : "09759c2fccf4bc00c60040d2568e721f"
}
}
}
{
"query" : {
"term" : {
"businessId" : "94aec9ce-0203-4cfd-a643-765fdc2da495"
}
}
}