python连接es_Elasticsearch --- 3. ik中文分词器, python操作es

一.IK中文分词器

1.下载安装

2.测试

#显示结果

{"tokens": [

{"token" : "上海","start_offset" : 0,"end_offset" : 2,"type" : "CN_WORD","position" : 0},

{"token" : "自来水","start_offset" : 2,"end_offset" : 5,"type" : "CN_WORD","position" : 1},

{"token" : "自来","start_offset" : 2,"end_offset" : 4,"type" : "CN_WORD","position" : 2},

{"token" : "水","start_offset" : 4,"end_offset" : 5,"type" : "CN_CHAR","position" : 3},

{"token" : "来自","start_offset" : 5,"end_offset" : 7,"type" : "CN_WORD","position" : 4},

{"token" : "海上","start_offset" : 7,"end_offset" : 9,"type" : "CN_WORD","position" : 5}

]

}

二. Ik 分词器的基本操作

1.ik_max_word(最细粒度的拆分)

#建立索引

PUT ik1

{"mappings": {"doc": {"dynamic": false,"properties": {"content": {"type": "text","analyzer": "ik_max_word"}

}

}

}

}

#添加数据

PUT ik1/doc/1{"content":"今天是个好日子"}

PUT ik1/doc/2{"content":"心想的事儿都能成"}

PUT ik1/doc/3{"content":"我今天不活了"}

开始查询

GET ik1/_search

{"query": {"match": {"content": "心想"}

}

}

显示结果

{"took" : 1,"timed_out" : false,"_shards": {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits": {"total" : 1,"max_score" : 0.2876821,"hits": [

{"_index" : "ik1","_type" : "doc","_id" : "2","_score" : 0.2876821,"_source": {"content" : "心想的事儿都能成"}

}

]

}

}

2.ik_smart(最粗粒度的拆分)

①以最粗粒度拆分

GET _analyze

{"analyzer": "ik_smart","text": "今天是个好日子"}

结果是:

{"tokens": [

你可能感兴趣的:(python连接es)