https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-full-text-queries.html
The high-level full text queries are usually used for running full text queries on full text fields like the body of an email. They understand how the field being queried is analyzed and will apply each field’s analyzer (or search_analyzer) to the query string before executing.
The standard query for performing full text queries, including fuzzy matching and phrase or proximity queries.
QueryBuilder query=QueryBuilders.matchQuery(
"name",
"kimchy elasticsearch");
The multi-field version of the match query.
QueryBuilder query=QueryBuilders.multiMatchQuery(
"kimchy elasticsearch",
"user", "message");
A more specialized query which gives more preference to uncommon words.
QueryBuilder query=QueryBuilders.commonTermsQuery("name", "kimchy");
Supports the compact Lucene query string syntax, allowing you to specify AND|OR|NOT conditions and multi-field search within a single query string. For expert users only.
QueryBuilder query=QueryBuilders.queryStringQuery("+kimchy -elasticsearch");
A simpler, more robust version of the query_string syntax suitable for exposing directly to users.
QueryBuilder query=QueryBuilders.simpleQueryStringQuery("+kimchy -elasticsearch");
package cn.hadron.es;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.util.Map;
public class QueryUtil {
private String index="index";
private int size=3;
private SearchHits hits;
private TransportClient client = ESUtil.getClient();
public QueryUtil(String index,int size){
this.index=index;
this.size=size;
}
public QueryUtil query(QueryBuilder query){
//搜索结果存入SearchResponse
SearchResponse response=client.prepareSearch(index)
.setQuery(query) //设置查询器
.setSize(size) //一次查询文档数
.get();
this.hits=response.getHits();
return this;
}
public void print(){
if(hits==null){
return ;
}
for(SearchHit hit:hits){
System.out.println("source:"+hit.getSourceAsString());
System.out.println("index:"+hit.getIndex());
System.out.println("type:"+hit.getType());
System.out.println("id:"+hit.getId());
//遍历文档的每个字段
Map map=hit.getSourceAsMap();
for(String key:map.keySet()){
System.out.println(key+"="+map.get(key));
}
}
}
}
(1)matchQuery
package cn.hadron.es;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class FullTextQuery {
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//构造查询对象
QueryBuilder qb=QueryBuilders.matchQuery(
"title",
"centos");
util.query(qb).print();
}
}
(2)Operator
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
//构造查询对象
//QueryBuilder qb=QueryBuilders.matchQuery("title", "centos");
QueryBuilder qb=QueryBuilders
.matchQuery("title", "centos升级")
.operator(Operator.AND);
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
(3)multiMatchQuery
public static void main(String[] args) {
QueryUtil util=new QueryUtil("website",5);
QueryBuilder qb=QueryBuilders.multiMatchQuery("centos","title","abstract");
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": "搭建Ember开发环境","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS下搭建Ember开发环境","url":"http://url.cn/53947507"}
index:website
type:blog
id:7
author=程裕强
postdate=2016-12-30
abstract=CentOS下搭建Ember开发环境
title=搭建Ember开发环境
url=http://url.cn/53947507
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
(4)