我正在使用spring数据elasticsearch来查询我的elastic文档。我的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
2条答案
按热度按时间eoigrqb61#
spring data elasticsearch从方法名派生的查询是一个带有给定参数的elasticsearch字符串查询。对于这些elasticsearch,分析和解析术语,然后搜索具有相同顺序的术语的文档。
使用“teamtest”的查询有两个术语“team”和“test”,并且您显示的所有文档在项目名称中都有这些术语,因此它们将被返回。
如果您有一个带有“团队测试”的文档,而这两个文档之间没有其他术语,则返回的分数会更高。
之所以选择此实现,是因为它是在elasticsearch中搜索时通常需要的。如果图像有一个带有名称的索引并搜索“harry miller”,则找不到带有“harry b”的文档。米勒”。
您可以编写一个自定义存储库方法来构建满足您需要的查询,然后使用它。或者,如果您总是希望对此字段进行精确搜索,可以将其定义为
keyword
字段来阻止解析和分析。您可以将匹配短语查询与此存储库方法定义一起使用(此处仅使用一个参数,您需要添加组织id,但结果查询对于这个小代码示例来说过于复杂):
sbtkgmzw2#
我不知道SpringDataElasticSearch,但是添加了一个使用json格式的索引数据、搜索查询和搜索结果的工作示例
索引数据:
索引了上述三个文件(给出了问题),并插入了第四个文件,如下所示。
搜索查询:
搜索结果: