本文整理汇总了Java中org.elasticsearch.index.query.QueryBuilders.queryStringQuery方法的典型用法代码示例。如果您正苦于以下问题:Java QueryBuilders.queryStringQuery方法的具体用法?Java QueryBuilders.queryStringQuery怎么用?Java QueryBuilders.queryStringQuery使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.index.query.QueryBuilders
的用法示例。
在下文中一共展示了QueryBuilders.queryStringQuery方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createConfig
import org.elasticsearch.index.query.QueryBuilders; //导入方法依赖的package包/类
public static LogcenterConfig createConfig(JSONObject jsonInput) throws LogConsumerException {
String productLine = String.valueOf(jsonInput.get("productLine"));
String appName = String.valueOf(jsonInput.get("appName"));
QueryBuilder qb = QueryBuilders.queryStringQuery("productLine:'" + productLine + "' AND appName:'" + appName + "'");
SearchResponse response = ElasticsearchClient.getClient()
.prepareSearch(Constants.METADATA_INDEX)
.setTypes(Constants.METADATA_TYPE)
.setQuery(qb)
.get();
JSONObject jsonObject = JSON.parseObject(response.toString());
JSONArray hitArray = (JSONArray) jsonObject.getJSONObject("hits").get("hits");
if (hitArray.size() == 0) {
throw new LogConsumerException("index does not exist,please check the configuration of the .logcenter index");
}
JSONObject document = (JSONObject) hitArray.get(0);
String jsonStr = document.get("_source").toString();
return JSONObject.parseObject(jsonStr, LogcenterConfig.class);
}
示例2: urlParamsToQueryBuilder
import org.elasticsearch.index.query.QueryBuilders; //导入方法依赖的package包/类
public static QueryBuilder urlParamsToQueryBuilder(RestRequest request) {
String queryString = request.param("q");
if (queryString == null) {
return null;
}
QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString);
queryBuilder.defaultField(request.param("df"));
queryBuilder.analyzer(request.param("analyzer"));
queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false));
queryBuilder.lenient(request.paramAsBoolean("lenient", null));
String defaultOperator = request.param("default_operator");
if (defaultOperator != null) {
queryBuilder.defaultOperator(Operator.fromString(defaultOperator));
}
return queryBuilder;
}
示例3: elasticSearch
import org.elasticsearch.index.query.QueryBuilders; //导入方法依赖的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();
}
}
示例4: parseQuerySource
import org.elasticsearch.index.query.QueryBuilders; //导入方法依赖的package包/类
public static QuerySourceBuilder parseQuerySource(RestRequest request) {
String queryString = request.param("q");
if (queryString == null) {
return null;
}
QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(queryString);
queryBuilder.defaultField(request.param("df"));
queryBuilder.analyzer(request.param("analyzer"));
queryBuilder.analyzeWildcard(request.paramAsBoolean("analyze_wildcard", false));
queryBuilder.lowercaseExpandedTerms(request.paramAsBoolean("lowercase_expanded_terms", true));
queryBuilder.lenient(request.paramAsBoolean("lenient", null));
String defaultOperator = request.param("default_operator");
if (defaultOperator != null) {
if ("OR".equals(defaultOperator)) {
queryBuilder.defaultOperator(QueryStringQueryBuilder.Operator.OR);
} else if ("AND".equals(defaultOperator)) {
queryBuilder.defaultOperator(QueryStringQueryBuilder.Operator.AND);
} else {
throw new IllegalArgumentException("Unsupported defaultOperator [" + defaultOperator + "], can either be [OR] or [AND]");
}
}
return new QuerySourceBuilder().setQuery(queryBuilder);
}
示例5: testForClient
import org.elasticsearch.index.query.QueryBuilders; //导入方法依赖的package包/类
@Test
public void testForClient() throws Exception {
QueryBuilder qb = QueryBuilders.queryStringQuery("+kimchy -elasticsearch");
client.prepareSearch().setQuery(qb).execute().actionGet();
}
示例6: queryForObject
import org.elasticsearch.index.query.QueryBuilders; //导入方法依赖的package包/类
/**
* 查询返回一个Map对象
*
* @param esIndexName
* @param type
* @param fields
* @param content
* @param sortField
* @param order
* @param from
* @param size
* @return
*/
public List<Map<String, Object>> queryForObject(String esIndexName, String type, String[] fields, String content, String sortField, SortOrder order, List<String> heightFields, int from, int size) {
SearchRequestBuilder reqBuilder = esClient.prepareSearch(esIndexName)
.setTypes(type).setSearchType(SearchType.DEFAULT)
.setExplain(true);
QueryStringQueryBuilder queryString = QueryBuilders.queryStringQuery("\"" + content + "\"");
for (String k : fields) {
queryString.field(k);
}
queryString.minimumShouldMatch("10");
reqBuilder.setExplain(true);
reqBuilder.setQuery(QueryBuilders.boolQuery().should(queryString))
.setExplain(true);
if (StringUtils.isNotEmpty(sortField) && order != null) {
reqBuilder.addSort(sortField, order);
}
if (from >= 0 && size > 0) {
reqBuilder.setFrom(from).setSize(size);
}
//设置高亮显示
if (heightFields != null) {
for (String hs : heightFields)
reqBuilder.addHighlightedField(hs);
}
reqBuilder.setHighlighterPreTags("<span style=\"color:red\">");
reqBuilder.setHighlighterPostTags("</span>");
SearchResponse resp = reqBuilder.execute().actionGet();
SearchHit[] hits = resp.getHits().getHits();
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (SearchHit hit : hits) {
Map<String, Object> map = new HashMap<String, Object>();
for (String key : hit.getSource().keySet()) {
if (heightFields != null && heightFields.contains(key)) {
map.put(key, hit.getHighlightFields().get(key).fragments()[0]);
} else
map.put(key, hit.getSource().get(key));
}
results.add(map);
}
return results;
}