本文整理汇总了Java中org.elasticsearch.action.search.SearchRequestBuilder.setQuery方法的典型用法代码示例。如果您正苦于以下问题:Java SearchRequestBuilder.setQuery方法的具体用法?Java SearchRequestBuilder.setQuery怎么用?Java SearchRequestBuilder.setQuery使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.action.search.SearchRequestBuilder
的用法示例。
在下文中一共展示了SearchRequestBuilder.setQuery方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertStoredBinaryFields
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
/**
* Make sure we can load stored binary fields.
*/
void assertStoredBinaryFields(String indexName, Version version) throws Exception {
SearchRequestBuilder builder = client().prepareSearch(indexName);
builder.setQuery(QueryBuilders.matchAllQuery());
builder.setSize(100);
builder.addStoredField("binary");
SearchHits hits = builder.get().getHits();
assertEquals(100, hits.getHits().length);
for(SearchHit hit : hits) {
SearchHitField field = hit.field("binary");
assertNotNull(field);
Object value = field.getValue();
assertTrue(value instanceof BytesArray);
assertEquals(16, ((BytesArray) value).length());
}
}
示例2: testScore
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public void testScore() throws Exception {
createIndex("test");
ensureGreen("test");
indexRandom(true,
client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"),
client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"),
client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye"));
ScoreFunctionBuilder<?> score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()));
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE));
req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); // make sure DF is consistent
SearchResponse rsp = req.get();
assertSearchResponse(rsp);
SearchHits hits = rsp.getHits();
assertEquals(3, hits.getTotalHits());
assertEquals("1", hits.getAt(0).getId());
assertEquals("3", hits.getAt(1).getId());
assertEquals("2", hits.getAt(2).getId());
}
示例3: testFromSize
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public void testFromSize() throws Exception {
Builder settings = Settings.builder();
settings.put(SETTING_NUMBER_OF_SHARDS, 1);
settings.put(SETTING_NUMBER_OF_REPLICAS, 0);
assertAcked(prepareCreate("test").setSettings(settings));
for(int i=0;i<5;i++) {
client().prepareIndex("test", "type", ""+i).setSource("text", "hello world").get();
}
refresh();
SearchRequestBuilder request = client().prepareSearch();
request.setQuery(QueryBuilders.termQuery("text", "hello"));
request.setFrom(1);
request.setSize(4);
request.addRescorer(RescoreBuilder.queryRescorer(QueryBuilders.matchAllQuery()), 50);
assertEquals(4, request.get().getHits().getHits().length);
}
示例4: selectAll
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public String selectAll(String indexs,String types,String condition){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.queryStringQuery(condition));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例5: selectMatchAll
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public String selectMatchAll(String indexs,String types,String field,String value){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.matchQuery(field, value));
request.highlighter(new HighlightBuilder().field(field));
request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例6: selectTermAll
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public String selectTermAll(String indexs,String types,String field,String value){
try {
if(client==null){
init();
}
SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
request.setQuery(QueryBuilders.termQuery(field, value));
request.highlighter(new HighlightBuilder().field(field));
request.addAggregation(AggregationBuilders.terms("data").field(field+".keyword"));
request.setExplain(false);
SearchResponse response = request.get();
return response.toString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
示例7: buildQuery
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
private static void buildQuery(SearchRequestBuilder requestBuilder) {
requestBuilder
.setFrom(0)
.setSize(5)
.addSort("@timestamp", SortOrder.DESC);
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
QueryBuilder regexpQuery = QueryBuilders.matchQuery("operation", "账号密码登录");
queryBuilder.must(regexpQuery);
requestBuilder.setQuery(queryBuilder);
}
示例8: countReplies
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
@Override
public long countReplies(String id) {
// Prepare count request
SearchRequestBuilder searchRequest = client
.prepareSearch(getIndex())
.setTypes(getType())
.setFetchSource(false)
.setSearchType(SearchType.QUERY_AND_FETCH)
.setSize(0);
// Query = filter on reference
TermQueryBuilder query = QueryBuilders.termQuery(RecordComment.PROPERTY_REPLY_TO_JSON, id);
searchRequest.setQuery(query);
// Execute query
try {
SearchResponse response = searchRequest.execute().actionGet();
return response.getHits().getTotalHits();
}
catch(SearchPhaseExecutionException e) {
// Failed or no item on index
logger.error(String.format("Error while counting comment replies: %s", e.getMessage()), e);
}
return 1;
}
示例9: queryWithConstraints
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
public List<Map<String, Object>> queryWithConstraints(final String indexName, final String fieldName, final String fieldValue, final Map<String, String> constraints, boolean latest) throws IOException {
SearchRequestBuilder request = this.elasticsearchClient.prepareSearch(indexName)
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setFrom(0);
BoolQueryBuilder bFilter = QueryBuilders.boolQuery();
bFilter.filter(QueryBuilders.constantScoreQuery(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery(fieldName, fieldValue))));
for (Object o : constraints.entrySet()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) o;
bFilter.filter(QueryBuilders.constantScoreQuery(QueryBuilders.termQuery((String) entry.getKey(), ((String) entry.getValue()).toLowerCase())));
}
request.setQuery(bFilter);
// get response
SearchResponse response = request.execute().actionGet();
// evaluate search result
ArrayList<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
SearchHit[] hits = response.getHits().getHits();
for (SearchHit hit: hits) {
Map<String, Object> map = hit.getSourceAsMap();
result.add(map);
}
return result;
}
示例10: query
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
List<SortBuilder> sorts) {
String date = data.getRequest("indexdate");
String currentIndex;
if (date != null) {
// 指定index
currentIndex = this.indexMgr.getIndexByDate(date);
}
else {
// current index
currentIndex = this.indexMgr.getCurrentIndex();
}
SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex)
.setTypes(ThreadAnalysisIndexMgr.JTA_TABLE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
int size = DataConvertHelper.toInt(data.getRequest("size"), -1);
if (from != -1 && size != -1) {
srb = srb.setFrom(from).setSize(size);
}
srb.setQuery(queryBuilder);
if (postFilter != null) {
srb.setPostFilter(postFilter);
}
for (SortBuilder sb : sorts) {
srb.addSort(sb);
}
SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));
return sr;
}
示例11: buildSearchRequest
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
private SearchRequestBuilder buildSearchRequest (EsQueryDo esQueryObj) throws EsException {
if (Check.NuNStrStrict(esClientFactory.getIndexs(esQueryObj.getIndexName()))) {
throw new EsException("没有指定要搜索的索引名称(indexName)");
}
for (ThreadPoolStats.Stats stats : esClientFactory.getClient().threadPool().stats()) {
logger.info(JSON.toJSONString(stats));
}
//加载要搜索索引
SearchRequestBuilder searchRequestBuilder = esClientFactory.getClient().prepareSearch(esClientFactory.getIndexs(esQueryObj.getIndexName()));
//由spring从配置加载要搜索的index的类型
searchRequestBuilder.setTypes(esQueryObj.getTypeName());
//由spring从配置加载要搜索的类型
searchRequestBuilder.setSearchType(SearchType.fromId(esQueryObj.getSearchType()));
//查询可以为null
searchRequestBuilder.setQuery(esQueryObj.getQueryBuilder());
if (!Check.NuNCollection(esQueryObj.getSortBuilders())) {
for (SortBuilder sortBuilder : esQueryObj.getSortBuilders()) {
searchRequestBuilder.addSort(sortBuilder);
}
}
if (!Check.NuNCollection(esQueryObj.getAggregationBuilders())) {
for (AbstractAggregationBuilder aggregationBuilder : esQueryObj.getAggregationBuilders()) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
//设置高亮域
if (esQueryObj.isHighLigth()) {
if (!Check.NuNObject(esQueryObj.highLigthFields())) {
for (String hlFieldName : esQueryObj.highLigthFields()) {
searchRequestBuilder.addHighlightedField(hlFieldName).setHighlighterPreTags(esQueryObj.getHighLigthPreTag())
.setHighlighterPostTags(esQueryObj.getHighLigthPostTag());
}
}
}
//分页
searchRequestBuilder.setFrom(esQueryObj.getFromIndex()).setSize(esQueryObj.getSize());
searchRequestBuilder.setExplain(esQueryObj.isExplain());
return searchRequestBuilder;
}
示例12: query
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
/**
*
* @param data
* @param queryBuilder
* @param postFilter
* @return
*/
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
SortBuilder[] sorts) {
String date = data.getRequest("indexdate");
String currentIndex;
if (date != null) {
// 指定index
currentIndex = this.indexMgr.getIndexByDate(date);
}
else {
// current index
currentIndex = this.indexMgr.getCurrentIndex();
}
SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex)
.setTypes(InvokeChainIndexMgr.IVC_Table).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
int size = DataConvertHelper.toInt(data.getRequest("size"), -1);
if (from != -1 && size != -1) {
srb = srb.setFrom(from).setSize(size);
}
srb.setQuery(queryBuilder);
if (postFilter != null) {
srb.setPostFilter(postFilter);
}
if (sorts != null && sorts.length > 0) {
for (SortBuilder sb : sorts) {
srb.addSort(sb);
}
}
SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));
return sr;
}
示例13: query
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
SortBuilder[] sorts) {
String appid = data.getRequest("appid");
if (appid == null) {
data.putResponse("rs", "ERR");
data.putResponse("msg", "appid is required");
return null;
}
String indexDate = data.getRequest("indexdate");
String currentIndex;
if (indexDate != null) {
// 获得指定的index
currentIndex = this.indexMgr.getIndexByDate(indexDate, appid);
}
else {
// get current index
currentIndex = this.indexMgr.getCurrentIndex(appid);
}
SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex).setTypes(buildTypes(data))
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
int size = DataConvertHelper.toInt(data.getRequest("size"), -1);
if (from != -1 && size != -1) {
srb = srb.setFrom(from).setSize(size);
}
srb.setQuery(queryBuilder);
if (postFilter != null) {
srb.setPostFilter(postFilter);
}
if (sorts != null && sorts.length > 0) {
for (SortBuilder sb : sorts) {
srb.addSort(sb);
}
}
SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));
return sr;
}
示例14: query
import org.elasticsearch.action.search.SearchRequestBuilder; //导入方法依赖的package包/类
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
SortBuilder[] sorts) {
String appid = data.getRequest("appid");
String indexDate = data.getRequest("indexdate");
String currentIndex;
if (indexDate != null) {
// 获得指定的index
currentIndex = this.indexMgr.getIndexByDate(indexDate, appid);
}
else {
// get current index
currentIndex = this.indexMgr.getCurrentIndex(appid);
}
// get logtype for search
SearchRequestBuilder srb = null;
if (StringHelper.isEmpty(data.getRequest("logtype"))) {
srb = client.getClient().prepareSearch(currentIndex).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
}
else {
String logType = data.getRequest("logtype").replace('.', '_');
srb = client.getClient().prepareSearch(currentIndex).setTypes(logType)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
}
int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
int size = DataConvertHelper.toInt(data.getRequest("size"), -1);
if (from != -1 && size != -1) {
long startLine = DataConvertHelper.toLong(data.getRequest("sline"), -1);
long endLine = DataConvertHelper.toLong(data.getRequest("eline"), -1);
// 判断如果只有endline则需要将结果倒序,取其前100行,然后在返回响应时再逆序回来,从而取到endline前的100行
if (startLine == -1 && endLine > -1) {
srb = srb.addSort("l_timestamp", SortOrder.DESC).addSort("l_num", SortOrder.DESC).setFrom(from)
.setSize(size);
}
else {
srb = srb.setFrom(from).setSize(size);
}
}
srb.setQuery(queryBuilder);
if (postFilter != null) {
srb.setPostFilter(postFilter);
}
if (sorts != null && sorts.length > 0) {
for (SortBuilder sb : sorts) {
srb.addSort(sb);
}
}
SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));
return sr;
}