elasticsearch 子文档nested类型查询
1.json 数据结构如下
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 6, "successful" : 6, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 162, "relation" : "eq" }, "max_score" : 11.1383505, "hits" : [ { "_index" : "sku_goods", "_type" : "_doc", "_id" : "3a0530c9-0457-c6f3-5b23-004156cfa44c", "_score" : 11.1383505, "_source" : { "Id" : "3a0530c9-0457-c6f3-5b23-004156cfa44c", "PartNo" : "105J250CS6G-FA", "Brand" : "PAKTRON CAPACITORS", "BrandSource" : "PAKTRON CAPACITORS", "CategoryId" : "00000000-0000-0000-0000-000000000000", "Packing" : "管装", "Describe" : "CS6 系列 250 V 1 uF ±5% 表面贴装 金属化 聚酯 Capstick® 电容", "ECCN" : "EAR99", "IsBrandStandard" : false, "IsCategoryClassify" : false, "DataSourceType" : "1000", "CreateTime" : "2022-07-21 10:33:09", "CreateTimeStamp" : 1658370789, "CreateUserId" : "00000000-0000-0000-0000-000000000000", "CreateUserName" : "系统", "UpdateTime" : "2022-07-21 10:33:09", "UpdateTimeStamp" : 1658370789, "UpdateUserId" : "00000000-0000-0000-0000-000000000000", "UpdateUserName" : "系统", "SpecItems" : [ ], "ResourceItems" : [ { "ResourceUri" : "https://media.xxx.com/PASSIVES/CAPACITORS/FILM-CAPACITORS/CAPSTICK-CS4-CS6-FILMCAP-SMT-BCK-MED.JPG?m=k9Jv3Y", "ResourceType" : 1, "DataSourceType" : "1000", "SSOGuid" : "00000000-0000-0000-0000-000000000000", "SSOPathUri" : "", "DownloadTime" : "" }, { "ResourceUri" : "https://xxx.com/wp-content/uploads/2018/07/CS-Capstick.pdf", "ResourceType" : 3, "DataSourceType" : "1000", "SSOGuid" : "00000000-0000-0000-0000-000000000000", "SSOPathUri" : "", "DownloadTime" : "" }, { "ResourceUri" : "https://media.xxx.com/PASSIVES/CAPACITORS/FILM-CAPACITORS/CAPSTICK-CS4-CS6-FILMCAP-SMT-FNT-MED.JPG?m=k9Jxmj", "ResourceType" : 1, "DataSourceType" : "1000", "SSOGuid" : "00000000-0000-0000-0000-000000000000", "SSOPathUri" : "", "DownloadTime" : "" } ] } } ] } }
2.查询统计子文档SSOGuid的值为"00000000-0000-0000-0000-000000000000"的数量
get sku_goods/_count { "query":{ "nested": { "path": "ResourceItems", "query": { "term": { "ResourceItems.SSOGuid": { "value": "00000000-0000-0000-0000-000000000000" } } } } } }
3.查询统计子文档SSOGuid的值不为"00000000-0000-0000-0000-000000000000"的数量
get sku_goods/_count { "query":{ "nested": { "path": "ResourceItems", "query": { "bool": { "must_not": [ { "term": { "ResourceItems.SSOGuid": { "value": "00000000-0000-0000-0000-000000000000" } } } ] } } } } }
4. 多条件查询
get sku_goods/_search { "query": { "bool": { "must": [ { "nested": { "path": "SpecItems", "query": { "exists": { "field": "SpecItems.SpecId" } } } }, { "term": { "DataSourceType": { "value": "3026" } } } ] } }, "sort": [ { "CreateTimeStamp": { "order": "desc" } } ] }