gpt4 book ai didi

spring-boot - Spring Data Elasticsearch没有给出预期的结果

转载 作者:行者123 更新时间:2023-12-03 01:11:27 24 4
gpt4 key购买 nike

我正在使用Spring数据elasticsearch在我的 flex 文档中查询。
我的Elasticsearch实体类别:

//all the annotation things i.e lombok, de/serializer etc
@Document(indexName = "project", type = "project")
@EqualsAndHashCode
public class ProjectEntity extends CommonProperty implements Serializable {
@Id
private String id;
private String projectName;
private String description;
private String parentProjectId;
private Long projectOwner;
private String projectOwnerName;
private Long projectManager;
private String projectManagerName;
private String departmentId;
private String status;
private String organizationId;

@Field(type = FieldType.Nested)
private List<ActionStatusEntity> actionStatusList= new ArrayList<>();

@Field(type = FieldType.Nested)
private List<TeamMember> teamMemberList;

@Field(type = FieldType.Nested)
private List<UserDefineProperty> riskList;

}
我做了其他事情,例如设置存储库,为了简洁起见。
资料搜寻:
    projectRepository.findByOrganizationIdAndProjectName(userEntity.getOrganizationId().toString() ,projectRequest.getProjectName().trim());
//userEntity.getOrganizationId().toString()="28", projectName="Team Test"
Spring为上述调用生成查询:
{
"from": 0,
"size": 10000,
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "28",
"fields": [
"organizationId^1.0"
],
"type": "best_fields",
"default_operator": "and",
"max_determinized_states": 10000,
"enable_position_increments": true,
"fuzziness": "AUTO",
"fuzzy_prefix_length": 0,
"fuzzy_max_expansions": 50,
"phrase_slop": 0,
"escape": false,
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
},
{
"query_string": {
"query": "Team Test",
"fields": [
"projectName^1.0"
],
"type": "best_fields",
"default_operator": "and",
"max_determinized_states": 10000,
"enable_position_increments": true,
"fuzziness": "AUTO",
"fuzzy_prefix_length": 0,
"fuzzy_max_expansions": 50,
"phrase_slop": 0,
"escape": false,
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
"version": true
}
查询结果:
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 4.1767306,
"hits" : [
{
"_index" : "project",
"_type" : "project",
"_id" : "215",
"_version" : 2,
"_score" : 4.1767306,
"_source" : {
"projectName" : "team member only test",
"description" : "team member only test",
"projectOwner" : 50,
"projectOwnerName" : "***",
"departmentId" : "team member only test",
"organizationId" : "28"
}
},
{
"_index" : "project",
"_type" : "project",
"_id" : "408",
"_version" : 17,
"_score" : 4.1767306,
"_source" : {

"projectName" : "Category & Team adding test",
"description" : "Category & Team adding test",
"projectOwner" : 50,
"projectOwnerName" : "***",
"projectManager" : 50,
"projectManagerName" : "***",
"departmentId" : "cat",

"organizationId" : "28"
}
},
{
"_index" : "project",
"_type" : "project",
"_id" : "452",
"_version" : 4,
"_score" : 3.4388955,
"_source" : {

"projectName" : "team member not in system test",
"description" : "id-452",
"projectOwner" : 53,
"projectOwnerName" : "***",
"projectManager" : 202,
"projectManagerName" : "***",
"departmentId" : "abc",
"organizationId" : "28",
}
}
]
}
}
查看结果集,检查 projectName字段值,就像 contains方法一样!它没有检查完整的给定参数。
为什么会这样呢?如何解决?
添加:organizationId和projectName字段设置为 fieldData=true

最佳答案

Spring Data Elasticsearch从方法名称派生的查询是一个Elasticsearch字符串查询,具有您所注意到的给定参数。对于这些Elasticsearch,将分析并解析术语,然后搜索具有相同顺序的这些术语的文档。
您使用“团队测试”进行的查询有两个术语“团队”和“测试”,并且您显示的所有文档在项目名称中都有这些术语,因此将它们返回。
如果您的文档带有“团队测试”,并且两者之间没有其他术语,则将以更高的分数返回。
选择该实现的原因是,在Elasticsearch中进行搜索时通常会期望这种实现。具有名称索引并搜索“Harry Miller”的图像将找不到“Harry B. Miller”的文档。
您可以编写一个自定义的存储库方法,以构建满足您需求的查询,然后改用它。或者,如果您始终想对该字段进行精确搜索,则可以将其定义为keyword字段,以防止进行分析和分析。
您可以在此存储库方法定义中使用match_phrase查询(仅在此处使用一个参数,您需要添加组织ID,但是对于这个小代码示例而言,生成的查询太复杂了):

@Query("{\"match_phrase\": {\"projectName\": \"?0\"}}\n")
SearchHits<ProjectEntity> findByProjectName(String name);

关于spring-boot - Spring Data Elasticsearch没有给出预期的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64151693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com