Elasticsearch 分层排序 [英] Elasticsearch layered ordering

查看:21
本文介绍了Elasticsearch 分层排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够按特定顺序返回预先输入的项目.例如,搜索 Para 应该返回:

I would like to be able to return typeahead items in a certain order. For example, search for Para should return:

 Paracetamol

 Parafin

 LIQUID PARAFFIN

 ISOMETHEPTENE WITH PARACETAMOL

1) 以搜索词 para 开头的建议应排在最上面,并按字母顺序排列

1) The suggestions that begin with the search term para should be ordered at the top and in alphabetical order

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

2) The rest of the items should appear below and also in alphabetical order

这可以通过 Elasticsearch 实现吗?

Is this possible with Elasticsearch?

如果我想要这样的输出怎么办:

What if I wanted the output to be like this:

 Paracetamol

 Parafin

 Amber Paraffin

 ISOMETHEPTENE WITH PARACETAMOL

 LIQUID PARAFFIN

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

So all the terms that contain the prefix are at the top and everything else in alphabetical order.

推荐答案

这是我的建议(另外,你需要启用脚本):

This is my suggestion (also, you need to enable scripting):

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"
      }
    }
  ]
}

更新

对于您更新的问题,即使我更希望有另一个帖子,请使用以下查询:

For your updated question, even if I would have preferred to have another post, use the following query:

{
  "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"
      }
    }
  ]
}

这篇关于Elasticsearch 分层排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