elasticsearch Elasticsearch如何按列表长度排序
我正在尝试按列表的长度对搜索进行排序。我有一个定义为的字段elasticsearch Elasticsearch如何按列表长度排序,elasticsearch,elasticsearch,我正在尝试按列表的长度对搜索进行排序。我有一个定义为的字段 "authors": { "type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100, "fields": { "
"authors": {
"type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100,
"fields": {
"raw" : { # This field is needed for your_paper suggestions search
"type" : "string",
"analyzer" : 'lower_keyword'
}
}
}
现在我想按这个字段排序,它可能看起来像['John Doe','Mike Smith']
根据elasticsearch文档和我找到的一些google搜索
doc['sort'] = {
"_script": {
"type": "number",
"script": "doc['authors'].length",
"order": "asc"
}
}
我还尝试了doc['authors'].values.length()和doc['authors'].values.size(),但它们都会导致
TransportError: TransportError(500, u'search_phase_execution_exception')
知道如何按列表长度排序吗?由于性能原因,您无法访问
doc['authors']
例如,您可以在内部字段中设置fielddata=trueraw
:
"authors": {
"type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100,
"fields": {
"raw" : { # This field is needed for your_paper suggestions search
"type" : "string",
"analyzer" : 'lower_keyword',
"fielddata": True
}
}
}
并按作者进行排序。改为按原始作者进行排序:
doc['sort'] = {
"_script": {
"type": "number",
"script": "doc['authors.raw'].length",
"order": "asc"
}
}
另一个解决方案是创建一个新的内部字段,类型为
keyword
,并按其排序。但我建议使用raw
,因为analyzer看起来只是降低了大小写。由于性能原因,您无法访问doc['authors']
例如,您可以在内部字段中设置fielddata=trueraw
:
"authors": {
"type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100,
"fields": {
"raw" : { # This field is needed for your_paper suggestions search
"type" : "string",
"analyzer" : 'lower_keyword',
"fielddata": True
}
}
}
并按作者进行排序。改为按原始作者进行排序:
doc['sort'] = {
"_script": {
"type": "number",
"script": "doc['authors.raw'].length",
"order": "asc"
}
}
另一个解决方案是创建一个新的内部字段,类型为keyword
,并按其排序。但是我建议使用raw
,因为看起来analyzer只是降低了大小写