1、创建索引
PUT 192.168.100.102:9200/news
2、创建mapping
POST 192.168.100.102:9200/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 192.168.100.102:9200/news/new/1
{
"title":"印度将因4大矛盾惨败中国 1项曾使印度国母被杀",
"content":"一大堆最近中国和阿三闹矛盾词语,此处省略N字。。。"
}
PUT 192.168.100.102:9200/news/new/2
{
"title":"印度不要敬酒不吃吃罚酒满嘴跑火车 想想1962年",
"content":"一大堆最近中国和阿三闹矛盾词语,此处省略N字。。。"
}
PUT 192.168.100.102:9200/news/new/3
{
"title":"莫迪就中印对峙发声:靠“亚洲古老传统”解决问题",
"content":"一大堆最近中国和阿三闹矛盾词语,此处省略N字。。。"
}
4、全文检索+高亮显示
全文检索:
POST 192.168.100.102:9200/news/new/_search
{
"query" :
{
"match" :
{
"content" : "中国 印度 "
}
}
}
高亮显示
POST 192.168.100.102:9200/news/new/_search
{
"query" :
{
"match" :
{
"content" : "中国 印度 "
}
}
,
"highlight":
{
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}