首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹性搜索分层排序

弹性搜索分层排序
EN

Stack Overflow用户
提问于 2015-10-30 12:09:34
回答 1查看 280关注 0票数 3

我希望能按一定的顺序退货。例如,搜索Para应该返回:

代码语言:javascript
复制
 Paracetamol

 Parafin

 LIQUID PARAFFIN

 ISOMETHEPTENE WITH PARACETAMOL

1)以搜索词段开头的建议应按字母顺序排列

( 2)其余项目应按字母顺序排列如下

这和Elasticsearch有可能吗?

更新

如果我希望输出是这样的话:

代码语言:javascript
复制
 Paracetamol

 Parafin

 Amber Paraffin

 ISOMETHEPTENE WITH PARACETAMOL

 LIQUID PARAFFIN

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-30 16:18:14

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

代码语言:javascript
复制
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"
            }
          }
        }
      }
    }
  }
}
代码语言:javascript
复制
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"}
代码语言:javascript
复制
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"
      }
    }
  ]
}

更新

对于您更新的问题,即使我希望有另一篇文章,也可以使用以下查询:

代码语言:javascript
复制
{
  "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"
      }
    }
  ]
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33435670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档