{"id":6471,"date":"2024-08-07T15:01:01","date_gmt":"2024-08-07T07:01:01","guid":{"rendered":""},"modified":"2024-08-07T15:01:01","modified_gmt":"2024-08-07T07:01:01","slug":"elasticsearch shard\u6570\u91cf\u8bbe\u7f6e_reset\u68c0\u9a8c","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6471.html","title":{"rendered":"elasticsearch shard\u6570\u91cf\u8bbe\u7f6e_reset\u68c0\u9a8c"},"content":{"rendered":"

 <\/p>\n

\u76ee\u5f55<\/strong><\/p>\n

\u81ea\u5b9a\u4e49\u62fc\u97f3\u5206\u8bcd\u5668\uff0c\u6839\u636e\u62fc\u97f3\u5f97\u5230\u5b57\u6bb5\u641c\u7d22\u6587\u6863\u6570\u636e <\/p>\n

Demo\uff08\u57fa\u4e8e\u62fc\u97f3\u5b9e\u73b0\u641c\u7d22\uff09<\/p>\n

completion suggester\u67e5\u8be2\u5b9e\u73b0\u5b57\u6bb5\u8865\u5168<\/p>\n

\u6848\u4f8b\uff1a\u9152\u5e97\u6570\u636e\u81ea\u52a8\u8865\u5145<\/p>\n

 RestClient\u5b9e\u73b0\u8865\u5168<\/p>\n


\n
\n

\u81ea\u5b9a\u4e49\u62fc\u97f3\u5206\u8bcd\u5668\uff0c\u6839\u636e\u62fc\u97f3\u5f97\u5230\u5b57\u6bb5\u641c\u7d22\u6587\u6863\u6570\u636e <\/h4>\n<\/blockquote>\n

\"elasticsearch<\/p>\n

\u62fc\u97f3\u5206\u8bcd\u5668\u4e0d\u4f1a\u5206\u8bcd \uff0c\u800c\u4e14\u4e5f\u6ca1\u6709\u8865\u5145\u6c49\u5b57\uff0c\u9700\u8981\u81ea\u5b9a\u4e49\u5206\u8bcd\u5668<\/p>\n

\u6211\u4eec\u7684\u5206\u8bcd\u5668\u8981\u6709\u4e24\u79cd\uff1a1.\u5206\u8bcd\u5668analyzer\uff1b2.\u641c\u7d22\u7684\u4e00\u4e2a\u5206\u8bcd\u5668\uff0c\u4e13\u95e8\u7528\u4e8e\u641c\u7d22\u7684\uff1asearch_analyzer<\/strong>\uff08\u4e00\u822c\u7528\u4e2d\u6587ik\uff09<\/p>\n

\u5206\u8bcd\u5668\u5206\u4e3a\u4e09\u90e8\u5206\uff1a<\/strong><\/span><\/p>\n

\"elasticsearch<\/p>\n

\"elasticsearch<\/p>\n<\/p>\n

\u6307\u5b9a\u5206\u8bcd\u914d\u7f6e<\/strong><\/span><\/p>\n

\u7f51\u7ad9\uff1a\/github.com\/medcl\/elasticsearch-analysis-pinyin<\/p>\n

\"elasticsearch<\/p>\n

 \u5728\u521b\u5efa\u7d22\u5f15\u5e93\u65f6\uff0c\u901a\u8fc7settings\u914d\u7f6e\u81ea\u5b9a\u4e49\u5206\u8bcd\u5668myanalyzer<\/strong><\/p>\n

 \u5305\u62ectokenizer<\/strong>\u4e0efilter<\/strong>\u2014\u2014>\u5bf9\u5206\u597d\u7684\u8bcd\u505a\u8fdb\u4e00\u6b65\u5904\u7406<\/p>\n

\/\/ \u81ea\u5b9a\u4e49\u62fc\u97f3\u5206\u8bcd\u5668 PUT \/test { \"settings\": { \"analysis\": { \"analyzer\": { \"my_analyzer\": { \"tokenizer\": \"ik_max_word\", \"filter\": \"py\" \/\/\u8fd9\u91cc\u4ea4\u7ed9\u62fc\u97f3\u8fc7\u6ee4\u5668\u7684\u8bdd\uff0c\u4f1a\u5931\u53bb\u4e2d\u6587 } }, \"filter\": { \"py\": { \/\/\u6211\u4eec\u6307\u5b9a\u62fc\u97f3\u8fc7\u6ee4\u5668\u7684\u914d\u7f6e \"type\": \"pinyin\", \"keep_full_pinyin\": false, \"keep_joined_full_pinyin\": true,\/\/\u5168\u62fc \"keep_original\": true,\/\/\u8981\u4e0d\u8981\u4fdd\u7559\u4e2d\u6587 \"limit_first_letter_length\": 16, \"remove_duplicated_term\": true, \"none_chinese_pinyin_tokenize\": false } } } } }<\/code><\/pre>\n

\u62fc\u97f3\u5206\u8bcd\u51fa\u73b0\u7684\u95ee\u9898\uff08\u5012\u6392\u7d22\u5f15\uff09\uff1a<\/strong><\/p>\n

\u5f53\u6211\u4eec\u641c\u7d22\u51fa\u73b0\u540c\u97f3\u8bcd\u65f6\uff0c\u4f1a\u51fa\u73b0\u4e0d\u540c\u8bcd\u7684\u8bbf\u95ee<\/p>\n

\u6bd4\u5982\uff1a<\/strong>\u4f60\u72ee\u5b50\u548c\u8671\u5b50\u8fdb\u884c\u5206\u8bcd->\u62fc\u97f3+\u4e2d\u6587\uff0c\u800c\u4f60\u8fdb\u884c\u641c\u7d22\u65f6\uff0c\u72ee\u5b50\u5206\u8bcd\u51fa\u73b0\u62fc\u97f3shizi\uff0c\u800c\u4f60\u4e4b\u524d\u72ee\u5b50\u4e0e\u8671\u5b50\u7684\u5206\u8bcd\u62fc\u97f3\u90fd\u4e00\u6837\uff0c\u6240\u4ee5\u5c31\u653e\u5728\u4e00\u8d77\u53bb\u4e86\uff0cid\u90fd\u5728\u540c\u4e00\u4e2a\u8bcd\u6761\u4e0a\uff0c\u6240\u4ee5\u4e00\u641c\u72ee\u5b50\u5c31\u4f1a\u51fa\u73b0\u8671\u5b50\uff1b<\/p>\n

\"elasticsearch<\/p>\n

 \u6240\u4ee5\u6211\u4eec\u8981\u5206\u5f00\u4f7f\u7528\uff0c\u5b57\u6bb5\u5728\u521b\u5efa\u5012\u6392\u7d22\u5f15\u65f6\u4f7f\u7528my_analyzer\u5206\u8bcd\u5668\uff1b\u5b57\u6bb5\u5728\u641c\u7d22\u65f6\u4f7f\u7528ik_smart\u5206\u8bcd\u5668<\/strong><\/p>\n

\u641c\u7d22\u7684\u65f6\u5019\u5982\u679c\u4f60\u7528\u4e86\u542b\u6709py\u7684\u5206\u8bcd\u5668\uff0c<\/strong>\u90a3\u4e48\u4f60\u641c\u7684\u5185\u5bb9\u5c31\u4f1a\u5206\u8bcd\u6210\u82f1\u6587\uff0c\u5c31\u4f1a\u5bf9\u7740\u82f1\u6587\u8fdb\u884c\u641c\u7d22\uff0c\u90a3\u4e48\u5c31\u4f1a\u591a\u4e00\u4e9b\u4e0d\u76f8\u5173\u7684\u5185\u5bb9\uff1b<\/p>\n

\u81ea\u5b9a\u4e49\u5206\u8bcd\u5668\u7528\u4e8e\u5b57\u6bb5\u521b\u5efa\u65f6\uff1a\u5305\u62ecik+py<\/p>\n

\u641c\u7d22\u65f6\uff1a\u7528ik<\/p>\n

\"elasticsearch<\/p>\n

\n

Demo\uff08\u57fa\u4e8e\u62fc\u97f3\u5b9e\u73b0\u641c\u7d22\uff09<\/h4>\n<\/blockquote>\n

 \u5bf9mappings\u4e2d\u7ea6\u675f\u7684\u5b57\u6bb5\u4f7f\u7528\u5206\u8bcd\u5668<\/p>\n

#\u81ea\u5b9a\u4e49\u62fc\u97f3\u5206\u8bcd\u5668my_analyzer PUT \/test { \"settings\": { \"analysis\": { \"analyzer\": { \"my_analyzer\": { \"tokenizer\": \"ik_max_word\", \"filter\": \"py\" } }, \"filter\": { \"py\": { \"type\": \"pinyin\", \"keep_full_pinyin\": false, \"keep_joined_full_pinyin\": true, \"keep_original\": true, \"limit_first_letter_length\": 16, \"remove_duplicated_term\": true, \"none_chinese_pinyin_tokenize\": false } } } }, \"mappings\": { \"properties\": { \"name\": { \"type\": \"text\", \"analyzer\": \"my_analyzer\" , \"search_analyzer\": \"ik_smart\" } } } }<\/code><\/pre>\n

\u7136\u540e\u6211\u4eec\u8fdb\u884c\u6d4b\u8bd5\uff0c\u67e5\u770b\u5b57\u6bb5\u5206\u8bcd\u7684\u7ed3\u679c\uff08_analyze\u662f\u7528\u4e8e\u5206\u8bcd\u6d4b\u8bd5\u7684\uff09\uff1a<\/strong><\/p>\n

\"elasticsearch <\/p>\n

\"elasticsearch<\/p>\n

\u7136\u540e\u6211\u4eec\u63d2\u5165\u6587\u6863\u6570\u636e\uff0c\u8fdb\u884c\u67e5\u8be2<\/strong><\/p>\n

\u6839\u636e\u524d\u9762\u7684\u7ea6\u675f\u53ef\u4ee5\u77e5\u9053\uff0c\u63d2\u5165\u7684\u6587\u6863\u6570\u636ename\u88ab\u8fdb\u884c\u5206\u8bcd\uff08\u82f1\u6587\u548c\u4e2d\u6587\u90fd\u6709\uff09\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7GET xxxx\/_search query\u8fdb\u884c\u67e5\u8be2\u83b7\u53d6\u6570\u636e<\/p>\n

\"elasticsearch<\/p>\n

\u6ce8\u610f\uff1a<\/strong>\u8fd9\u91cc\u6d89\u53ca\u4e0a\u8ff0\u641c\u7d22\u95ee\u9898\uff0c\u5982\u679c\u641c\u7d22\u91c7\u7528\u7684\u662f\u62fc\u97f3\u5206\u8bcd\u5668\uff0c\u90a3\u4e48\u6211\u4eecquery\u65f6\u5019\u4f1a\u5c06query\u7684\u5185\u5bb9\u8fdb\u884c\u5206\u8bcd\u2014\u2014>\u5206\u6210\u82f1\u6587\u8fdb\u884c\u67e5\u8be2\uff0c\u800c\u6709\u4e9b\u4e0d\u76f8\u5e72\u7684\u4e2d\u6587\u7684\u82f1\u6587\u662f\u4e00\u6837\u7684\uff0c\u6240\u4ee5\u641c\u7d22\u7684\u5206\u8bcd\u5668\u5e94\u8be5\u8bbe\u7f6e\u4e3aik\u2014\u2014>\u5728\u521b\u5efa\u5012\u6392\u7d22\u5f15\uff08mappings\u4e2d\u7ea6\u675f\u7684\u5b57\u6bb5\uff09\u65f6\u5019\u8fdb\u884c\u8bbe\u7f6e<\/strong><\/p>\n


\n

 \u4e0a\u8ff0\u53ea\u662f\u8bb2\u89e3\u4e86\u62fc\u97f3\u5206\u8bcd\u5668\u2014\u2014>\u5b9e\u73b0\u57fa\u4e8e\u62fc\u97f3\u5bf9\u6587\u6863\u6570\u636e\u7684\u641c\u7d22\uff0c\u4e0b\u9762\u662f\u57fa\u4e8e\u9996\u5b57\u6bcd\u7684\u8865\u5168\u529f\u80fd<\/p>\n

\n

completion suggester\u67e5\u8be2\u5b9e\u73b0\u5b57\u6bb5\u8865\u5168<\/h4>\n<\/blockquote>\n

\u4f5c\u7528\uff1a\u8fd9\u79cd\u67e5\u8be2\u4f1a\u4ee5\u7528\u6237\u8f93\u5165\u5185\u5bb9\u7684\u5f00\u5934\u7684\u8bcd\u6761\u5e76\u8fd4\u56de<\/strong><\/p>\n

\"elasticsearch<\/p>\n

suggest\u76f8\u5f53\u4e8equery\u7c7b\u578b\uff0c\u610f\u601d\uff1a\u8865\u5168\u67e5\u8be2<\/p>\n

title_suggest\u76f8\u5f53\u4e8e\u8865\u5168\u67e5\u8be2\u7684\u540d\u5b57<\/p>\n

text\u5173\u952e\u5b57\uff1a\u610f\u601d\u5c31\u662f\u4ee5....\u5f00\u5934\u53ef\u4ee5\u8fdb\u884c\u81ea\u52a8\u8865\u5168\u67e5\u8be2<\/p>\n

completion\u662f\u8865\u5168\u67e5\u8be2\u7684\u4e00\u79cd\u7c7b\u578b<\/p>\n

field\u662f\u80fd\u591f\u8865\u5168\u67e5\u8be2\u7684\u5b57\u6bb5<\/p>\n

skip_duplicates\uff1a\u8df3\u8fc7\u91cd\u590d\u7684<\/p>\n

size\uff1a\u83b7\u53d6\u7684\u7ed3\u679c\u6570 <\/p>\n

\u8ddf\u666e\u901a\u67e5\u8be2\u4e00\u6837:GET \/\u7d22\u5f15\/_search<\/strong><\/p>\n

\u53ea\u662f\u91cc\u9762\u52a0\u4e86suggest\uff0c\u7136\u540e\u5b57\u6bb5\u7684\u8bdd\uff0c\u9700\u8981type+completion\uff1a\u6307\u660e\u53ef\u4ee5\u8865\u5168<\/p>\n

 \"elasticsearch<\/p>\n

 demo:<\/strong><\/p>\n

1.\u5148\u8bbe\u7f6e\u6587\u6863\u4e2d\u53c2\u4e0e\u8865\u5168\u5b57\u6bb5\u4fe1\u606f\u2014\u2014>\u7c7b\u578b\u4e00\u5b9a\u8981\u662fcompletion<\/strong><\/p>\n

2.\u7136\u540ePOST\u6570\u636e\u5373\u53ef<\/strong><\/p>\n

3.\u6700\u540e\u518d\u81ea\u52a8\u8865\u5168\u67e5\u8be2\uff0c\u5229\u7528suggect\u67e5\u8be2<\/strong><\/p>\n

\"elasticsearch<\/p>\n

 \u7ed3\u679c\uff1a<\/strong><\/p>\n

\u53ef\u4ee5\u53d1\u73b0hits\u662f\u7a7a\u7684\uff0c\u56e0\u4e3a\u6211\u4eec\u6ca1\u6709\u8fdb\u884cquery\uff0c\u6ca1\u6709\u641c\u6587\u6863\uff0c\u800c\u662f\u81ea\u52a8\u8865\u5168\u67e5\u8be2<\/strong><\/p>\n

options\u4e2d\u6709\u81ea\u52a8\u8865\u5168\u7684\u7ed3\u679c<\/strong><\/p>\n

\u8fd9\u4e2atext\u5b57\u6bb5\u5c31\u662f\u6211\u4eec\u5b57\u6bb5\u8865\u5168\u540e\u7684\u7ed3\u679c<\/p>\n

 \"elasticsearch<\/p>\n

 \"elasticsearch<\/p>\n<\/p>\n


\n
\n

\u6848\u4f8b\uff1a\u9152\u5e97\u6570\u636e\u81ea\u52a8\u8865\u5145<\/h4>\n<\/blockquote>\n

\u4e24\u4e2a\u81ea\u5b9a\u4e49\u5206\u8bcd\u5668\uff0c\u4e00\u4e2atext_anlyzer\uff0c\u4e00\u4e2a\u662fcompletion_analyzer\uff1a<\/p>\n

\u610f\u601d\u5c31\u662f\u4e00\u4e2a\u662f\u5168\u6587\u68c0\u7d22\u5c31\u662f\u524d\u9762\u90a3\u4e2a\uff0c\u81ea\u52a8\u8865\u5168\u5c31\u662f\u540e\u9762\u90a3\u4e2a\uff1b<\/p>\n

\"elasticsearch<\/p>\n

\"elasticsearch  <\/p>\n

\u6dfb\u52a0\u65b0\u7684\u5b57\u6bb5\u7ea6\u675fsuggestion<\/strong>\uff0ctype=completion\u8bf4\u660e\u662f\u652f\u6301\u8865\u5168\u7684\uff0c\u5206\u8bcd\u5668\u7528\u7684completion_analyzer<\/strong>\u8bf4\u660e\u662f\u4e0d\u652f\u6301\u5206\u8bcd\u7684\uff0c\u76f4\u63a5\u5b57\u6bb5\u62fc\u97f3\u641c\u7d22 <\/p>\n

 \"elasticsearch<\/p>\n<\/p>\n

settings\uff1a\u5b9a\u4e49\u9152\u5e97\u5206\u8bcd\u5668<\/p>\n

suggection\u91cc\u9762\u5c01\u88c5\u4e86\u54c1\u724c\u4fe1\u606f\u548c\u5546\u5708\u4fe1\u606f\uff08\u81ea\u52a8\u8865\u5168\u7684\u5185\u5bb9\uff09<\/p>\n

\"elasticsearch<\/p>\n

 \u6309\u7167\u9996\u5b57\u6bcds\u67e5\u8be2\uff0c\u81ea\u52a8\u8865\u5168\u7c7b\u578b\u662fcompletion\uff0c\u5b57\u6bb5\u7c7b\u578b\u662fsuggestion<\/p>\n

\"elasticsearch<\/p>\n

\n

 RestClient\u5b9e\u73b0\u8865\u5168<\/h4>\n<\/blockquote>\n
GET \/hotel\/_search \/\/\u51c6\u5907\u8bf7\u6c42 { \"suggest\": { \"mySuggection\": { \/\/\u8865\u5168\u641c\u7d22\u540d\u5b57 \"text\": \"h\", \/\/ \u5173\u952e\u5b57 \"completion\": { \/\/\u8865\u5168\u7c7b\u578b \"FIELD\": \"title\", \/\/\u8865\u5168\u5b57\u6bb5 \"skip_duplicates\":true, \"size\":10 \/\/\u83b7\u53d6\u524d\u5341\u6761\u7ed3\u679c } } } }<\/code><\/pre>\n

\"elasticsearch<\/p>\n

 <\/p>\n

 \/** * \u81ea\u52a8\u8865\u5168\u67e5\u8be2 * @param prefix\uff1a\u641c\u7d22\u524d\u7f00 * @return\uff1aoptions\u4e2dtext\u5185\u5bb9 *\/ @Override public List<String> getSuggestions(String prefix) throws IOException { SearchRequest request = new SearchRequest(\"hotel\"); \/\/2.\u51c6\u5907DSL request.source().suggest(new SuggestBuilder() .addSuggestion(\"suggestions\", SuggestBuilders.completionSuggestion(\"suggection\") .prefix(prefix) .skipDuplicates(true) .size(10))); \/\/3.\u53d1\u9001\u8bf7\u6c42 SearchResponse response = client.search(request, RequestOptions.DEFAULT); \/\/4.\u5904\u7406\u54cd\u5e94 Suggest suggest = response.getSuggest(); CompletionSuggestion suggestions = suggest.getSuggestion(\"suggestions\"); \/\/4.2\u83b7\u53d6options List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions(); \/\/4.3\u904d\u5386 ArrayList<String> result = new ArrayList<>(options.size()); for (CompletionSuggestion.Entry.Option option : options) { String text = option.getText().toString(); result.add(text); } return result; }<\/code><\/pre>\n
\n
\n

\u5b9e\u73b0\u9152\u5e97\u641c\u7d22\u9875\u9762\u8f93\u5165\u6846\u7684\u81ea\u52a8\u8865\u5168<\/p>\n<\/blockquote>\n

\"elasticsearch<\/p>\n

 <\/p>\n<\/p>\n<\/p>\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"elasticsearch shard\u6570\u91cf\u8bbe\u7f6e_reset\u68c0\u9a8c\u81ea\u5b9a\u4e49\u62fc\u97f3\u5206\u8bcd\u5668\uff0c\u6839\u636e\u62fc\u97f3\u5f97\u5230\u5b57\u6bb5\u641c\u7d22\u6587\u6863\u6570\u636e\u62fc\u97f3\u5206\u8bcd\u5668\u4e0d\u4f1a\u5206\u8bcd\uff0c\u800c\u4e14\u4e5f\u6ca1\u6709\u8865\u5145...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6471"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6471"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6471\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}