• Elastic Search 7.x 学习笔记


    h1. 创建索引

     PUT http://localhost:9200/shopping

    1. {
    2. "acknowledged": true,
    3. "shards_acknowledged": true,
    4. "index": "shopping"
    5. }

     重复创建索引

    1. {
    2. "error": {
    3. "root_cause": [
    4. {
    5. "type": "resource_already_exists_exception",
    6. "reason": "index [shopping/UgVEWxBdTOWi7DO9TfZUfw] already exists",
    7. "index_uuid": "UgVEWxBdTOWi7DO9TfZUfw",
    8. "index": "shopping"
    9. }
    10. ],
    11. "type": "resource_already_exists_exception",
    12. "reason": "index [shopping/UgVEWxBdTOWi7DO9TfZUfw] already exists",
    13. "index_uuid": "UgVEWxBdTOWi7DO9TfZUfw",
    14. "index": "shopping"
    15. },
    16. "status": 400
    17. }

    2. 查看所有索引

    GET http://localhost:9200/_cat/indices?v    (v代表携带表头)

    1. health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
    2. yellow open holmes_im_message_202207 a3Y4s1kNQj6TNgknOAcZEQ 5 1 0 0 1.2kb 1.2kb
    3. yellow open shopping UgVEWxBdTOWi7DO9TfZUfw 5 1 0 0 1.1kb 1.1kb
    4. yellow open users RA2vnPmkQhqjegKPcwiYag 5 1 2 0 9.3kb 9.3kb
    5. yellow open flink_kafka_to_es_user pYsiaUTfQm6qVtR82dTRIA 5 1 10 0 24kb 24kb
    6. yellow open my_index YzWyj2KrS7-ZBba0tTUa6g 5 1 5 0 13.6kb 13.6kb
    7. yellow open nba v2yMIG0ZRkCZ54w9KBHXWA 5 1 10 0 40.3kb 40.3kb
    8. yellow open woodpecker_clue_life_cycle_log_202207 4puKGmyjQJe9zx2VckkC7g 5 1 791 0 577.4kb 577.4kb
    9. green open .kibana nZWGcHfKTCCtzrmCXdtN1A 1 0 2 0 10.8kb 10.8kb

    3. 删除索引

    DELETE http://localhost:9200/shopping

    4. 创建文档

    POST http://localhost:9200/shopping/_doc (POST不是幂等性,PUT是幂等性,添加数据每次返回ID是唯一标识,所以要用POST)

    1. {
    2. "title": "小米手机",
    3. "category": "小米",
    4. "images": "https://www.baidu.com",
    5. "price": 3999.00
    6. }

    返回结果

    1. {
    2. "_index": "shopping",
    3. "_type": "_doc",
    4. "_id": "lIuU2IIBp4Wa2GZ3RcL_",
    5. "_version": 1,
    6. "result": "created",
    7. "_shards": {
    8. "total": 2,
    9. "successful": 1,
    10. "failed": 0
    11. },
    12. "_seq_no": 0,
    13. "_primary_term": 1
    14. }

    PUT http://localhost:9200/shopping/_doc/1001 (这里1001指定了ID 所以满足了幂等性,可以用PUT,但是_version会+1)

    1. {
    2. "_index": "shopping",
    3. "_type": "_doc",
    4. "_id": "1001",
    5. "_version": 1,
    6. "result": "created",
    7. "_shards": {
    8. "total": 2,
    9. "successful": 1,
    10. "failed": 0
    11. },
    12. "_seq_no": 1,
    13. "_primary_term": 1
    14. }

    5. 查询文档

    GET http://localhost:9200/shopping/_doc/1001

    1. {
    2. "_index": "shopping",
    3. "_type": "_doc",
    4. "_id": "1001",
    5. "_version": 2,
    6. "found": true,
    7. "_source": {
    8. "title": "小米手机",
    9. "category": "小米",
    10. "images": "https://www.baidu.com",
    11. "price": 3999.00
    12. }
    13. }

    6. 查询索引下全部文档

    GET http://localhost:9200/shopping/_search

    1. {
    2. "took": 178,
    3. "timed_out": false,
    4. "_shards": {
    5. "total": 5,
    6. "successful": 5,
    7. "skipped": 0,
    8. "failed": 0
    9. },
    10. "hits": {
    11. "total": 2,
    12. "max_score": 1.0,
    13. "hits": [
    14. {
    15. "_index": "shopping",
    16. "_type": "_doc",
    17. "_id": "lIuU2IIBp4Wa2GZ3RcL_",
    18. "_score": 1.0,
    19. "_source": {
    20. "title": "小米手机",
    21. "category": "小米",
    22. "images": "https://www.baidu.com",
    23. "price": 3999.00
    24. }
    25. },
    26. {
    27. "_index": "shopping",
    28. "_type": "_doc",
    29. "_id": "1001",
    30. "_score": 1.0,
    31. "_source": {
    32. "title": "小米手机",
    33. "category": "小米",
    34. "images": "https://www.baidu.com",
    35. "price": 3999.00
    36. }
    37. }
    38. ]
    39. }
    40. }

    7. 更新索引文档

    7.1 全量更新

    PUT http://localhost:9200/shopping/_doc/1001

    1. {
    2. "price": 4999.00
    3. }

    返回结果:别的字段都没有了 ,所以要包含全部的字段和值!

    1. {
    2. "_index": "shopping",
    3. "_type": "_doc",
    4. "_id": "1001",
    5. "_version": 3,
    6. "found": true,
    7. "_source": {
    8. "price": 4999.00
    9. }
    10. }

    7.2 局部更新

    POST http://localhost:9200/shopping/_update/1001

    1. {
    2. "doc":{
    3. "price": 4999.00
    4. }
    5. }

    更新后结果:

    1. {
    2. "_index": "shopping",
    3. "_type": "_doc",
    4. "_id": "1001",
    5. "_version": 2,
    6. "_seq_no": 1,
    7. "_primary_term": 1,
    8. "found": true,
    9. "_source": {
    10. "title": "小米手机",
    11. "category": "小米",
    12. "images": "https://www.baidu.com",
    13. "price": 4999.0
    14. }
    15. }

    8. 删除索引文档

    DELETE  http://localhost:9200/shopping/_doc/1001

    9. 文档查询

    GET http://localhost:9200/shopping/_search?q=category:小米

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "match":{
    4. "category": "大米"
    5. }
    6. }
    7. }

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "match_all":{
    4. }
    5. }
    6. }

    10. 分页查询

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "match_all":{
    4. }
    5. },
    6. "from":0,
    7. "size":3
    8. }

    11. 筛选查询结果字段 排序

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "match_all":{
    4. }
    5. },
    6. "from":0,
    7. "size":3,
    8. "_source": ["title","price"],
    9. "sort": {
    10. "price":{
    11. "order": "desc"
    12. }
    13. }
    14. }

    12. 查询must or should

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "bool":{
    4. "should":[
    5. {
    6. "match":{
    7. "category": "小米"
    8. }
    9. },
    10. {
    11. "match":{
    12. "price": 4999.0
    13. }
    14. }
    15. ]
    16. }
    17. }
    18. }

    13. 查询 过滤 filter

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "bool":{
    4. "should":[
    5. {
    6. "match":{
    7. "category": "小米"
    8. }
    9. },
    10. {
    11. "match":{
    12. "price": 4999.0
    13. }
    14. }
    15. ],
    16. "filter":{
    17. "range":{
    18. "price":{
    19. "gt": 4000
    20. }
    21. }
    22. }
    23. }
    24. }
    25. }

     14. 查询 match_phrase

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "bool":{
    4. "should":[
    5. {
    6. "match_phrase":{
    7. "category": "小米"
    8. }
    9. }
    10. ]
    11. }
    12. }
    13. }

    原理:18.match_phrase的用法 - outback123 - 博客园

     15. 高亮查询

    GET http://localhost:9200/shopping/_search

    1. {
    2. "query":{
    3. "bool":{
    4. "must":[
    5. {
    6. "match_phrase":{
    7. "category": "小米"
    8. }
    9. }
    10. ]
    11. }
    12. },
    13. "highlight":{
    14. "fields":{
    15. "category": {}
    16. }
    17. }
    18. }

  • 相关阅读:
    Java-使用sqlSessionTemplate实现批量更新-模拟mybatis 动态sql
    EFCore学习笔记(5)——生成值
    基于海鸥算法的无人机航迹规划-附代码
    刷爆力扣之检查数组对是否可以被 k 整除
    投影仪怎么安装小容量软件?5款小体积应用下载搞定内存不足
    MyBatis-plus使用
    1300*C. Coin Rows(枚举&模拟)
    angular12 angular.json
    JavaWeb 学习笔记 1:MyBatis
    来自男装的“制服诱惑”,这波设计够酷
  • 原文地址:https://blog.csdn.net/qq_16397653/article/details/126541369