河曲智叟 发表于 2024-11-12 21:19:22

ES 利用

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

[*] 创建一个新索引:
Bash

1PUT /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替换为实际的索引名称。
在进行重命名利用时,务必警惕,确保所有数据都已正确迁移,并在确认无误后再删除旧索引。此外,考虑到性能影响,对于大型索引,应在非高峰时段实行此利用。


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