2014-01-20 63 views
0

似乎,我有排序结果的问题。下面是我的配置:ElasticSearch - 排序不起作用

curl -X PUT localhost:9200/companies -d ' 
{ 
    "settings" : { 
    "analysis" : { 
     "analyzer" : { 
      "my_edge_ngram_analyzer" : { 
       "tokenizer" : "my_edge_ngram_tokenizer" 
      } 
      }, 
     "tokenizer" : { 
      "my_edge_ngram_tokenizer" : { 
       "type" : "edgeNGram", 
       "min_gram" : "1", 
        "max_gram" : "5", 
        "token_chars": [ "letter", "digit" ] 
      } 
     } 
    } 
    }, 
    "mappings": { 
    "company" : { 
     "properties" : { 
     "name" : { "type" : "string" }, 
     "count" : {"type" : "long" }, 
     "name_suggest" : { 
      "type" :  "completion", 
      "index_analyzer": "my_edge_ngram_analyzer", 
      "search_analyzer": "my_edge_ngram_analyzer" 
     } 
     } 
    } 
    } 
}' 

下面举例来投入ES:

curl -X PUT localhost:9200/companies/company/1 -d ' 
{ 
    "name" :   "1800flowers", 
    "count": 1000, 
    "name_suggest" : { 
    "input" :  [ 
     "1800 flowers.Com, Inc", 
     "1800 Flowers","1800-Flowers.com", 
     "1 800 Flowers", 
     "www.1800flowers.com", 
     "1800Flowers.com", 
     "Inc,1-800-FLOWERS.COM", 
     "1-800-FLOWERS.COM, INC", 
     "1800Flowers", 
     "1800Flowers Inc", 
     "1800Flowers.com (Consultant)", 
     "1-800-FLOWERS","1800Flowers.com", 
     "1800FLOWERS INTERNATIONAL", 
     "1-800 Flowers", 
     "1-800 FLOWERS.COM, INC", 
     "1-800-FLOWERS, Inc", 
     "1800 flowers.com", 
     "1-800Flowers.com", 
     "1-800 flowers.com", 
     "1800 Flowers Inc" 
    ], 
    "output" : "1800 Flowers" 
    } 
}' 

curl -X PUT localhost:9200/companies/company/2 -d ' 
{ 
    "name" :   "1800 Ruby", 
    "count": 10000, 
    "name_suggest" : { 
    "input" :  [ 
    "1800" 
    ], 
    "output" : "1800 Ruby" 
    } 
}' 

现在清楚,如果我在1800做一个文本搜索,我应该得到两个对象回来,给人输出“ 1800朵花“,”1800红宝石“。

现在真的,我想这些结果按count,降序排序,所以我应该有:“1800红宝石”,“1800花”,但这是行不通的!

curl -X POST localhost:9200/companies/_suggest -d ' 
{ 
    "query":{ 
    "companies" : { 
    "text" : "18", 
    "completion" : { 
     "field" : "name_suggest" 
    } 
    } 
    }, 
    "sort": [ {"count": {"order": "desc"} } ] 
}' 

回答

0

您必须在文档的"name_suggest"字段中添加权重。另外,您不能在_suggest请求中使用"query"语法。

因此,使用相同的设置和映射你有以上情况,我可以更新与建议者的权重等于"count"的文档,内容如下:

curl -XPUT "http://localhost:9200/companies/company/1" -d' 
{ 
    "name" :   "1800flowers", 
    "count": 1000, 
    "name_suggest" : { 
    "input" :  [ 
     "1800 flowers.Com, Inc", 
     "1800 Flowers","1800-Flowers.com", 
     "1 800 Flowers", 
     "www.1800flowers.com", 
     "1800Flowers.com", 
     "Inc,1-800-FLOWERS.COM", 
     "1-800-FLOWERS.COM, INC", 
     "1800Flowers", 
     "1800Flowers Inc", 
     "1800Flowers.com (Consultant)", 
     "1-800-FLOWERS","1800Flowers.com", 
     "1800FLOWERS INTERNATIONAL", 
     "1-800 Flowers", 
     "1-800 FLOWERS.COM, INC", 
     "1-800-FLOWERS, Inc", 
     "1800 flowers.com", 
     "1-800Flowers.com", 
     "1-800 flowers.com", 
     "1800 Flowers Inc" 
    ], 
    "output" : "1800 Flowers", 
    "weight" : 1000 
    } 
}' 

curl -XPUT "http://localhost:9200/companies/company/2" -d' 
{ 
    "name" :   "1800 Ruby", 
    "count": 10000, 
    "name_suggest" : { 
    "input" :  [ 
    "1800" 
    ], 
    "output" : "1800 Ruby", 
    "weight" : 10000 
    } 
}' 

然后正常的建议请求

curl -XPOST "http://localhost:9200/companies/_suggest" -d' 
{ 
    "companies": { 
     "text": "18", 
     "completion": { 
      "field": "name_suggest" 
     } 
     } 
}' 

产生预期的结果

{ 
    "_shards": { 
     "total": 1, 
     "successful": 1, 
     "failed": 0 
    }, 
    "companies": [ 
     { 
     "text": "18", 
     "offset": 0, 
     "length": 2, 
     "options": [ 
      { 
       "text": "1800 Ruby", 
       "score": 10000 
      }, 
      { 
       "text": "1800 Flowers", 
       "score": 1000 
      } 
     ] 
     } 
    ] 
} 

这里一个完整的可运行示例: http://sense.qbox.io/gist/21cac07480e4077e083b037c5ac00016b04503f2

+0

似乎工作正常。你知道一种简单引用count属性的方法,而不是写两次吗? – redrubia

+0

不幸的是,我无法找到一个。据我所知,完成的一个限制条件表明了这一点。 –