1、删除索引的所有记录
curl -X POST "localhost:9200/<index-name>/_delete_by_query" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
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
来增加结果的丰富度和多样性,如:
Java
1BoolQuery boolQuery = new BoolQuery.Builder()
2 .must(matchQuery) // 必须满足的文本匹配
3 .should(knnQuery) // 可以满足的向量相似性
4 .build();
在这个例子中,所有结果必须与文本查询匹配,但如果文档还与KNN查询匹配,则会获得更高的相关性评分,从而可能在结果中排名更高。
总结
选择使用must
还是should
主要取决于你的业务逻辑和你对查询结果的期望。如果你需要严格的匹配条件,使用must
;如果你希望结果更加宽泛和多样化,使用should
;如果你需要两者之间的平衡,考虑混合使用两者。
---------------------------------------------------------------
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"
},
"questionId": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"createTime": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"updateTime": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"hitCount": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"answerPattern": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"questionEnclosureVOList": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"questionRelationVOList": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
},
"rmsRoutingAnswerVos": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
----------------
删除 特定label的数据
POST /content_erp_nlp_help_test/_delete_by_query
{
"query": {
"term": {
"label.keyword": {
"value": "帮助中心FAQ"
}
}
}
}
-----------------------------------------------
在Elasticsearch中,你可以使用如下命令来删除索引和重命名索引(实际上是在创建一个新的索引并把数据迁移过去)。
删除索引
要删除一个索引,你可以使用以下API请求:
Bash
DELETE /your_index_name
或者使用Curl命令行工具:
Bash
1curl -X DELETE "localhost:9200/your_index_name"
请确保将your_index_name
替换为实际的索引名称。
重命名索引
Elasticsearch本身不支持直接重命名索引,但你可以通过创建一个新索引并将旧索引中的数据重新索引到新索引中来实现这一目的。这通常涉及以下步骤:
-
创建一个新索引:
Bash1PUT /new_index_name
-
将旧索引的数据重新索引到新索引:
Bash
1POST _reindex 2{ 3 "source": { 4 "index": "old_index_name" 5 }, 6 "dest": { 7 "index": "new_index_name" 8 } 9}
-
确认数据已完全迁移后,删除旧索引:
Bash
1DELETE /old_index_name
请记得将old_index_name
和new_index_name
替换为实际的索引名称。
在进行重命名操作时,务必小心,确保所有数据都已正确迁移,并在确认无误后再删除旧索引。此外,考虑到性能影响,对于大型索引,应在非高峰时段执行此操作。