gpt4 book ai didi

python - Elasticsearch - match_phrase 查询按 int 排序

转载 作者:行者123 更新时间:2023-12-03 02:02:02 27 4
gpt4 key购买 nike

我有“设备”类型的文档,我使用以下查询按模型搜索(使用 Flask 和 Elasticsearch 作为 api):

match handset
query = {
"query": {
"match_phrase": {
"model": model_name
}
},
"track_scores": True,
"size": 1,
"sort":
[
{"_score": {"order": "desc"}},
{"model": {"order": "asc"}}
]
}
device = es.search(body=query, doc_type='device')

这将返回“模型”最接近请求(模型名称)的单个设备。

设备列表示例:
[{ "id":482,
"memory":"16",
"model":"iPhone 5s 16GB" },
{ "id":483,
"memory":"32",
"model":"iPhone 5s 32GB" },
{ "id":484,
"memory":"16",
"model":"iPhone 5c 16GB" },
{ "id":486,
"memory":"64",
"model":"iPhone 6 64GB" },
{ "id":485,
"memory":"32",
"model":"iPhone 6 32GB" }]

如何更改它以使其返回内存最低的设备?
>>> query.query.match_phrase.model = 'iPhone 5s'
>>> device = es.search(body=query, doc_type='device')
{ "id":482,
"memory":"16",
"model":"iPhone 5s 16GB" }


>>> query.query.match_phrase.model = 'iPhone 6'
>>> device = es.search(body=query, doc_type='device')
{ "id":485,
"memory":"32",
"model":"iPhone 6 32GB" }

任何线索高度赞赏。

最佳答案

我会更改 "memory" 的类型字段到 "integer"在你的映射中,并适本地索引数据,那么很容易得到你想要的结果。

因此,使用这样的映射:

PUT /test_index
{
"mappings": {
"doc": {
"_id": {
"path": "id"
},
"properties": {
"id": {
"type": "integer"
},
"memory": {
"type": "integer"
},
"model": {
"type": "string"
}
}
}
}
}

这些文件被索引:
POST /test_index/doc/_bulk
{"index":{}}
{"id":482,"memory":16,"model":"iPhone 5s 16GB"}
{"index":{}}
{"id":483,"memory":32,"model":"iPhone 5s 32GB"}
{"index":{"_id":1}}
{"id":484,"memory":16,"model":"iPhone 5c 16GB"}
{"index":{}}
{"id":486,"memory":64,"model":"iPhone 6 64GB"}
{"index":{}}
{"id":485,"memory":32,"model":"iPhone 6 32GB"}
{"index":{}}

您可以像这样查询以获得最低的内存命中 "iPhone 5s" :
POST /test_index/_search
{
"query": {
"match": {
"model": {
"query": "iPhone 5s",
"operator": "and"
}
}
},
"sort": [
{
"memory": {
"order": "asc"
}
}
],
"size": 1
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "482",
"_score": null,
"_source": {
"id": 482,
"memory": 16,
"model": "iPhone 5s 16GB"
},
"sort": [
16
]
}
]
}
}

这是我使用的代码:

http://sense.qbox.io/gist/8441d7379485e03a75fdbaa9ae0bf9748098be33

关于python - Elasticsearch - match_phrase 查询按 int 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413849/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com