栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elasticsearch分层排序

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch分层排序

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

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393239.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号