es数据字段,关注_source内容,为自定义的es表字段内容
{"clerk_id": 3150036230,"clerk_follow_status": 60,"create_time": 1729156110000,"channel": 1,"mid": 1538020071,"binlog_timestamp": null,"clue_tag": 0,"is_sync": 1,"vuid": 199603942960332807,"idempotence": "8101929dc442442b847a46fa09fa1c11","sync_operation_time": 1733448887759,"follow_time": 1729230615000,"update_time": 1729230615000,"follow_count": 1,"id": 12428,"spu_id": 8797,"org_code": "MI0101","wecom_friend": [3150036230],"latest_purchase_time": 1656489614
}
要实现的排序逻辑是:如果wecom_friend字段包含指定id则优先排序
第一步,百度参考:Elasticsearch-script 脚本排序-CSDN博客
当前参考是字段==比较,那我们的是数组,则直接使用contains方法尝试
{"_script": {"script": {"source": "if(params['_source']['wecom_friend'].contains('3150036230')){1}else{0}","lang": "painless"},"type": "number","order": "desc"}
}
执行可以看到,并没有匹配到
第二步,当前字段为long类型,所以是不是要去掉引号
执行报错
提示错误,需要改成long类型数字去匹配
第三步,修改后,执行成功
最终查询代码
{"_script": {"script": {"source": "if(params['_source']['wecom_friend'].contains(3150036230L)){1}else{0}","lang": "painless"},"type": "number","order": "desc"}
}
Java代码实现