ES 利用

打印 上一主题 下一主题

主题 1853|帖子 1853|积分 5559

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1、删除索引的所有记录
  1. curl -X POST "localhost:9200/<index-name>/_delete_by_query" -H 'Content-Type: application/json' -d'
  2. {
  3.   "query": {
  4.     "match_all": {}
  5.   }
  6. }
  7. '
复制代码
POST /content_erp_nlp_help/_delete_by_query
{
  "query": {
    "match_all": {}
  }
}

删除某条记录
DELETE /content_erp_nlp_help/_doc/GGeSU5ABmEtPy2n2IGD-

2、创建索引模板
# Create an index
PUT /content_erp_nlp_help

PUT _template/content_erp_nlp_help
{
  "index_patterns": [
    "content_vector*"
  ],
  "settings": {
    "analysis": {
      "analyzer": {
        "my_ik_analyzer": {
          "type": "ik_smart"
        }
      }
    },
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "content_vector": {
        "type": "dense_vector",
        "similarity": "cosine",
        "index": true,
        "dims": 768,
        "element_type": "float",
        "index_options": {
          "type": "hnsw",
          "m": 16,
          "ef_construction": 128
        }
      },
      "content_answer": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "param": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "type": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}
-------------------------------------------------------------------------------------------------
# Create an index
PUT /content_erp_nlp_help_test

PUT _template/content_erp_nlp_help_test
{
  "index_patterns": [
    "content_vector*"
  ],
  "settings": {
    "analysis": {
      "analyzer": {
        "my_ik_analyzer": {
          "type": "ik_smart"
        }
      }
    },
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "content": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "content_vector": {
        "type": "dense_vector",
        "similarity": "cosine",
        "index": true,
        "dims": 768,
        "element_type": "float",
        "index_options": {
          "type": "hnsw",
          "m": 16,
          "ef_construction": 128
        }
      },
      "content_answer": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "title": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "param": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "type": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      }
    }
  }
}
3
利用must还是should在布尔查询中取决于你的具体需求和渴望的查询行为。
利用 must

当你利用must时,所有包含在must列表中的子查询都必须匹配。这意味着,为了使文档成为搜索结果的一部分,文档必须同时满足所有must查询的条件。如果你的意图是找到同时与文本查询和KNN向量查询匹配的文档,那么利用must是合适的。这通常用于要求严格的匹配场景,比如需要文档同时具有特定的文本特性和与某向量相近。
利用 should

相比之下,should表示查询中的恣意子查询匹配即可。如果至少有一个should子查询匹配,那么文档就会被视为匹配。这提供了更大的灵活性,意味着即使文档只满足文本查询或KNN查询中的一个,它也大概出如今结果中。这对于希望扩大搜索范围,或者答应部分条件匹配的情况非常有用。
效果比较



  • 精确性 vs. 宽泛性: must查询倾向于提供更精确的结果集,因为所有条件都必须满足。而should查询大概产生更宽泛的结果,因为它答应部分条件匹配。
  • 性能影响: 在某些情况下,利用should大概会导致更多的文档被扫描和评估,从而大概略微降低查询性能。这是因为Elasticsearch需要查抄每个should子查询是否匹配。
混合利用 must 和 should

在很多情况下,混合利用must和should可以达到最佳的效果。例如,你可以定义一些硬性条件作为must,确保结果至少满足这些核心需求,同时利用should来增加结果的丰富度和多样性,如:
  1. [/code] Java
  2. [code]1BoolQuery boolQuery = new BoolQuery.Builder()
  3. 2                .must(matchQuery) // 必须满足的文本匹配
  4. 3                .should(knnQuery) // 可以满足的向量相似性
  5. 4                .build();
复制代码
在这个例子中,所有结果必须与文本查询匹配,但如果文档还与KNN查询匹配,则会获得更高的相关性评分,从而大概在结果中排名更高。
总结

选择利用must还是should重要取决于你的业务逻辑和你对查询结果的渴望。如果你需要严格的匹配条件,利用must;如果你希望结果更加宽泛和多样化,利用should;如果你需要两者之间的平衡,考虑混合利用两者。

