Elasticsearch嵌套查询无结果:目标positionID存在却未返回
问题:Elasticsearch查询无结果排查
问题场景
执行了以下Elasticsearch查询,明明存在包含目标positionID的文档,但查询始终返回空结果。
执行的查询
GET /My_index_here/_search { "query": { "bool": { "must": [ { "nested": { "path": "positions", "query": { "bool": { "must": [ { "wildcard": { "positions.positionID": "04b698d0-afbf-4c09-a21a-d02ec8efaa70" } } ] } } } } ] } } }
存在的目标文档示例
{ "took" : 60, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "manpower_cv_index_analyzed_accseparated_new3", "_type" : "_doc", "_id" : "bf03fe1d-4cb0-4e6f-a8a8-3c4b74e9cda3", "_score" : 1.0, "_source" : { "primaryKey" : "bf03fe1d-4cb0-4e6f-a8a8-3c4b74e9cda3", "addressList" : [ { "city" : "", "country" : "", "fullAddress" : "", "location" : { "longitudeLatitude" : { "lat" : -2.0, "lon" : -2.0 } }, "prov" : "" } ], "allSkills" : "", "allSoftskills" : "null", "awards" : "null", "corpusId" : "BODY", "custBirthDateRange" : "NoAge", "custEMail" : "test@yahoo.com", "custFirstName" : "ewe", "custLastName" : "rfwe", "username" : "testk12@yahoo", "genderID" : 0, "custMiddleName" : "", "custOrigine" : "R", "custPBCandID" : "null", "custSesso" : "null", "custURLcvR" : "", "custUpdateDate" : "2023-03-03T08:19:50.2300000", "candidateUpdateDate" : "0001-01-01T00:00:00", "createdDate" : "2023-02-15T19:24:56.1070000", "recruiterActivityRange" : "1W", "reconnectionDateRange" : "N/A", "cvFile" : "", "cvId" : "00000000-0000-0000-0000-000000000000", "cvLanguage" : "English", "cvText" : "", "plText" : "\"\"\"\"", "email" : "test@yahoo.com", "facebookID" : "", "firstname" : "null", "fullname" : "ewe rfwe", "displayName" : "ewe rfwe", "lastname" : "null", "linkedInID" : "", "references" : "", "skypeID" : "", "twitterID" : "null", "viadeoID" : "null", "recruiterComments" : "Recruiter has modified profile details, Recruiter has modified profile details, Recruiter has modified profile details, Recruiter has modified profile details", "reconnectionDate" : "0001-01-01T00:00:00", "lastTimeIndexed" : "2023-03-03T08:23:23.2328586+00:00", "RCandidateId" : "bf03fe1d-4cb0-4e6f-a8a8-3c4b74e9cda3", "RAccountId" : [ "bc73afda-d4f2-42ae-9ec9-32a86e8e445e" ], "Tags" : [ ], "idType" : 0, "employeeId" : "", "Nationality" : 0, "mobilePhone" : "N/A", "otherPhone" : "", "bestTimeToCall" : "", "expectedSalary" : 0, "ExpectedSalaryRange" : "<5k", "minExpectedSalary" : 0.0, "maxExpectedSalary" : 0.0, "expectedSalaryPer" : 0, "expectedSalaryCurrency" : 21, "salaryPerHours" : { "gt" : 0.0, "gte" : 0.0, "lt" : 0.0, "lte" : 0.0 }, "marketingNotifications" : false, "contactByEmail" : false, "contactByPhone" : false, "contactByText" : false, "contractTypeDesc" : "", "Classification" : [ ], "score" : 0.0, "isGeneralPrivacy" : true, "generalPrivacyDate" : "2023-02-15T19:24:56.1070000", "isLegalNoticePrivacy" : true, "legalNoticeSignedDate" : "2023-02-15T19:24:56.1070000", "isMarketingPrivacy" : false, "marketingPrivacyDate" : "2023-02-15T19:24:56.1070000", "isSensitivePrivacy" : false, "sensitivePrivacyDate" : "2023-02-15T19:24:56.1070000", "lastCandidateActivity" : "2023-02-15T19:24:56.1070000", "positionInfoEsLst" : [ { "positionID" : "6c10fa0c-8d9c-49a0-96e7-1c9b2e2b42d6", "positionName" : "test1", "positionRoleNo" : "", "employerID" : "63717ce2-c93a-4686-828e-8499324a99d9", "employerName" : "General", "sourceName" : "", "employeeName" : "", "jobStartDate" : "2023-02-15T00:00:00", "jobEndDate" : "2023-02-16T00:00:00", "candidateFlowStatusTypeID" : 1, "candidateFlowStatusID" : "09c75489-5334-4af3-ad80-799393ef3139", "lastCandidateFlowStatusDate" : "2023-02-15T00:00:00" }, { "positionID" : "04b698d0-afbf-4c09-a21a-d02ec8efaa70", "positionName" : "new case", "positionRoleNo" : "", "employerID" : "63717ce2-c93a-4686-828e-8499324a99d9", "employerName" : "General", "sourceName" : "Indeed", "employeeName" : "", "jobStartDate" : "2023-03-01T06:09:12.9430000", "jobEndDate" : "2023-03-02T06:09:12.9470000", "candidateFlowStatusTypeID" : 3, "candidateFlowStatusID" : "c4f2d4b5-17bd-41a0-a171-74b7b15fbb3f", "lastCandidateFlowStatusDate" : "2023-03-02T00:00:00" } ], "customFields" : [ ] } } ] } }
排查原因及解决方案
核心问题1:嵌套路径名称不匹配
查询中指定的nested path是positions,但实际文档中存储position数组的字段是positionInfoEsLst,路径完全不匹配,导致查询无法定位到正确的字段。
核心问题2:映射类型是否为nested
即使路径正确,还需要确认positionInfoEsLst在索引映射中是否被定义为nested类型:
- 如果是普通object数组,使用nested查询会失效,需用普通bool查询;
- 如果是nested类型,则必须用nested查询并指定正确的path。
正确的查询示例
场景1:positionInfoEsLst是nested类型
GET /My_index_here/_search { "query": { "bool": { "must": [ { "nested": { "path": "positionInfoEsLst", "query": { "bool": { "must": [ { "term": { "positionInfoEsLst.positionID": "04b698d0-afbf-4c09-a21a-d02ec8efaa70" } } ] } } } } ] } } }
注:positionID是精确匹配,用term查询比wildcard更高效,也避免wildcard可能带来的匹配问题。
场景2:positionInfoEsLst是普通object类型
GET /My_index_here/_search { "query": { "bool": { "must": [ { "term": { "positionInfoEsLst.positionID": "04b698d0-afbf-4c09-a21a-d02ec8efaa70" } } ] } } }
验证映射类型
可通过以下命令查看索引映射,确认positionInfoEsLst的类型:
GET /My_index_here/_mapping
内容的提问来源于stack exchange,提问作者Code Noob
相关产品推荐
相关产品推荐

