• elasticsearch


    这里操作的工具是postman。

    Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,它集中存储您的数据,帮助你发现意料之中以及意料之外的情况。

    elasticsearch是面向文档的,文档是索引和搜索的基本单位。文档以类型来分组,类型包含若干文档。一个或多个类型存在于同一索引中,索引是更大的容器。

    可以将elasticsearch的索引比作数据库,类型就是表,文档比作的行

    安装与启动

    下载地址:https://www.elastic.co/cn/downloads/elasticsearch

    解压后把config/elasticsearch.yml中的下面内容改为false,否则会连接不上。

    执行bin\elasticsearch.bat,启动服务。

     在浏览器地址中输入localhost:9200测试是否能连接启动,出现串JSON数据就表示成功启动了。

    安装分词器

    下载分词器:https://github.com/medcl/elasticsearch-analysis-ik/releases

    在plugins目录下新建ik目录,下载后解压到ik目录,重启elasticsearch.bat

    elasticsearch的基本使用

    对books数据库进行基本的增删查。

    作用请求类型请求语句
    创建PUThttp://localhost:9200/books
    查询PUThttp://localhost:9200/books
    删除DELETEhttp://localhost:9200/books

    创建索引并指定规则(示例)

    请求类型PUT,请求路径:http://localhost:9200/books

    请求body

    1. {
    2. "mappings":{
    3. "properties":{
    4. "id":{
    5. "type":"keyword"
    6. },
    7. "name":{
    8. "type":"text",
    9. "analyzer":"ik_max_word",
    10. "copy_to":"all"
    11. },
    12. "type":{
    13. "type":"keyword"
    14. },
    15. "description":{
    16. "type":"text",
    17. "analyzer":"ik_max_word",
    18. "copy_to":"all"
    19. },
    20. "all":{
    21. "type":"text",
    22. "analyzer":"ik_max_word"
    23. }
    24. }
    25. }
    26. }

    创建文档

    url语句:

    作用请求类型请求语句
    使用系统生成的idPUSThttp://localhost:9200/books/_doc
    使用指定idPOSThttp://localhost:9200/books/_create/1
    使用指定id,不存在创建,存在更新(版本递增)POSThttp://localhost:9200/books/_doc/1

    body中的语句:

    1. {
    2. 'name':"springboot",
    3. 'type':"计算机系类书籍",
    4. 'description':"springboot这个框架很好用"
    5. }

    查询文档与删除文档

    条件查询:地址/数据库名/_search?q=字段名:关键字

    例如:http://localhost:9200/books/_search?q=name:springboot

    查询文档时默认有分页的效果,每10条分一页。

     

    修改文档

    全量修改(全量修改)

    put类型,url为:http://localhost:9200/books/_doc/1,修改id为1的数据。

    body中的数据

    1. {
    2. 'name':"springbootnew",
    3. 'type':"springbootnew类型",
    4. 'description':"springbootnew很好"
    5. }

     全量修改(部分修改)

    post类型,url为:http://localhost:9200/books/_update/1,修改id为1的数据。

    body中的数据

    1. {
    2. "doc":{
    3. 'name':"springbootnew"
    4. }
    5. }

    SpringBoot整合elasticsearch

    创建springBoot项目,勾选elasticsearch

    在application中配置elasticsearch。

    1. spring:
    2. elasticsearch:
    3. rest:
    4. uris: http://localhost:9200

    查询示例

    1. @SpringBootTest
    2. class JavaProApplicationTests {
    3. @Autowired
    4. private BookDao bookDao;
    5. @Autowired
    6. private RestHighLevelClient client;
    7. @Test
    8. //创建一个books的索引(数据库)
    9. void createIndex() throws IOException {
    10. CreateIndexRequest request = new CreateIndexRequest("books");
    11. client.indices().create(request, RequestOptions.DEFAULT);
    12. }
    13. @Test
    14. //创建一个books的索引(数据库)并指定规则
    15. void createIndexByIK() throws IOException {
    16. CreateIndexRequest request = new CreateIndexRequest("books");
    17. //把写在body中的json写在这里
    18. String json = "{\"mappings\":{\"properties\":{\"id\":{\"type\":\"keyword\"},\"name\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\",\"copy_to\":\"all\"},\"type\":{\"type\":\"keyword\"},\"description\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\",\"copy_to\":\"all\"},\"all\":{\"type\":\"text\",\"analyzer\":\"ik_max_word\"}}}}";
    19. request.source(json, XContentType.JSON);
    20. client.indices().create(request, RequestOptions.DEFAULT);
    21. }
    22. @Test
    23. //创建文档
    24. void createDoc() {
    25. IndexRequest request = new IndexRequest("books").id("9");
    26. //把写在body中的json写在这里
    27. String json = "{\"name\":\"李义新999\",\"type\":\"person\",\"description\":\"很棒\"}";
    28. request.source(json, XContentType.JSON);
    29. try {
    30. client.index(request, RequestOptions.DEFAULT);
    31. } catch (IOException e) {
    32. }
    33. }
    34. @Test
    35. //创建全文档,把mysql中读取的数据取出来放到elasticsearch中
    36. void createDocAll() throws IOException {
    37. List books = bookDao.selectList(null);
    38. BulkRequest bulk = new BulkRequest();
    39. for (Book book : books) {
    40. IndexRequest request = new IndexRequest("books").id(book.getId().toString());
    41. String json = JSON.toJSONString(book);
    42. request.source(json, XContentType.JSON);
    43. bulk.add(request);
    44. }
    45. try {
    46. client.bulk(bulk, RequestOptions.DEFAULT);
    47. } catch (Exception e) {
    48. }
    49. }
    50. @Test
    51. //查询索引(数据库)中的全部内容
    52. void GetAll() throws IOException {
    53. SearchRequest request = new SearchRequest();
    54. request.indices("books");
    55. request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
    56. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    57. SearchHits hits = response.getHits();
    58. System.out.println(hits.getTotalHits()); //查询条数
    59. System.out.println(response.getTook()); //查询时长
    60. for (SearchHit hit : hits) {
    61. System.out.println(hit.getSourceAsString());
    62. }
    63. }
    64. @Test
    65. //按条件查询索引(数据库)中的内容
    66. void Get() throws IOException {
    67. SearchRequest request = new SearchRequest("books");
    68. SearchSourceBuilder builder = new SearchSourceBuilder();
    69. builder.query(QueryBuilders.termQuery("name", ""));
    70. request.source(builder);
    71. SearchResponse search = client.search(request, RequestOptions.DEFAULT);
    72. SearchHits hits = search.getHits();
    73. SearchHit[] sh = hits.getHits();
    74. for (SearchHit h : sh) {
    75. String source = h.getSourceAsString();
    76. System.out.println(source);
    77. }
    78. }
    79. }

    扩展

    比如输入url语句按条件查询: http://localhost:9200/books/_search?q=name:计算机

    要查询_search中的数据,先获取hits,再通过hits获取下一级的hits数组,通过hist数组获取里面search中的数据。

  • 相关阅读:
    Sentinel的简单介绍和使用
    RestTemplate使用
    长牌游戏功能整理
    成都优优聚代运营:打造精细化运营新标杆
    PAT 1024 Palindromic Number(高精度加法)
    Ubuntu虚拟机安装
    高级工技能等级认定理论部分 看了就过关
    目标检测|边框检测框转换,交并比计算 代码实现
    C++各知识点参考资料汇总(不定期更新)
    六级高频词汇——Group07
  • 原文地址:https://blog.csdn.net/LYXlyxll/article/details/126770070