假设你的环境和我前面都一致的(elasticsearch学习二、安装中文分词ik)查看下分词效果curl -XGET 'http://localhost:9200/index/_analyze?analyzer=ik' -d '中国驻洛杉矶领事馆遭亚裔男子枪击'
确认索引的数据添加:curl -XPOST http://localhost:9200/index/fulltext/1 -d'{'content':'美国留给伊拉克的是个烂摊子吗'}'curl -XPOST http://localhost:9200/index/fulltext/2 -d'{'content':'公安部:各地校车将享最高路权'}'curl -XPOST http://localhost:9200/index/fulltext/3 -d'{'content':'中韩渔警冲突调查:韩警平均每天扣1艘中国渔船'}'curl -XPOST http://localhost:9200/index/fulltext/4 -d'{'content':'中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首'}'
term检索如果content分词后含有中国这个token,就会检索到。即分词后完全匹配curl -XPOST http://localhost:9200/index/fulltext/_search -d'{ 'query' : { 'term' : { 'content' : '中国' }}, 'highlight' : { 'pre_tags' : ['
querystring检索应该是将查询的词先分成词库中存在的词,然后分别去检索。存在任一存在的词即返回,查询词分词后是OR的关系。curl -XPOST http://localhost:9200/index/fulltext/_search -d'{ 'query' : { 'query_string' : { 'default_field' : 'content', 'query' : '中国美国' } }}'返回含有‘中国’或者‘美国’的数据
Java其他内容可以可以看 elasticsearch学习四、JAVA调用主要也就是建立查询的时候term:QueryBuilder qs = QueryBuilders.termQuery('content', '中国');query_string:QueryStringQueryBuilder qs = new QueryStringQueryBuilder('中国美国'); qs.field('content');
运行查看结果