Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | Email

25.4. 中文分詞插件管理

25.4.1. 安裝插件

curl -s https://raw.githubusercontent.com/oscm/shell/master/search/elasticsearch/elasticsearch-analysis-ik-5.2.2.sh | bash
			

25.4.2. 創建索引

curl -XPUT http://localhost:9200/information
			

25.4.3. 刪除索引

如果索引已經存在請刪除後重新創建索引

curl -XDELETE http://localhost:9200/information/news/_mapping?pretty
curl -XDELETE http://localhost:9200/information/?pretty			
			

25.4.4. 配置索引分詞插件

			

curl -XPOST http://localhost:9200/information/news/_mapping?pretty -d'
{
    "news": {
            "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        },
        "properties": {
            "content": {
                "type": "text",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}'
			
			

25.4.4.1. 測試分詞效果

				
curl -XPOST http://localhost:9200/information/news/ -d'
{"title": "越南胡志明遊記·教堂·管風琴的天籟之音","content":"這是我平生第一次去教堂,也是第一次完整的參加宗教儀式。當我駐足教堂外的時候,耳邊傳來天籟之音,是管風琴,確切的說是電子風琴。真正的管風琴造價昂貴,管風琴通常需要根據教堂尺寸定製,無法量產。我記得中國只有4座管風琴,深圳音樂廳有一座。"}
'
curl -XPOST http://localhost:9200/information/news/ -d'
{"title": "越南胡志明遊記·信仰·法事","content":"佛經的形成過程是與佛教的發展相始終的,按照佛教發展的時間順序,最早形成的是小乘佛教三藏,之後形成的是大乘佛教三藏,最後形成的是密宗三藏。"}
'

curl -XPOST http://localhost:9200/information/news/_search  -d'
{
    "query" : { "term" : { "content" : "佛經" }},
    "highlight" : {
        "pre_tags" : ["<strong>", "<strong>"],
        "post_tags" : ["</strong>", "</strong>"],
        "fields" : {
            "content" : {}
        }
    }
}'		

curl -XPOST http://localhost:9200/information/news/_search  -d'
{
    "query" : { "term" : { "content" : "中國" }},
    "highlight" : {
        "pre_tags" : ["<b>", "<i>"],
        "post_tags" : ["</b>", "</i>"],
        "fields" : {
            "content" : {}
        }
    }
}'