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

在很多情况下,混合使用mustshould可以达到最佳的效果。例如,你可以定义一些硬性条件作为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本身不支持直接重命名索引,但你可以通过创建一个新索引并将旧索引中的数据重新索引到新索引中来实现这一目的。这通常涉及以下步骤:

  1. 创建一个新索引:

     Bash 
    1PUT /new_index_name
  2. 将旧索引的数据重新索引到新索引:

    Bash
    
    1POST _reindex
    2{
    3    "source": {
    4        "index": "old_index_name"
    5    },
    6    "dest": {
    7        "index": "new_index_name"
    8    }
    9}
  3. 确认数据已完全迁移后,删除旧索引:

    Bash
    
    1DELETE /old_index_name

请记得将old_index_namenew_index_name替换为实际的索引名称。

在进行重命名操作时,务必小心,确保所有数据都已正确迁移,并在确认无误后再删除旧索引。此外,考虑到性能影响,对于大型索引,应在非高峰时段执行此操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值