弹性搜索的自定义排序

Custom ordering on elastic search

我正在执行一个简单的查询,其中 returns 项与 companyId 匹配。

除了仅显示与特定公司匹配的客户之外,如果我以某种方式通过 排序,我还希望匹配特定位置的记录出现在 top.So 中: “location=Johannesburg”它将 return 下面的数据和与特定位置匹配的项目将出现在顶部,然后是其他位置的项目。

数据:

{
  "clientId" : 1,
  "clientName" : "Name1",
  "companyId" : 8,
  "location" : "Cape Town"
},
{
  "clientId" : 2,
  "clientName" : "Name2",
  "companyId" : 8,
  "location" : "Johannesburg"
}

查询:

 {
      "query": {
        "match": {
          "companyId": "8"
        }
      },
      "size": 10,
      "_source": {
        "includes": [
          "firstName",
          "companyId",
          "location"
        ]
      }
    }

在 elastic 中是否可以实现类似的功能?如果可以,这个概念的名称是什么?(我什至不确定要 Google 来解决这个问题)

可以通过不同的方式完成。 最简单的(如果只进行文本匹配)是使用 bool 查询和 should 语句。

The bool query takes a more-matches-is-better approach, so the score from each matching must or should clause will be added together to provide the final _score for each document. Doc

示例:

{"query":
   "bool": {
     "must": [
        "match": {
          "companyId": "8"
        }
      ],
     "should": [
        "match": {
          "location": "Johannesburg"
         }
      ]
     }
   }
}

更复杂的解决方案是在位置中存储 GEO 点,并以 Distance feature query 为例。