ElasticSearch - 排序不起作用

时间:2014-01-20 19:57:18

标签: sorting autocomplete 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 Flowers”,“1800 Ruby”。

现在真的,我希望这些结果按计数,降序排序,这样我就应该:“1800 Ruby”,“1800 Flowers”,但这不起作用!

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

1 个答案:

答案 0 :(得分:0)

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

因此,使用上面提到的相同设置和映射,我可以使用等于"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