ElasticSearch 6.x 學習筆記:31.Java API之詞項查詢
1、term查詢
Find documents which contain the exact term specified in the field specified.
package cn.hadron;
import cn.hadron.es.QueryUtil;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class TermQuery {
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.termQuery("title","vmware");
util.query(qb).print();
}
}
source:{ "title": "vmware複製虛擬機器","author":"程裕強","postdate":"2016-12-29","abstract":"vmware複製虛擬機器","url":"http://url.cn/53946664"}
index:website
type:blog
id: 4
author=程裕強
postdate=2016-12-29
abstract=vmware複製虛擬機器
title=vmware複製虛擬機器
url=http://url.cn/53946664
2、terms查詢
Find documents which contain any of the exact terms specified in the field specified.
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.termsQuery("title","centos","yum");
util.query(qb).print();
}
source:{ "title": "CentOS更換國內yum源","author":"程裕強","postdate":"2016-12-30","abstract":"CentOS更換國內yum源","url":"http://url.cn/53946911"}
index:website
type:blog
id:6
author=程裕強
postdate=2016-12-30
abstract=CentOS更換國內yum源
title=CentOS更換國內yum源
url=http://url.cn/53946911
source:{ "title": "CentOS升級gcc","author":"程裕強","postdate":"2016-12-25","abstract":"CentOS升級gcc","url":"http://url.cn/53868915"}
index:website
type:blog
id:3
author=程裕強
postdate=2016-12-25
abstract=CentOS升級gcc
title=CentOS升級gcc
url=http://url.cn/53868915
3、range查詢
Find documents where the field specified contains values (dates, numbers, or strings) in the range specified.
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.rangeQuery("postdate").from("2017-01-01").to("2017-12-31").format("yyyy-MM-dd");
util.query(qb).print();
}
source:{ "title": "es高亮","author":"程裕強","postdate":"2017-01-03","abstract":"Elasticsearch查詢關鍵字高亮","url":"http://url/53991802"}
index:website
type:blog
id:8
author=程裕強
postdate=2017-01-03
abstract=Elasticsearch查詢關鍵字高亮
title=es高亮
url=http://url/53991802
4、exists查詢
Find documents where the field specified contains any non-null value.
5、prefix查詢
Find documents where the field specified contains terms which being with the exact prefix specified.
package cn.hadron;
import cn.hadron.es.QueryUtil;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class TermQuery {
public static void main(String[] args) {
QueryUtil util=new QueryUtil("my-index",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.prefixQuery("name","程");
util.query(qb).print();
}
}
source:{
"name":"程裕強",
"age":31,
"gender":"男",
"salary":20000,
"dep":"bigdata"
}
index:my-index
type:persion
id:5
gender=男
name=程裕強
salary=20000
age=31
dep=bigdata
6、wildcard查詢
Find documents where the field specified contains terms which match the pattern specified, where the pattern supports single character wildcards (?) and multi-character wildcards (*)
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.wildcardQuery("title","*yum*");
util.query(qb).print();
}
source:{ "title": "CentOS更換國內yum源","author":"程裕強","postdate":"2016-12-30","abstract":"CentOS更換國內yum源","url":"http://url.cn/53946911"}
index:website
type:blog
id:6
author=程裕強
postdate=2016-12-30
abstract=CentOS更換國內yum源
title=CentOS更換國內yum源
url=http://url.cn/53946911
7、regexp查詢
Find documents where the field specified contains terms which match the regular expression specified.
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.regexpQuery("title","gc.*");
util.query(qb).print();
}
source:{ "title": "CentOS升級gcc","author":"程裕強","postdate":"2016-12-25","abstract":"CentOS升級gcc","url":"http://url.cn/53868915"}
index:website
type:blog
id:3
author=程裕強
postdate=2016-12-25
abstract=CentOS升級gcc
title=CentOS升級gcc
url=http://url.cn/53868915
8、fuzzy查詢
Find documents where the field specified contains terms which are fuzzily similar to the specified term. Fuzziness is measured as a Levenshtein edit distance of 1 or 2.
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//構造查詢物件
QueryBuilder qb=QueryBuilders.fuzzyQuery("title","vmwere");
util.query(qb).print();
}
source:{ "title": "vmware複製虛擬機器","author":"程裕強","postdate":"2016-12-29","abstract":"vmware複製虛擬機器","url":"http://url.cn/53946664"}
index:website
type:blog
id:4
author=程裕強
postdate=2016-12-29
abstract=vmware複製虛擬機器
title=vmware複製虛擬機器
url=http://url.cn/53946664
9、type查詢
Find documents of the specified type.
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",2);
//構造查詢物件
QueryBuilder qb=QueryBuilders.typeQuery("blog");
util.query(qb).print();
}
source:{ "title": "libstdc++.so.6","author":"程裕強","postdate":"2016-12-30","abstract":"libstdc++.so.6問題解決","url":"http://url.cn/53946911"}
index:website
type:blog
id:5
author=程裕強
postdate=2016-12-30
abstract=libstdc++.so.6問題解決
title=libstdc++.so.6
url=http://url.cn/53946911
source:{ "title": "es高亮","author":"程裕強","postdate":"2017-01-03","abstract":"Elasticsearch查詢關鍵字高亮","url":"http://url/53991802"}
index:website
type:blog
id:8
author=程裕強
postdate=2017-01-03
abstract=Elasticsearch查詢關鍵字高亮
title=es高亮
url=http://url/53991802
10、ids查詢
Find documents with the specified type and IDs.
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",2);
//構造查詢物件
QueryBuilder qb=QueryBuilders.idsQuery().addIds("1","3");
util.query(qb).print();
}
source:{ "title": "Ambari原始碼編譯","author":"程裕強","postdate":"2016-12-21","abstract":"CentOS7.x下的Ambari2.4原始碼編譯","url":"http://url.cn/53788351"}
index:website
type:blog
id:1
author=程裕強
postdate=2016-12-21
abstract=CentOS7.x下的Ambari2.4原始碼編譯
title=Ambari原始碼編譯
url=http://url.cn/53788351
source:{ "title": "CentOS升級gcc","author":"程裕強","postdate":"2016-12-25","abstract":"CentOS升級gcc","url":"http://url.cn/53868915"}
index:website
type:blog
id:3
author=程裕強
postdate=2016-12-25
abstract=CentOS升級gcc
title=CentOS升級gcc
url=http://url.cn/53868915