ElasticSearch7 常用Restful Api(使用总结,持续更新)

目录

简述

常用api

创建索引和mapping

 scroll深度分页查询

 删除文档

 基本查询

 多值匹配

 多字段匹配

ElasticSearch7无感知修改Mapping

创建新的索引mapping

reindex复制索引数据

问题

修改关联别名

删除旧索引(可选)

复制索引

suggest complete功能的实现(要安装ik,pinyin插件)


简述

7.x版本对比5.x版本,移除的type字段,默认使用_doc type类型

常用api

创建索引和mapping

PUT datacenter_laws
{
  "settings": {
    "number_of_shards": 3,
    "index.refresh_interval": "5s"
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword",
        "ignore_above": 256
      },
      "pick_url": {
        "type": "keyword",
        "ignore_above": 256
      },
      "content_html": {
        "type": "text"
      },
      "content_text": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "suggest": {
            "type": "completion"
          }
        }
      },
      "effective_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "effectiveness": {
        "type": "keyword",
        "ignore_above": 256
      },
      "issue_authority": {
        "type": "keyword",
        "ignore_above": 256
      },
      "pick_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "issue_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "issue_no": {
        "type": "keyword",
        "ignore_above": 256
      },
      "site_name": {
        "type": "keyword",
        "ignore_above": 256
      },
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          },
          "suggest": {
            "type": "completion"
          }
        },
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "type1": {
        "type": "keyword",
        "ignore_above": 256
      },
      "type2": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}

 scroll深度分页查询

不支持跳页,跳页用最简单的from,size


GET datacenter_laws/_search
{
  "size": 1,
  "query": {
    "multi_match": {
      "query": "学习",
      "fields": ["title","content_text"]
    }
  },
  "sort": [
    {
      "issue_date": {
        "order": "asc"
      },
      "id": {
        "order": "desc"
      }
    }
  ]
}
#获取到search_after需要的值,然后分页查询
GET datacenter_laws/_search
{
  "size": 1,
  "query": {
    "multi_match": {
      "query": "学习",
      "fields": ["title","content_text"]
    }
  },
  "sort": [
    {
      "issue_date": {
        "order": "asc"
      },
      "id": {
        "order": "desc"
      }
    }
  ],
  "search_after": [
    1625992020000,
    "37f9a47944c10ea6ede98c9e7d92cb43"
  ]
}

 删除文档

#删除指定id索引
DELETE datacenter_laws/_doc/uRW4U3sBTAVER0k-Kpav
#多id删除
POST oa_material_document/_delete_by_query
{
  "query": {
    "terms": {
      "FIELD": [
        "VALUE1",
        "VALUE2"
      ]
    }
  }
}

 基本查询

#查询
GET datacenter_laws/_search
#查询mapping
GET datacenter_laws/_mapping
#搜索建议自动完成
POST datacenter_laws/_search
{
  "suggest": {
    "my-suggest": {
      "text": "学",
      "completion": {
        "field": "title.suggest"
      }
    }
  }
}

 多值匹配

GET datacenter_laws/_search
{
  "query": {
    "terms": {
      "FIELD": [
        "VALUE1",
        "VALUE2"
      ]
    }
  }
}

 多字段匹配

GET datacenter_laws/_search
{
  "query": {
    "multi_match": {
      "query": "扶贫",
      "fields": ["title","content_text"]
    }
  },
"from": 0,
"size": 20
}

ElasticSearch7无感知修改Mapping

  • 创建新的索引mapping

PUT datacenter_laws_v2
{
  "settings": {
    "number_of_shards": 3,
    "index.refresh_interval": "5s"
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword",
        "ignore_above": 256
      },
      "pick_url": {
        "type": "keyword",
        "ignore_above": 256
      },
      "content_html": {
        "type": "text"
      },
      "content_text": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart",
        "fields": {
          "suggest": {
            "type": "completion"
          }
        }
      },
      "effective_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "effectiveness": {
        "type": "keyword",
        "ignore_above": 256
      },
      "issue_authority": {
        "type": "text"
      },
      "pick_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "issue_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "issue_no": {
        "type": "keyword",
        "ignore_above": 256
      },
      "site_name": {
        "type": "keyword",
        "ignore_above": 256
      },
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          },
          "suggest": {
            "type": "completion"
          }
        },
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "type_first": {
        "type": "keyword",
        "ignore_above": 256
      },
      "type_second": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}
  • reindex复制索引数据

