弹性搜索分层排序 [英] Elasticsearch layered ordering

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

问题描述

我希望能够以特定顺序返回头像。例如,搜索 Para 应该返回:

 对乙酰氨基酚

Parafin

LIQUID PARAFFIN

ISOMETHEPTENE WITH PARACETAMOL

1)从搜索字词para开始的建议应按顶部和字母顺序排序



2)其余的项目应



更新



如果我希望输出如下:

 对乙酰氨基酚

Parafin

琥珀石蜡

ISOMETHEPTENE WITH PARACETAMOL

LIQUID PARAFFIN

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

解决方案

这是我的建议(也需要启用脚本):

  PUT / test 
{
settings:{
analysis:{
analyzer:{
autocomplete:{
type:custom,
tokenizer:standard,
filter:[
standard,
smallcase,
ngram

},
search_ngram:{
type:custom,
tokenizer:keyword,
filter:smallcase
}
},
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:位置,
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}};回报0;,
params:{
term_to_search:Para,
field_to_search:text
},
order asc
}
},
{
text.not_analyzed_sorting:{
order:asc
}

]
}

更新 p>

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

  {
查询:{
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
}
}
]
}


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) The suggestions that begin with the search term para should be ordered at the top and in alphabetical order

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

Is this possible with Elasticsearch?

Update

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

UPDATE

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

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

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