ElasticSearch 按字符串长度排序

ElasticSearch Order By String Length

我正在通过 NEST c# 使用 ElasticSearch。我有大量关于人的信息

{
   firstName: 'Frank',
   lastName: 'Jones',
   City: 'New York'
}

我希望能够按姓氏过滤和排序此项目列表并按长度排序,这样姓名中只有 5 个字符的人将位于结果集的开头然后人有 10 个字符。

所以我想用一些伪代码做一些类似的事情 list.wildcard("j*").sort(m => lastName.length)

您可以使用 script-based sorting 进行排序。

作为玩具示例,我用几个文档设置了一个简单的索引:

PUT /test_index

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"Bob"}
{"index":{"_id":2}}
{"name":"Jeff"}
{"index":{"_id":3}}
{"name":"Darlene"}
{"index":{"_id":4}}
{"name":"Jose"}

然后我可以这样排序搜索结果:

POST /test_index/_search
{
   "query": {
      "match_all": {}
   },
   "sort": {
      "_script": {
         "script": "doc['name'].value.length()",
         "type": "number",
         "order": "asc"
      }
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": null,
            "_source": {
               "name": "Bob"
            },
            "sort": [
               3
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "4",
            "_score": null,
            "_source": {
               "name": "Jose"
            },
            "sort": [
               4
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": null,
            "_source": {
               "name": "Jeff"
            },
            "sort": [
               4
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": null,
            "_source": {
               "name": "Darlene"
            },
            "sort": [
               7
            ]
         }
      ]
   }
}

要按长度过滤,我可以用类似的方式使用 script filter

POST /test_index/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "script": {
               "script": "doc['name'].value.length() > 3",
               "params": {}
            }
         }
      }
   },
   "sort": {
      "_script": {
         "script": "doc['name'].value.length()",
         "type": "number",
         "order": "asc"
      }
   }
}
...
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "4",
            "_score": null,
            "_source": {
               "name": "Jose"
            },
            "sort": [
               4
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": null,
            "_source": {
               "name": "Jeff"
            },
            "sort": [
               4
            ]
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": null,
            "_source": {
               "name": "Darlene"
            },
            "sort": [
               7
            ]
         }
      ]
   }
}

这是我使用的代码:

http://sense.qbox.io/gist/22fef6dc5453eaaae3be5fb7609663cc77c43dab

P.S.: 如果任何姓氏包含空格,您可能需要在该字段上使用 "index": "not_analyzed"