当前位置: 首页 > 面试题库 >

Elasticsearch分层排序

乜心思
2023-03-14
问题内容

我希望能够以一定顺序返回预输入项。例如,搜索Para应返回:

 Paracetamol

 Parafin

 LIQUID PARAFFIN

 ISOMETHEPTENE WITH PARACETAMOL

1)以搜索词para开头的建议应在顶部并按字母顺序排列

2)其余项目应按字母顺序显示在下方

Elasticsearch有可能吗?

更新资料

如果我希望输出像这样:

 Paracetamol

 Parafin

 Amber Paraffin

 ISOMETHEPTENE WITH PARACETAMOL

 LIQUID PARAFFIN

因此,所有包含前缀的术语都位于顶部,其他所有术语均按字母顺序排列。


问题答案:

这是我的建议(同样,您需要启用脚本):

PUT /test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "standard",
            "lowercase",
            "ngram"
          ]
        },
        "search_ngram": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      },
      "filter": {
        "ngram": {
          "type": "ngram",
          "min_gram": 2,
          "max_gram": 15
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "text": {
          "type": "string",
          "index_analyzer": "autocomplete",
          "search_analyzer": "search_ngram",
          "index_options": "positions",
          "fields": {
            "not_analyzed_sorting": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}
POST test/test/_bulk
{"index":{"_id":1}}
{"text":"Paracetamol"}
{"index":{"_id":2}}
{"text":"Paracetamol xxx yyy zzz"}
{"index":{"_id":3}}
{"text":"Parafin"}
{"index":{"_id":4}}
{"text":"LIQUID PARAFFIN"}
{"index":{"_id":5}}
{"text":"ISOMETHEPTENE WITH PARACETAMOL"}
GET /test/test/_search
{
  "query": {
    "match": {
      "text": "Para"
    }
  },
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": "termInfo=_index[field_to_search].get(term_to_search.toLowerCase(),_POSITIONS);if (termInfo) {for(pos in termInfo){return pos.position}};return 0;",
        "params": {
          "term_to_search": "Para",
          "field_to_search": "text"
        },
        "order": "asc"
      }
    },
    {
      "text.not_analyzed_sorting": {
        "order": "asc"
      }
    }
  ]
}

更新

对于您的更新问题, 即使我希望再发表一则文章 ,也可以使用以下查询:

{
  "query": {
    "match": {
      "text": "Para"
    }
  },
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": "termInfo=_index[field_to_search].get(term_to_search.toLowerCase(),_POSITIONS);if (termInfo) {for(pos in termInfo){if (pos.position==0) return pos.position; else return java.lang.Integer.MAX_VALUE}};return java.lang.Integer.MAX_VALUE;",
        "params": {
          "term_to_search": "Para",
          "field_to_search": "text"
        },
        "order": "asc"
      }
    },
    {
      "text.not_analyzed_sorting": {
        "order": "asc"
      }
    }
  ]
}


 类似资料:
  • 我有一个字段(比如color),它可以有四个值(红色、蓝色、黄色和绿色)之一。我想对它们进行排序,这样红色将有一个1的分数,所以所有的红色将在顶部,其次是蓝色,分数为2,黄色,分数为3等等。 如何为ElasticSearch中的排序分配这些手动评分?

  • 我正在尝试使用Elasticsearch(2.4)聚合对使用该查询的多个索引按“productId”分组 1) 我想按分数排序,所以我尝试使用 哪个返回 2) 此外,我正在尝试使用分页,“size”键实际起作用,但“from”键不起作用 **更新-聚合结果示例** 希望有人能帮忙

  • 我的索引中有以下类型的文档,但由于深度嵌套方面,我找不到正确排序的方法。 文档示例: 我希望排序或提升在匹配时间,以便我可以得到排序的文档(asc/desc)与约束和内嵌套文档和内嵌套文档

  • 在elasticsearch中,我可以在第二个聚合的数字字段上聚合和排序聚合。 例如。 但是,我想根据分类字段值对聚合进行排序。也就是说,字段2的值是(“a”、“b”、“c”)中的一个值——我想首先按所有文档对a1进行排序,字段2为:“a”,然后字段2为“b”,最后字段2为“c”。 在我的例子中,每个字段1都有一个唯一的字段2。所以我真的只想找到一种方法,按字段2对a1结果进行排序。

  • 问题内容: 我正在尝试在Elasticsearch中进行嵌套排序,但到目前为止没有成功。 我的数据结构: 我想根据文档中第一作者的姓氏对文档进行排序。 使用的映射: 使用SearchRequestBuilder(JAVA)进行排序: 这行得通,但没有给出想要的结果(例如,首先是“叫卖”,然后是“罗杰”)。 我错过了什么吗?有没有办法表明Elasticsearch访问数组authorList的ind

  • 问题内容: 我有ElasticSearch 5,我想根据字段值进行排序。想象一下,具有类别(例如流派)的文档可能具有科幻,戏剧,喜剧等值,并且在进行搜索时,我想对值进行排序,以便首先出现喜剧,然后是科幻和戏剧。然后,我当然会按照其他条件在小组内订购。有人可以指出我该怎么做吗? 问题答案: 使用手动排序进行Elasticsearch排序 在可以根据字段的特定值分配顺序的情况下,这是可能的。 我已经使