POST _reindex
{
  "source": {
    "index": "datacenter_laws_v1"
  },
  "dest": {
    "index": "datacenter_laws_v2"
  }
}
  • 问题

如果数据量量较大,报错是正常的,但是后台会需求复制数据

  • 修改关联别名

POST /_aliases
{
    "actions": [
        { "remove": { "index": "datacenter_laws_v1", "alias": "datacenter_laws" }},
        { "add":    { "index": "datacenter_laws_v2", "alias": "datacenter_laws" }}
    ]
}
  • 删除旧索引(可选)

复制索引

PUT [index]/_settings
{
  "settings": {
    "index.blocks.write": false
  }
}

POST [index]/_clone/[new_index]

PUT [index]/_settings
{
  "settings": {
    "index.blocks.write": true
  }
}

suggest complete功能的实现(要安装ik,pinyin插件)

PUT xxx_suggest_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "full_pinyin_letter_analyzer": {
          "tokenizer": "full_pinyin_letter"
        },
        "default": {
          "tokenizer": "ik_max_word"
        },
        "first_py_letter_analyzer": {
          "tokenizer": "first_py_letter"
        },
        "pinyin_analyzer": {
          "tokenizer": "shopmall_pinyin"
        }
      },
      "tokenizer": {
        "shopmall_pinyin": {
          "keep_joined_full_pinyin": "true",
          "lowercase": "true",
          "keep_none_chinese_in_joined_full_pinyin": "true",
          "keep_original": "true",
          "keep_first_letter": "true",
          "keep_separate_first_letter": "false",
          "type": "pinyin",
          "limit_first_letter_length": "16",
          "keep_full_pinyin": "true"
        },
        "first_py_letter": {
          "keep_none_chinese_in_first_letter": "false",
          "lowercase": "true",
          "none_chinese_pinyin_tokenize": "false",
          "keep_none_chinese_in_joined_full_pinyin": "true",
          "keep_original": "false",
          "keep_first_letter": "true",
          "trim_whitespace": "true",
          "type": "pinyin",
          "keep_none_chinese": "true",
          "limit_first_letter_length": "16",
          "keep_full_pinyin": "false"
        },
        "full_pinyin_letter": {
          "keep_joined_full_pinyin": "true",
          "keep_none_chinese_in_first_letter": "false",
          "lowercase": "true",
          "none_chinese_pinyin_tokenize": "false",
          "keep_none_chinese_in_joined_full_pinyin": "true",
          "keep_original": "false",
          "keep_first_letter": "false",
          "keep_separate_first_letter": "false",
          "type": "pinyin",
          "keep_none_chinese": "true",
          "limit_first_letter_length": "16",
          "keep_full_pinyin": "false"
        }
      }
    }
  },
  "mappings": {
  "properties": {
    "name": {
      "type": "completion",
      "analyzer": "simple",
      "preserve_separators": true,
      "preserve_position_increments": true,
      "max_input_length": 50,
      "fields": {
        "keyword_first_py": {
          "type": "completion",
          "analyzer": "first_py_letter_analyzer",
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50
        },
        "keyword_pinyin": {
          "type": "completion",
          "analyzer": "full_pinyin_letter_analyzer",
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50
        },
        "pinyin": {
          "type": "completion",
          "analyzer": "pinyin_analyzer",
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50
        }
      }
    },
    "type":{
      "type": "keyword"
    },
    "id":{
      "type": "keyword"
    }
  }
  }
}

使用别名,修改mapping

POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "dataintell_comment1",
        "alias": "dataintell_comment"
      }
    },
    {
      "add": {
        "index": "dataintell_comment2",
        "alias": "dataintell_comment"
      }
    }
  ]
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值