Skip to content

Instantly share code, notes, and snippets.

@zhuqling
Last active August 29, 2015 14:08
Show Gist options
  • Save zhuqling/db3cf862fe11c971e3ee to your computer and use it in GitHub Desktop.
Save zhuqling/db3cf862fe11c971e3ee to your computer and use it in GitHub Desktop.
elastic search使用中文IK分词器

中文全文检索

安装

cp elasticsearch-analysis-ik-1.2.6.jar ./plugins/analysis-ik/ # 复制jar

cp . ./config/ik/ # 复制字库

nano ./config/elasticsearch.yml

追加

index:
  analysis:
    analyzer:
      ik:
        alias:
        - ik_analyzer
        type: org.elasticsearch.index.analysis.IkAnalyzerProvider
      ik_max_word:
        type: ik
        use_smart: false
      ik_smart:
        type: ik
        use_smart: true

#index.analysis.analyzer.default.type: keyword
index.analysis.analyzer.default.type: ik # 设置默认分析器为ik [可选]

删除索引

DELETE /index

创建索引

PUT /index

设置mapping

POST /index/fulltext/_mapping
{
  "fulltext": {
    "_all": {
      "indexAnalyzer": "ik",
      "searchAnalyzer": "ik",
      "term_vector": "no",
      "store": "false"
    },
    "properties": {
      "content": {
        "type": "string",
        "store": "no",
        "term_vector": "with_positions_offsets",
        "indexAnalyzer": "ik",
        "searchAnalyzer": "ik",
        "include_in_all": "true",
        "boost": 8
      }
    }
  }
}

检查mapping

GET /index/fulltext/_mapping

建立索引

POST /index/fulltext/_bulk
{"index":{"_id":1}}
{content:"美国留给伊拉克的是个烂摊子吗"}
{"index":{"_id":2}}
{content:"公安部:各地校车将享最高路权"}
{"index":{"_id":3}}
{content:"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
{"index":{"_id":4}}
{content:"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}

搜索

POST /index/fulltext/_search?pretty=true
{
    "query" : { "term" : { "content" : "烂摊子" }},
    "highlight" : {
        "pre_tags" : ["", ""],
        "post_tags" : ["", ""],
        "fields" : {
            "content" : {}
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment