1.需要用到dense_vector + script_score来进行算分 --- 排序
2.ES版本为7.5.2,用到了自定义的插件,短期无法升级到7.17.5(或更高版本)
3.script_score查询在加入date类型的字段作为查询条件时,出现NPE异常
1. 索引mapping
PUT zmc-test
{
"aliases": {},
"mappings": {
"dynamic": "false",
"properties": {
"dataChange_LastTime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"features": {
"type": "dense_vector",
"dims": 128
}
}
}
}
2. 查询语句
GET zmc-test/_search
{
"size": 50,
"query": {
"script_score": {
"query": {
"bool": {
"must": [{
"range": {
"dataChange_LastTime": {
"from": "2022-01-02 22:57:02",
"to": "2022-12-31 18:05:20",
"include_lower": true,
"include_upper": true,
"boost": 1.0
}
}
}]
}
},
"script": {
"source": "cosineSimilarity(params.queryVector, doc['features'])+1.0",
"lang": "painless",
"params": {
"queryVector": [-3.3732865, -0.088037536, 0.000...., -0.17549877]
}
},
"boost": 1.0
}
},
"_source": {
"includes": ["xxx", "xxx"],
"excludes": []
}
}
3.异常信息
{
"error": {
"root_cause": [
{
"type": "null_pointer_exception",
"reason": null
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "zmc-test",
"node": "tc3gB-p-TNOIbLJkaNk8uw",
"reason": {
"type": "null_pointer_exception",
"reason": null
}
}
],
"caused_by": {
"type": "null_pointer_exception",
"reason": null,
"caused_by": {
"type": "null_pointer_exception",
"reason": null
}
}
},
"status": 500
}
4. 猜测是date类型format的问题,换成默认date类型依然报错
`strict_date_optional_time||epoch_millis`
如:
{
"date": "2022-01-01T12:00:00.000+0800",
"timestamp": 1641004800000
}
5.测试其他类型(非date,如long、int等类型range查询均正常),测试其他ES版本,7.17.5版本date类型也正常
1. 换成long类型,数据存timestamp
2. 升级版本到7.17.5或或更高