Elasticsearch 按元素顺序取数组内容(qbit)
前言
正文
doc['author_id'] , 得到的数组内容不能保持原始顺序GET my_index/_search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"id_combine": [
"2031435113240",
"2031592776783"
]
}
}
]
}
},
"rescore": [
{
"window_size": 5000,
"query": {
"score_mode": "multiply",
"rescore_query": {
"function_score": {
"max_boost": 1000,
"score_mode": "multiply",
"boost_mode": "multiply",
"functions": [
{
"filter": {
"script": {
"script": {
"lang": "painless",
"source": """
def ids = doc['author_id'];
Debug.explain(ids);
""",
"params": {
"auid": "4465029247"
}
}
}
},
"weight": 10
}
]
}
},
"query_weight": 1,
"rescore_query_weight": 1
}
}
],
"track_total_hits": true,
"from": 0,
"size": 10,
"_source": [
"id",
"title",
"pub_year",
"author_id"
]
}params._source 取到的值为 nullGET my_index/_search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"id_combine": [
"2031435113240",
"2031592776783"
]
}
}
]
}
},
"rescore": [
{
"window_size": 5000,
"query": {
"score_mode": "multiply",
"rescore_query": {
"function_score": {
"max_boost": 1000,
"score_mode": "multiply",
"boost_mode": "multiply",
"functions": [
{
"filter": {
"script": {
"script": {
"lang": "painless",
"source": """
def ids = params._source;
Debug.explain(ids);
""",
"params": {
"auid": "4465029247"
}
}
}
},
"weight": 10
}
]
}
},
"query_weight": 1,
"rescore_query_weight": 1
}
}
],
"track_total_hits": true,
"from": 0,
"size": 10,
"_source": [
"id",
"title",
"pub_year",
"author_id"
]
}runtime_mappings 生成第一作者动态字段,达到预期效果GET my_index/_search
{
"runtime_mappings": {
"auid_1st": {
"type": "keyword",
"script": {
"source": """
def auid = params._source.author_id;
if (auid == null) return;
if (auid instanceof List && auid.size() > 0) emit(auid.get(0).toString());
else if (!(auid instanceof List)) emit(auid.toString());
"""
}
}
},
"query": {
"bool": {
"must": [
{
"terms": {
"id_combine": [
"2031435113240",
"2031592776783"
]
}
}
]
}
},
"rescore": [
{
"window_size": 5000,
"query": {
"score_mode": "multiply",
"rescore_query": {
"function_score": {
"max_boost": 1000,
"score_mode": "multiply",
"boost_mode": "multiply",
"functions": [
{
"filter": {
"term": {
"auid_1st": {
"value": "4465029247"
}
}
},
"weight": 10
}
]
}
},
"query_weight": 1,
"rescore_query_weight": 1
}
}
],
"track_total_hits": true,
"from": 0,
"size": 10,
"_source": [
"id",
"title",
"pub_year",
"author_id"
]
}本文出自 qbit snap