java1234开源博客系统
博客信息

elasticsearch基于smartcn中文分词查询

0
发布时间:『 2018-01-16 20:35』  博客类别:elasticsearch  阅读(5007) 评论(0)

我们新建索引film2

然后映射的时候,指定smartcn分词;


post  http://192.168.1.111:9200/film2/_mapping/dongzuo/

{

    "properties": {

        "title": {

            "type": "text",

"analyzer": "smartcn"

        },

        "publishDate": {

            "type": "date"

        },

        "content": {

            "type": "text",

"analyzer": "smartcn"

        },

        "director": {

            "type": "keyword"

        },

        "price": {

            "type": "float"

        }

    }

}


然后执行前面的数据代码;


这样前面film索引,数据是标准分词,中文全部一个汉字一个汉字分词;film2用了smartcn,根据内置中文词汇分词;


我们用java代码来搞分词搜索;


先定义一个静态常量:

private static final String ANALYZER="smartcn";

/**
 * 条件分词查询
 * @throws Exception
 */
@Test
public void search()throws Exception{
	SearchRequestBuilder srb=client.prepareSearch("film2").setTypes("dongzuo");
	SearchResponse sr=srb.setQuery(QueryBuilders.matchQuery("title", "星球狼").analyzer(ANALYZER))
		.setFetchSource(new String[]{"title","price"}, null)
		.execute()
		.actionGet(); 
	SearchHits hits=sr.getHits();
	for(SearchHit hit:hits){
		System.out.println(hit.getSourceAsString());
	}
}

指定了 中文分词,查询的时候 查询的关键字先进行分词 然后再查询,不指定的话,默认标准分词;


这里再讲下多字段查询,比如百度搜索,搜索的不仅仅是标题,还有内容,所以这里就有两个字段;

我们使用 multiMatchQuery 我们看下Java代码:‘’

/**
 * 多字段条件分词查询
 * @throws Exception
 */
@Test
public void search2()throws Exception{
	SearchRequestBuilder srb=client.prepareSearch("film2").setTypes("dongzuo");
	SearchResponse sr=srb.setQuery(QueryBuilders.multiMatchQuery("非洲星球", "title","content").analyzer(ANALYZER))
		.setFetchSource(new String[]{"title","price"}, null)
		.execute()
		.actionGet(); 
	SearchHits hits=sr.getHits();
	for(SearchHit hit:hits){
		System.out.println(hit.getSourceAsString());
	}
}


关键字:   elasticsearc     smartcn中文分词查询     多字段查询  
关注Java1234微信公众号
博主信息
Java1234_小锋
(知识改变命运,技术改变世界)
Powered by Java1234 V3.0 Copyright © 2012-2016 Java知识分享网 版权所有