2016-02-29 70 views
0

我elasticsearch 2.2Elasticsearch查询特定的顺序

我有些文件索引这样的工作排序由现场:

{  
    "id": 2116, 
    "brand": "The Brand 1", 
    "reference": "OI444", 
    "supplier": "My supplier 1" 
}, 
{  
    "id": 2118, 
    "brand": "The Brand", 
    "reference": "OI44488", 
    "supplier": "My supplier" 
} 

我怎么可以要求该指数以获得通过ID列表排序的查询,这个查询工作,但以其他顺序返回结果。

{ 
    "query": { 
    "bool": { 
     "filter": { 
     "ids": { 
      "values": [ 
      2118, 2116 
      ] 
     } 
     } 
    } 
    } 
} 

就我而言,我的ID列表可以是随机像216105208,我需要这个命令在结果集中得到尊重

回答

1
  • 现场您使用应映射来没有分析
  • 添加排序

查询

POST c1_2/Test/_search/ 
{ 
    "sort": [ 
    { 
     "_script": { 
     "script": "doc['id'] != null ? sortOrder.indexOf(doc['id'].value.toInteger()) : 0", 
     "type": "number", 
     "params": { 
      "sortOrder": [ 
      2116, 
      211 
      ] 
     }, 
     "order": "desc" 
     } 
    }, 
    "_score" 
    ], 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "terms": { 
      "brand": [ 
       "The" 
      ] 
      } 
     }, 
     { 
      "ids": { 
      "values": [ 
       2118, 
       2116, 
       211 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 
+0

谢谢,但在我的情况下,我的ids列表可以像216,105,208那样是随机的,我需要这个顺序受到尊重, – bourvill

+0

是的,但是这些ids在文档中,为什么它应该重要?你过滤在你的文档字段“id”:“2116”上的ID为 –

+0

,所以你告诉ES在这个字段上排序我 –