本文假设你已经搭建好elasticsearch服务器,并在上面装了kibana和IK中文分词组件
elasticsearch+kibana+ik的安装,之前的文章有介绍,可参考。
mapping介绍:定义索引(index)类型(type)的元数据,包括:数据类型、分词行为、建立倒排索引行为、搜索行为等。在搜索的时候会根据这个mapping定义的分词行为、搜索行为进行搜索。
1、创建索引
PUT news
2、创建mapping
POST news/new/_mapping { "new": { "properties": { "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } }
有两个字段:title和content
3、插入数据
PUT news/new/1 { "title":"印度将因4大矛盾惨败中国 1项曾使印度国母被杀", "content":"一大堆最近中国和阿三闹矛盾词语,此处省略N字。。。" }
PUT news/new/2 { "title":"印度不要敬酒不吃吃罚酒满嘴跑火车 想想1962年", "content":"一大堆最近中国和阿三闹矛盾词语,此处省略N字。。。"
}
PUT news/new/3 { "title":"莫迪就中印对峙发声:靠“亚洲古老传统”解决问题", "content":"一大堆最近中国和阿三闹矛盾词语,此处省略N字。。。"
}
4、全文检索+高亮显示
全文检索:
GET news/new/_search { "query" : { "match" : { "content" : "中国 印度 " } } }
高亮显示
GET news/new/_search { "query" : { "match" : { "content" : "中国 印度 " } } , "highlight": { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } }