本文整理汇总了Java中org.elasticsearch.search.builder.SearchSourceBuilder.query方法的典型用法代码示例。如果您正苦于以下问题:Java SearchSourceBuilder.query方法的具体用法?Java SearchSourceBuilder.query怎么用?Java SearchSourceBuilder.query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.search.builder.SearchSourceBuilder
的用法示例。
在下文中一共展示了SearchSourceBuilder.query方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: selectTermAll
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public String selectTermAll(String indexs,String types,String field,String value){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
search.query(QueryBuilders.termQuery(field, value));
}
search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例2: fullTextQuerySingleField
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public String fullTextQuerySingleField(String textQuery, boolean isPhraseQuery,
String fieldName, int start, int count) {
QueryBuilder qb;
if (!isPhraseQuery)
qb = QueryBuilders.matchQuery(fieldName, textQuery);
else
qb = QueryBuilders.matchPhraseQuery(fieldName, textQuery);
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(qb);
ssb.from(start);
if (count > -1)
ssb.size(count);
return ssb.toString();
}
示例3: selectMatchAll
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public String selectMatchAll(String indexs,String types,String field,String value){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
if(!StringUtil.isEmpty(field)&&!StringUtil.isEmpty(value)&&!(field.matches(regex)||field.matches(value))){
search.query(QueryBuilders.matchQuery(field, value));
}
search.aggregation(AggregationBuilders.terms("data").field(field+".keyword"));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例4: elasticSearch
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
private static void elasticSearch(JestClient client, String indexName, String type, String query) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(query);
searchSourceBuilder.query(queryStringQueryBuilder);
Search searchElastic = (Search) new Search.Builder(searchSourceBuilder.toString())
// multiple index or types can be added.
.addIndex(indexName).addType(type).build();
try {
SearchResult elasticRs = client.execute(searchElastic);
List<Hit<Article, Void>> hits = elasticRs.getHits(Article.class);
for (Hit<Article, Void> hit : hits) {
Article talk = hit.source;
System.out.println("Elastic hits: " + talk.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例5: fullTextQueryMultipleFields
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public String fullTextQueryMultipleFields(String textQuery, boolean isPhraseQuery,
List<String> fieldNames, int start, int count) {
BoolQueryBuilder qb = QueryBuilders.boolQuery();
String fn;
Iterator<String>itr = fieldNames.iterator();
while (itr.hasNext()) {
fn = itr.next();
if (!isPhraseQuery)
qb.should(QueryBuilders.matchQuery(fn, textQuery));
else
qb.should(QueryBuilders.matchPhraseQuery(fn, textQuery));
}
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(qb);
ssb.from(start);
if (count > -1)
ssb.size(count);
return ssb.toString();
}
示例6: selectAll
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public String selectAll(String indexs,String types,String condition){
try {
if(StringUtil.isEmpty(indexs))indexs="_all";
if(xclient==null){
init();
}
SearchSourceBuilder search = new SearchSourceBuilder();
search.query(QueryBuilders.queryStringQuery(condition));
search.explain(false);
SearchRequest request = new SearchRequest();
request.searchType(SearchType.DFS_QUERY_THEN_FETCH);
request.source(search);
request.indices(indexs.split(","));
request.types(types.split(","));
SearchResponse response = xclient.search(request);
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例7: find
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public String find() {
try {
final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.sort("_score");
searchSourceBuilder.sort("_uid");
final String queryText = "*";
final SimpleQueryStringBuilder simpleQueryStringBuilder =
new SimpleQueryStringBuilder(queryText);
simpleQueryStringBuilder.defaultOperator(Operator.AND);
searchSourceBuilder.query(simpleQueryStringBuilder);
searchSourceBuilder.size(500);
//LOGGER.info("Elasticsearch query: {}", searchSourceBuilder.toString());
final SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("tweets");
searchRequest.types("tweet");
searchRequest.source(searchSourceBuilder);
final SearchResponse searchResponse = restHighLevelClient.search(searchRequest);
return searchResponse.toString();
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
}
示例8: prefixTextQuerySingleField
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
/**
* Return all hits which begin with <code>prefix</code>, e.g. "Wh"
* @param prefix
* @param fieldName
* @param start
* @param count
* @return
*/
public String prefixTextQuerySingleField(String prefix, String fieldName, int start, int count) {
QueryBuilder qb = QueryBuilders.prefixQuery(fieldName, prefix);
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(qb);
ssb.from(start);
if (count > -1)
ssb.size(count);
return ssb.toString();
}
示例9: t_queryByEsQueryDo2
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
@org.junit.Test
public void t_queryByEsQueryDo2()throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder qb = QueryBuilders.matchAllQuery();
searchSourceBuilder.query(qb);
ShapeBuilder shapeBuilder = ShapeBuilder.newPoint(new Coordinate(116.402257, 39.914548));
QueryBuilder qb2 = QueryBuilders.geoShapeQuery("geometry", shapeBuilder, ShapeRelation.CONTAINS);
QueryBuilder qb3 = QueryBuilders.boolQuery().must(qb).filter(qb2);
searchSourceBuilder.query(qb3);
iEsQueryDao.query(searchSourceBuilder,"twitter","user","user2");
/*logger.info(JSON.toJSONString(es));
Assert.assertNotEquals("1", es.getStatus());*/
}
示例10: findByNameEntry
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
private ResourceDto<ESSearchResult> findByNameEntry(String nameEntry, Integer perPage, Integer page, String... indices) {
if (nameEntry == null || nameEntry.isEmpty()) {
throw new IllegalArgumentException("Name entry can not be null");
}
if (page == null) {
page = FIRST_PAGE_NUM;
}
if (page <= 0) {
throw new IllegalArgumentException("Page must be greater than zero");
}
if (perPage == null) {
perPage = PER_PAGE_DEFAULT;
}
if (perPage <= 0) {
throw new IllegalArgumentException("Per page value must be greater than zero");
}
JsonNode jsonQuery = matchQueryByName(nameEntry);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.wrapperQuery(jsonQuery.toString()));
int offset = (page - 1) * perPage;
sourceBuilder.from(offset);
sourceBuilder.size(perPage);
SearchRequest searchRequest = new SearchRequest(indices);
searchRequest.source(sourceBuilder);
ResourceDto<ESSearchResult> resultPage = new ResourceDto<>();
try {
SearchResponse response = client.search(searchRequest);
JsonNode result = mapper.readTree(response.toString());
resultPage.setResults(mapToResultList(result));
resultPage.setPagination(getPaginationInfo(page, perPage, response.getHits().getTotalHits()));
} catch (IOException e) {
log.warn("Can not get ES search response. Exception: {}", e);
}
return resultPage;
}
示例11: main
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
public static void main(String[] args) {
/* QueryBuilder qb1 = QueryBuilders.matchQuery("a","b");
System.out.println(qb1.toString());*/
String json="{\"query\":{\"match_all\":{}},\"filter\":{\"geo_shape\":{\"geometry\":{\"relation\":\"CONTAINS\",\"shape\":{\"coordinates\":[116.402257,39.914548],\"type\":\"point\"}}}}}";
QueryBuilder qb= QueryBuilders.matchAllQuery();
//System.out.println(qb.toString());
SearchSourceBuilder searchSourceBuilder=new SearchSourceBuilder();
searchSourceBuilder.query(qb);
// System.out.println(searchSourceBuilder.toString());
ShapeBuilder shapeBuilder= ShapeBuilder.newPoint(new Coordinate(116.402257,39.914548));
QueryBuilder qb2= QueryBuilders.geoShapeQuery("geometry",shapeBuilder, ShapeRelation.CONTAINS);
System.out.println(qb2.toString());
//searchSourceBuilder.postFilter(qb2);
QueryBuilder qb3= QueryBuilders.boolQuery().must(qb).filter(qb2);
searchSourceBuilder.query(qb3);
System.out.println(qb3.toString());
System.out.println(searchSourceBuilder.toString());
QueryBuilder qb4= QueryBuilders.boolQuery().must(qb).must(qb2);
System.out.println(qb4.toString());
SortBuilder sort= SortBuilders.geoDistanceSort("pin.location") .point(40, -70).
unit(DistanceUnit.fromString(DistanceUnit.KILOMETERS.toString())).order(SortOrder.DESC);
/* QueryBuilder qb5 = QueryBuilders.geoDistanceQuery("pin.location")
.point(40, -70)
.distance(400, DistanceUnit.fromString(DistanceUnit.KILOMETERS.toString()))
.geoDistance(GeoDistance.ARC);
System.out.println(qb5.toString());
*/
searchSourceBuilder.sort(sort);
System.out.println(searchSourceBuilder.toString());
//QueryBuilder qb3=QueryBuilders.filteredQuery(null,qb2);
//QueryBuilder qb4=QueryBuilders.filteredQuery(qb,qb2);
//searchSourceBuilder.query(qb3.toString());
// searchSourceBuilder.query(qb4);
// System.out.println(qb4.toString());
//System.out.println(searchSourceBuilder.toString());
// System.out.println(qb.toString());
/* QueryBuilder qb2 = QueryBuilders.geoBoundingBoxQuery("pin.location")
.topLeft(40.73, -74.1)
.bottomRight(40.717, -73.99);
//String strstr= JSON.toJSONString(qb2);
System.out.println(qb2.toString());
System.out.println("1111111");*/
}
示例12: wildCardTextQuerySingleField
import org.elasticsearch.search.builder.SearchSourceBuilder; //导入方法依赖的package包/类
/**
* <p>e.g. "W?F*HW"
* where "?" matches any character
* where * matches zero or more characters</p>
* <p>Note, avoid using terms which begin with a wildcard,
* e.g. *foo</p>
* @param textQuery
* @param fieldName
* @param start
* @param count
* @return
* @see https://www.elastic.co/guide/en/elasticsearch/guide/current/_wildcard_and_regexp_queries.html
*/
public String wildCardTextQuerySingleField(String textQuery, String fieldName, int start, int count) {
QueryBuilder qb = QueryBuilders.wildcardQuery(fieldName, textQuery);
SearchSourceBuilder ssb = new SearchSourceBuilder();
ssb.query(qb);
ssb.from(start);
if (count > -1)
ssb.size(count);
return ssb.toString();
}