分词查询大概需要两步操作:1.设置ES 某index下的mapping,2,针对字段进行query搜索
前提是先安装ES的分词插件,参考地址:http://ludizhang.iteye.com/blog/2323939
1. 设置index mapping
向ES发送http PUT请求,
url:http://ip:port/indexName
postBody :
{ "mappings": { "testBase2": { "properties": { "field1": { "type": "string", "index": "analyzed", "analyzer": "ik", "search_analyzer": "ik", "store":"yes" }, "field1": { "type": "string", "index": "analyzed", "analyzer": "ik", "search_analyzer": "ik",-- 指定分词器 "store":"yes" } } } } }
怎么判断设置成功了呢,可以参考[img]
[/img]
在那黑框中有mapingg信息,不是setting里面的mapping哦,而是直接一个mapping节点,
同时图片中的页面是基于ES的_plugin/head/插件的
2.分词查询 向ES 发送 查询的POST请求
url:http://ip:port/indexName/typeName/_search
{ "query": { "bool": { "must": [ { "query_string": { "analyzer": "ik",-- 分析器,基于IK分词 "default_field": "field1",-- 查询字段 "query": "中国和美国",-- 匹配内容 "boost": 6 -- 查询权重 }, "query_string": { "analyzer": "ik", "default_field": "field2", "query": "中国和美国", "boost": 4 } } ] } }, "size": 10,-- 分页设置,每页条数 "from": 0-- 开始索引 }
查询结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,-- 总条数
"max_score": 1,
"hits": [
{
"_index": "index",
"_type": "testBase2",
"_id": "3187",
"_score": 1,
"_source": {
"brandName": "test5",
"classifyId": 23,
"labelWord": "标签3",
"videoName": "投放两段,9-12,14-16",
"brandId": 6,
"videoDesc": "视频简介5",
"videoId": 3187,
"classifyName": "生活aa",
"keyWord": "关键4"
}
},
....
]
}
}
********************* 更新,用更加简单发方式进行复杂的查询
{ "fields": ["videoName","videoDesc"],//指定返回的字段 "query": { "bool": { "must": [ { "query_string": { "fields": ["videoName^9","videoDesc^1"],//查询字段+权重 "analyzer": "ik", "query": "解决方式" } } ] } },"from": 0, "size": 60 }
返回的结果和上一个查询方法返回的格式差不多,区别在于我们的这个查询是指定字段查询在
返回的数据部分
{ "took": 38, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2444, "max_score": 1, "hits": [ { "_index": "index", "_type": "testBase2", "_id": "1230", "_score": 1, "fields": { "videoName": [ "一路向北" ], "videoDesc": [ "开辆奔驰,一路向北。在每条路上,玩的是乐趣,若能变换自如,我必飞奔到底。|奔驰" ] } } ] } }
区别在于 内部的hits 中的_source:字段,变成了fields字段,返回的数据格式由简单json数据 变为了 json 嵌套 jsonArray数据,解析起来比较麻烦一点,这个看个人爱好了