简体   繁体   English

弹性搜索的自定义排序

[英]Custom ordering on elastic search

I'm executing a simple query which returns items matched by companyId.我正在执行一个简单的查询,它返回与 companyId 匹配的项目。

In addition to only showing clients matching a specific company I also want records matching a certain location to appear at the top.So if somehow I pass through pseudo sort:"location=Johannesburg" it would return the data below and items which match the specific location would appear on top, followed by items with other locations.除了仅显示与特定公司匹配的客户外,我还希望与特定位置匹配的记录出现在顶部。因此,如果我以某种方式通过排序:“location=Johannesburg”,它将返回下面的数据和与特定匹配的项目位置将出现在顶部,然后是具有其他位置的项目。

Data:数据:

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

Query:询问:

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

Is something like this possible in elastic and if so what is the name of this concept?(I'm not sure what to even Google for to solve this problem)这样的事情在弹性中是否可能,如果是的话,这个概念的名称是什么?(我什至不确定谷歌如何解决这个问题)

It can be done in different ways.它可以通过不同的方式完成。 Simplest (if go only with text matching) is use bool query with should statement.最简单的(如果 go 仅与文本匹配)是使用带有should语句的bool查询。

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. bool 查询采用匹配越多越好的方法,因此每个匹配的 must 或 should 子句的分数将被加在一起以提供每个文档的最终 _score。 Doc 文档

Example:例子:

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

More complex solution is to store GEO points in location, and use Distance feature query as example.更复杂的解决方案是将GEO点存储在位置中,并以距离特征查询为例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM