简体   繁体   English

弹性搜索排序

[英]Elastic Search Sort

I have a table for some activities like 我有一些活动的桌子,例如

[
{
  "id": 123,
  "name": "Ram",
  "status": 1,
  "activity": "Poster Design"
},
{
  "id": 123,
  "name": "Ram",
  "status": 1,
  "activity": "Poster Design"
},
{
  "id": 124,
  "name": "Leo",
  "categories": [
    "A",
    "B",
    "C"
  ],
  "status": 1,
  "activity": "Brochure"
},
{
  "id": 134,
  "name": "Levin",
  "categories": [
    "A",
    "B",
    "C"
  ],
  "status": 1,
  "activity": "3D Printing"
}

] ]

I want to get this data from elastic search 5.5 by sorting on field activity, but I need all the data corresponding to name = "Ram" first and then remaining in a single query. 我想通过对字段活动进行排序从弹性搜索5.5中获取此数据,但是我需要首先与name =“ Ram”相对应的所有数据,然后保留在单个查询中。

You can use function score query to boost the result based on match for the filter(this case ram in name). 您可以使用函数得分查询基于过滤器(本例中为ram)的匹配来提高结果。

Following query should work for you 以下查询应为您工作

POST sort_index/_search
{
    "query": {
        "function_score": {
            "query": {
                "match_all": {}
            },
            "boost": "5",
            "functions": [{
                "filter": {
                    "match": {
                        "name": "ram"
                    }
                },
                "random_score": {},
                "weight": 1000
            }],
            "score_mode": "max"
        }
    },
    "sort": [{
        "activity.keyword": {
            "order": "desc"
        }
    }]
}

I would suggest using a bool query combined with the should clause. 我建议将布尔查询与should子句结合使用。 U will also need to use the sort clause on your field. U还需要在您的字段上使用sort子句。

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

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