1. 程式人生 > >ElasticSearch建立索引(中文分詞、拼音搜尋)

ElasticSearch建立索引(中文分詞、拼音搜尋)

    我這裡用到的建立索引主要用來進行中文分詞搜尋和拼音搜尋功能,建立語句如下:

1、建立索引:

PUT /lzjsyd/ 
{
    "index" : {
        "analysis" : {
            "analyzer" : {
                "pinyin_analyzer" : {
                    	"tokenizer" : "rfpinyin",
 			"filter" : "pinyin_first_letter_and_full_pinyin_filter"
                    }
            },
            "tokenizer" : {
                "rfpinyin" : {
                    "type" : "pinyin",
                    "keep_separate_first_letter" : true,
                    "keep_full_pinyin" : true,
                    "keep_original" : true,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "remove_duplicated_term" : false
                }
            },
	    "filter" : {
                "pinyin_first_letter_and_full_pinyin_filter" : {
                    "type" : "pinyin",
                    "keep_first_letter" : true,
                    "keep_full_pinyin" : false
                    "keep_none_chinese" : true,
                    "keep_original" : false,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "trim_whitespace" : true,
                    "keep_none_chinese_in_first_letter" : true
                }
            }
        }
    }
}

2、設定:

PUT /lzjsyd/ 
{
    "index" : {
        "analysis" : {
            "analyzer" : {
                "pinyin_analyzer" : {
                    "tokenizer" : "whitespace",
                    "filter" : "pinyin_first_letter_and_full_pinyin_filter"
                }
            },
            "filter" : {
                "pinyin_first_letter_and_full_pinyin_filter" : {
                    "type" : "pinyin",
                    "keep_first_letter" : true,
                    "keep_full_pinyin" : false,
                    "keep_none_chinese" : true,
                    "keep_original" : false,
                    "limit_first_letter_length" : 16,
                    "lowercase" : true,
                    "trim_whitespace" : true,
                    "keep_none_chinese_in_first_letter" : true
                }
            }
        }
    }
}

3、新增Mapping:

POST /lzjsyd/view_search/_mapping 
{
    "view_search": {
        "properties": {
            "cxmc": {
              "type":"text",
              "analyzer": "ik_max_word",
              "search_analyzer": "ik_max_word",
              "boost": 8,
              "fields": {
			"title":{
			"type": "text",
			"analyzer": "ik_max_word"
			},
                        "pinyin": {
                            "type": "text",
                            "store": false,
                            "term_vector": "with_offsets",
                            "analyzer": "pinyin_analyzer",
                            "boost": 1
                        }
                }
            }
        }
    }
}

4、刪除查詢結果

POST lzjsyd/view_search/_delete_by_query
{  
  "query": {  
    "match_all": {}  
  }
}