首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Elasticsearch - match_phrase查询按int排序

Elasticsearch - match_phrase查询按int排序
EN

Stack Overflow用户
提问于 2015-07-14 17:52:17
回答 1查看 663关注 0票数 0

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

代码语言:javascript
复制
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')

返回与请求(model_name)最接近的“模型”的单个设备。

设备示例列表:

代码语言:javascript
复制
[{ "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" }]

如何更改它,使其返回内存最低的设备?

代码语言:javascript
复制
>>> 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" }

任何线索都很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-14 18:10:06

我会在映射中将"memory"字段的类型更改为"integer",并对数据进行适当的索引,这样就很容易得到所需的结果。

因此,通过这样的映射:

代码语言:javascript
复制
PUT /test_index
{
   "mappings": {
      "doc": {
         "_id": {
            "path": "id"
         },
         "properties": {
            "id": {
               "type": "integer"
            },
            "memory": {
               "type": "integer"
            },
            "model": {
               "type": "string"
            }
         }
      }
   }
}

这些文件索引如下:

代码语言:javascript
复制
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"上获得最低的内存命中。

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31413849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档