怎么又是你

带过滤条件的排序.setNestedFilter()无效

Elasticsearch | 作者 wokeyi | 发布于2019年04月04日 | 阅读数:2792

builder.withSort(new FieldSortBuilder("goodsLabel.sortSeq").order(SortOrder.DESC)
.setNestedFilter(QueryBuilders.termQuery("goodsLabel.dictId", labelId))); //排序内嵌过滤

字段的索引为:
多字段属性:
# 商品标签 1->6个标签 (eff_flag为1)
"goodsLabel":{
"properties":{
"dictId":{ # 字典表id
"type":"long",
"index":"not_analyzed",
"store":"true"
},
"sortSeq":{ #排序序号
"type": "long",
"store": "true",
"index": "not_analyzed",
"fielddata": {
"loading": "lazy"
}
}
}
}

对应的dsl语句是:
{
"from" : 0,
"size" : 50,
"query" : {
"bool" : {
"must" : [ {
"multi_match" : {
"query" : "毛衣",
"fields" : [ "goodsCode", " goodsName", " partCode", " partName" ]
}
}, {
"term" : {
"companyId" : 239
}
}, {
"term" : {
"buscontsId" : 1004002
}
} ]
}
},
"sort" : [ {
"goodsLabel.sortSeq" : {
"nested_filter" : {
"term" : {
"goodsLabel.dictId" : "10080104"
}
},
"order" : "asc"
}
}, {
"_score" : { }
} ]
}
想要对goodsLabel.dictId 为10080104的商品进行排序;
已邀请:

wokeyi

赞同来自:

排序的结果无效,通过观察,无论设置了哪个内嵌对排序都没有实质影响:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 7,
"max_score": null,
"hits": [
{
"_index": "my_goods-4",
"_type": "my_goods_info",
"_id": "1",
"_score": 0.6086315,
"_source": {
"id": "1",
"sellFlag": 1,
"ptiPartHdId": 20190328002,
"goodsCode": "goods004",
"goodsName": "商品004",
"buscontsId": 1004002,
"showWayId": "123456",
"ordQty": 10,
"partCode": "part003",
"partName": "基础商品003",
"pictFileId": 100100,
"tagPrice": 399.9,
"salePrice": 299.9,
"orderFlag": 0,
"companyId": 239,
"brandId": 2001,
"serialId": 2002,
"styleId": 2003,
"typeversionId": 2004,
"storyId": 2005,
"themeId": 2006,
"sceneId": 2007,
"quarterId": 2008,
"year": 2019,
"goodsLabel": [
{
"dictId": 10080101,
"sortSeq": 4
},
{
"dictId": 10080102,
"sortSeq": 3
},
{
"dictId": 10080103,
"sortSeq": 2
},
{
"dictId": 10080104,
"sortSeq": 1
}
]
},
"highlight": {
"goodsCode": [
"goods<em>00</em>4"
]
},
"sort": [
1,
0.6086315
]
},
{
"_index": "my_goods-4",
"_type": "my_goods_info",
"_id": "2",
"_score": 0.6086315,
"_source": {
"id": "2",
"sellFlag": 1,
"ptiPartHdId": 20190328002,
"goodsCode": "goods002",
"goodsName": "商品002",
"buscontsId": 1004002,
"showWayId": "123456",
"ordQty": 10,
"partCode": "part003",
"partName": "基础商品003",
"pictFileId": 100100,
"tagPrice": 399.9,
"salePrice": 299.9,
"orderFlag": 0,
"companyId": 239,
"brandId": 2001,
"serialId": 2002,
"styleId": 2003,
"typeversionId": 2004,
"storyId": 2005,
"themeId": 2006,
"sceneId": 2007,
"quarterId": 2008,
"year": 2019,
"goodsLabel": [
{
"dictId": 10080101,
"sortSeq": 3
},
{
"dictId": 10080102,
"sortSeq": 2
},
{
"dictId": 10080103,
"sortSeq": 1
},
{
"dictId": 10080104,
"sortSeq": 4
}
]
},
"highlight": {
"goodsCode": [
"goods<em>00</em>2"
]
},
"sort": [
1,
0.6086315
]
},
{
"_index": "my_goods-4",
"_type": "my_goods_info",
"_id": "4",
"_score": 0.6086315,
"_source": {
"id": "4",
"sellFlag": 1,
"ptiPartHdId": 20190328002,
"goodsCode": "goods003",
"goodsName": "商品003",
"buscontsId": 1004002,
"showWayId": "123456",
"ordQty": 10,
"partCode": "part003",
"partName": "基础商品003",
"pictFileId": 100100,
"tagPrice": 299.9,
"salePrice": 199.9,
"orderFlag": 0,
"companyId": 239,
"brandId": 2001,
"serialId": 2002,
"styleId": 2003,
"typeversionId": 2004,
"storyId": 2005,
"themeId": 2006,
"sceneId": 2007,
"quarterId": 2008,
"year": 2019,
"goodsLabel": [
{
"dictId": 10080101,
"sortSeq": 1
},
{
"dictId": 10080102,
"sortSeq": 4
},
{
"dictId": 10080103,
"sortSeq": 8
},
{
"dictId": 10080104,
"sortSeq": 1
}
]
},
"highlight": {
"goodsCode": [
"goods<em>00</em>3"
]
},
"sort": [
1,
0.6086315
]
}
]
}
}

wokeyi

赞同来自:

搞定了!
首先,索引的字段需要指定类型为nested:
"goodsLabel":{
"type":"nested", #必须指定nested嵌套类型
"properties":{
"dictId":{ # 字典表id
"type":"long",
"index":"not_analyzed",
"store":"true"
},
"sortSeq":{ #排序序号
"type": "long",
"store": "true",
"index": "not_analyzed",
"fielddata": {
"loading": "lazy"
}
}
}
}

搜索时需要指定嵌套的路径:
{
"from" : 0,
"size" : 10,
"sort" : [ {
"goodsLabel.sortSeq" : {
[b]"nested_path": "goodsLabel"[/b], #指定嵌套的路径,否则找不到对应的字段
"order" : "desc", #指定排序的顺序
"nested_filter" : {
"filter" : {
"goodsLabel.dictId" : 10080101 #标签为10080101,未设置该标签的,默认排到最后
}
}
}
}, {
"_score" : { } #设置先根据指定的字段进行排序,序号一样的,根据得分进行排序
} ],
"_source": [
"ptiPartHdId",
"goodsCode",
"shopPrice"
]
}
对应的java代码:
builder.withSort(new FieldSortBuilder("goodsLabel.sortSeq").order(sortOrder)
[b].setNestedPath("goodsLabel")[/b] //指定字段名称,否则排序无效
.setNestedFilter(QueryBuilders.termQuery("goodsLabel.dictId", labelId)));
builder.withSort(SortBuilders.scoreSort().order(SortOrder.DESC)); //根据得分降序排序

要回复问题请先登录注册