1、安裝中文分詞器IK
下載地址:https://github.com/medcl/elasticsearch-analysis-ik
在線下載安裝: elasticsearch-plugin.bat install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.2/elasticsearch-analysis-ik-5.5.2.zip
先下載后安裝:elasticsearch-plugin.bat install file:///D:\work\ElasticSearch\plugin\elasticsearch-analysis-ik-5.5.2.zip
2、重啟 elasticsearch
3、創建空索引
curl -XPUT http://127.0.0.1:9200/index_china
或
在kibana的Dev Tools中用 PUT /index_american/
4、創建映射
curl -XPOST http://127.0.0.1:9200/index_china/fulltext/_mapping -d "{\"properties\": {\"content\": {\"type\": \"text\",\"analyzer\": \"ik_max_word\",\"search_analyzer\": \"ik_max_word\"}}}"
或
POST /index_american/fulltext/_mapping
{
"properties":
{
"content":
{
"type": "text",
"analyzer":
"ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
5、索引數據
POST /index_china/fulltext
{
"content" : "中國是世界上人口最多的國家",
"title" : "中國",
"tags" : [ "中國", "人口" ]
}
批量索引數據
POST /_bulk { "create": { "_index": "index_china", "_type": "fulltext", "_id": 1 } } { "title": "周星馳最新電影" } { "create": { "_index": "index_china", "_type": "fulltext", "_id": 2 } } { "title": "周星馳最好看的新電影" } { "create": { "_index": "index_china", "_type": "fulltext", "_id": 3 } } { "title": "周星馳最新電影,最好,新電影" } { "create": { "_index": "index_china", "_type": "fulltext", "_id": 4 } } { "title": "最最最最好的新新新新電影" } { "create": { "_index": "index_china", "_type": "fulltext", "_id": 5 } } { "title": "I'm not happy about the foxes" }
6、查詢
GET /index_china/fulltext/_search
{
"query": {
"match": {
"content": "中國"
}
}
}
7、最大分詞和最小分詞
ik_smart,
ik_max_word
GET /_analyze { "analyzer": "ik_smart", "text": "中華人民共和國" } GET /_analyze { "analyzer": "ik_max_word", "text": "中華人民共和國" }
#刪除索引 DELETE /ott_test #創建索引 PUT /ott_test { "mappings": { "ott_type" : { "properties" : { "title" : { "type" : "text", "index":true, "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" }, "date" : { "type" : "date" }, "keyword" : { "type" : "keyword" }, "source" : { "type" : "keyword" }, "link" : { "type" : "keyword" } } } } } #索引數據 POST /ott_test/ott_type { "title":"微博新規惹爭議:用戶原創內容版權歸屬於微博?", "link":"http://www.yidianzixun.com/article/0HHoxgVq", "date":"2017-09-17", "source":"虎嗅網", "keyword":"內容" } #分析 GET /ott_test/_analyze { "field": "title", "text": "內容" } #查詢 GET /ott_test/ott_type/_search { "query": { "match": { "title": "內容" } } } #只查詢title和date兩個字段的數據 GET /ott_test/ott_type/_search { "query": {"match_all": {}}, "_source": ["title","date"] }