---------------------------------------------------------------
  1. PUT _template/content_erp_nlp_help_test
  2. {
  3.   "index_patterns": [
  4.     "content_vector*"
  5.   ],
  6.   "settings": {
  7.     "analysis": {
  8.       "analyzer": {
  9.         "my_ik_analyzer": {
  10.           "type": "ik_smart"
  11.         }
  12.       }
  13.     },
  14.     "number_of_shards": 1
  15.   },
  16.   "mappings": {
  17.     "properties": {
  18.       "id": {
  19.         "type": "long"
  20.       },
  21.       "content": {
  22.         "type": "text",
  23.         "analyzer": "ik_max_word",
  24.         "search_analyzer": "ik_smart"
  25.       },
  26.       "content_vector": {
  27.         "type": "dense_vector",
  28.         "similarity": "cosine",
  29.         "index": true,
  30.         "dims": 768,
  31.         "element_type": "float",
  32.         "index_options": {
  33.           "type": "hnsw",
  34.           "m": 16,
  35.           "ef_construction": 128
  36.         }
  37.       },
  38.       "content_answer": {
  39.         "type": "text",
  40.         "analyzer": "ik_max_word",
  41.         "search_analyzer": "ik_smart"
  42.       },
  43.       "title": {
  44.         "type": "text",
  45.         "analyzer": "ik_max_word",
  46.         "search_analyzer": "ik_smart"
  47.       },
  48.       "param": {
  49.         "type": "text",
  50.         "analyzer": "ik_max_word",
  51.         "search_analyzer": "ik_smart"
  52.       },
  53.       "type": {
  54.         "type": "text",
  55.         "analyzer": "ik_max_word",
  56.         "search_analyzer": "ik_smart"
  57.       },
  58.       "questionId": {
  59.         "type": "text",
  60.         "analyzer": "ik_max_word",
  61.         "search_analyzer": "ik_smart"
  62.       },
  63.       "createTime": {
  64.         "type": "text",
  65.         "analyzer": "ik_max_word",
  66.         "search_analyzer": "ik_smart"
  67.       },
  68.       "updateTime": {
  69.         "type": "text",
  70.         "analyzer": "ik_max_word",
  71.         "search_analyzer": "ik_smart"
  72.       },
  73.       "hitCount": {
  74.         "type": "text",
  75.         "analyzer": "ik_max_word",
  76.         "search_analyzer": "ik_smart"
  77.       },
  78.       "answerPattern": {
  79.         "type": "text",
  80.         "analyzer": "ik_max_word",
  81.         "search_analyzer": "ik_smart"
  82.       },
  83.       "questionEnclosureVOList": {
  84.         "type": "text",
  85.         "analyzer": "ik_max_word",
  86.         "search_analyzer": "ik_smart"
  87.       },
  88.       "questionRelationVOList": {
  89.         "type": "text",
  90.         "analyzer": "ik_max_word",
  91.         "search_analyzer": "ik_smart"
  92.       },
  93.       "rmsRoutingAnswerVos": {
  94.         "type": "text",
  95.         "analyzer": "ik_max_word",
  96.         "search_analyzer": "ik_smart"
  97.       }
  98.     }
  99.   }
  100. }
复制代码
----------------
删除 特定label的数据
  1. POST /content_erp_nlp_help_test/_delete_by_query
  2. {
  3.   "query": {
  4.     "term": {
  5.       "label.keyword": {
  6.         "value": "帮助中心FAQ"
  7.       }
  8.     }
  9.   }
  10. }
复制代码
-----------------------------------------------
在Elasticsearch中,你可以利用如下命令来删除索引和重命名索引(实际上是在创建一个新的索引并把数据迁移已往)。
删除索引

要删除一个索引,你可以利用以下API请求:
  1. Bash
复制代码
  1. DELETE /your_index_name
复制代码
或者利用Curl命令行工具:
  1. Bash
复制代码
  1. 1curl -X DELETE "localhost:9200/your_index_name"
复制代码
请确保将your_index_name替换为实际的索引名称。
重命名索引

Elasticsearch本身不支持直接重命名索引,但你可以通过创建一个新索引并将旧索引中的数据重新索引到新索引中来实现这一目标。这通常涉及以下步骤:

  • 创建一个新索引:
    1. [/code] Bash
    2. [code]1PUT /new_index_name
    复制代码
  • 将旧索引的数据重新索引到新索引:
    1. Bash
    复制代码
    1. 1POST _reindex
    2. 2{
    3. 3    "source": {
    4. 4        "index": "old_index_name"
    5. 5    },
    6. 6    "dest": {
    7. 7        "index": "new_index_name"
    8. 8    }
    9. 9}
    复制代码
  • 确认数据已完全迁移后,删除旧索引:
    1. Bash
    复制代码
    1. 1DELETE /old_index_name
    复制代码
请记得将old_index_name和new_index_name替换为实际的索引名称。
在进行重命名利用时,务必警惕,确保所有数据都已正确迁移,并在确认无误后再删除旧索引。此外,考虑到性能影响,对于大型索引,应在非高峰时段实行此利用。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表