0

我正在使用 Spring data elasticsearch 在我的弹性文档中进行查询。我的 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

4

2 回答 2

1

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);
于 2020-10-01T10:29:15.490 回答
0

我不知道 Spring Data Elasticsearch,但添加了一个带有 JSON 格式的索引数据、搜索查询和搜索结果的工作示例

指数数据:

索引所有上述三个文档(有问题),并插入第四个文档,如下所示。

{
    "projectName": "team test",
    "description": "id-452",
    "projectOwner": 53,
    "projectOwnerName": "***",
    "projectManager": 202,
    "projectManagerName": "***",
    "departmentId": "abc",
    "organizationId": "28"
}

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "organizationId": 28
          }
        },
        {
          "multi_match": {
            "query": "Team Test",
            "type": "phrase",
            "fields": [
              "projectName"
            ]
          }
        }
      ]
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64151693",
        "_type": "_doc",
        "_id": "4",
        "_score": 0.5003766,
        "_source": {
          "projectName": "team test",
          "description": "id-452",
          "projectOwner": 53,
          "projectOwnerName": "***",
          "projectManager": 202,
          "projectManagerName": "***",
          "departmentId": "abc",
          "organizationId": "28"
        }
      }
    ]
于 2020-10-01T09:51:48.157 回答