下图我存入es的一条测试数据,基于这条数据我们来看下es的各种查询方法
image.png
一·词条搜索
①term query
这个是基本的词条匹配,查询出给定字段里面包含该词条的记录
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.termQuery("name", "cyb"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
查询name字段里面包含cyb词条的记录,是命中的,这里说一下es的term搜索的不是全文搜索,是词条搜索,比如name字段会拆分成cyb,zqj,ccc三个词条,还有个比较特殊的地方,就是es的词条默认都是小写化,比如上面的zqj词条,我们如果去搜索Zqj是搜索不到的,但是zqj是可以搜索到的,即使我们存入的内容是Zqj
②terms query
逻辑和term query一样,只是匹配多个字段,只要匹配一个就代表命中
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.termsQuery("name", "zqj", "cyb"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
③range query
查询包含词条在制定范围内的记录
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.rangeQuery("sex").from("boy").to("boz")
.includeLower(true).includeUpper(true))//开闭区间
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
④exists query
插叙指定字段不为null的记录
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.existsQuery("sex"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
⑤prefix query
查询词条的前缀是指定字符的记录
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.prefixQuery("name", "cy"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
⑥wildcard query
简单的通配符匹配查询,?代表匹配任何一个字符,*代表匹配任何多个字符
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.wildcardQuery("name", "c?b"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
⑦regex query
正则表达式匹配查询
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.regexpQuery("name", "c[a-z]{1}b?"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
⑧fuzzy query
模糊查询,这个用到了莱文斯坦算法,用于计算两个字符串的相似度,如果在设置的相似度之内,那么会被命中
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.fuzzyQuery("name", "yb").fuzziness(Fuzziness.ONE))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
这里我的如果把相似度改为Fuzziness.ZERO,那么yb和cyb的相似度是1,所以是命中失败的,默认是Fuzziness.AUTO
二·全文搜索
① match,我的全文搜索的理解就是它已经把基本的搜索需求封装好,包括自动拆词,使用模糊等
private void search() {
SearchResponse searchResponse = client.prepareSearch("test1").setTypes("user1")
.setQuery(QueryBuilders.matchQuery("name", "cyb ccc"))
.get();
SearchHits hits = searchResponse.getHits();
System.out.println(JSON.toJSONString(hits));
}
②common terms query
为了一些出现率很高,但是并不是需要命中的词准备的查询,比如我们输入一个语句“elasticsearch的查询方式”,里面的“的”这个字就不是我们需要关注的,但是在文档里面应该是会反复出现的,所以在做查询的时候可以配置一些参数来判别哪些是高频无用词,具体的说明看https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-common-terms-query.html,可以使用chrome的翻译功能翻译下再看
网友评论