use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project xmall by Exrick.
the class SearchServiceImpl method search.
/**
* 使用QueryBuilder
* termQuery("key", obj) 完全匹配
* termsQuery("key", obj1, obj2..) 一次匹配多个值
* matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
* multiMatchQuery("text", "field1", "field2"..); 匹配多个字段, field有通配符忒行
*/
@Override
public SearchResult search(String key, int page, int size, String sort, int priceGt, int priceLte) {
try {
Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
SearchResult searchResult = new SearchResult();
// 设置查询条件
// 单字段搜索
QueryBuilder qb = matchQuery("productName", key);
// 设置分页
if (page <= 0) {
page = 1;
}
int start = (page - 1) * size;
// 设置高亮显示
HighlightBuilder hiBuilder = new HighlightBuilder();
hiBuilder.preTags("<a style=\"color: #e4393c\">");
hiBuilder.postTags("</a>");
hiBuilder.field("productName");
// 执行搜索
SearchResponse searchResponse = null;
if (priceGt >= 0 && priceLte >= 0 && sort.isEmpty()) {
searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
true).highlighter(// 设置高亮显示
hiBuilder).setPostFilter(// 过滤条件
QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).get();
} else if (priceGt >= 0 && priceLte >= 0 && sort.equals("1")) {
searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
true).highlighter(// 设置高亮显示
hiBuilder).setPostFilter(// 过滤条件
QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.ASC).get();
} else if (priceGt >= 0 && priceLte >= 0 && sort.equals("-1")) {
searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
true).highlighter(// 设置高亮显示
hiBuilder).setPostFilter(// 过滤条件
QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.DESC).get();
} else if ((priceGt < 0 || priceLte < 0) && sort.isEmpty()) {
searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
true).highlighter(// 设置高亮显示
hiBuilder).get();
} else if ((priceGt < 0 || priceLte < 0) && sort.equals("1")) {
searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
true).highlighter(// 设置高亮显示
hiBuilder).addSort("salePrice", SortOrder.ASC).get();
} else if ((priceGt < 0 || priceLte < 0) && sort.equals("-1")) {
searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
true).highlighter(// 设置高亮显示
hiBuilder).addSort("salePrice", SortOrder.DESC).get();
}
SearchHits hits = searchResponse.getHits();
// 返回总结果数
searchResult.setRecordCount(hits.totalHits);
List<SearchItem> list = new ArrayList<>();
if (hits.totalHits > 0) {
for (SearchHit hit : hits) {
// 总页数
int totalPage = (int) (hit.getScore() / size);
if ((hit.getScore() % size) != 0) {
totalPage++;
}
// 返回结果总页数
searchResult.setTotalPages(totalPage);
// 设置高亮字段
SearchItem searchItem = new Gson().fromJson(hit.getSourceAsString(), SearchItem.class);
String productName = hit.getHighlightFields().get("productName").getFragments()[0].toString();
searchItem.setProductName(productName);
// 返回结果
list.add(searchItem);
}
}
searchResult.setItemList(list);
return searchResult;
} catch (Exception e) {
e.printStackTrace();
throw new XmallException("查询ES索引库出错");
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project kylo by Teradata.
the class ElasticSearchSearchResultTransform method transformResult.
public SearchResult transformResult(String query, int size, int start, SearchResponse searchResponse) {
final String KYLO_DATA = "kylo-data";
final String KYLO_SCHEMA_METADATA = "kylo-schema-metadata";
final String KYLO_FEEDS = "kylo-feeds";
final String KYLO_CATEGORIES = "kylo-categories";
final String ELASTIC_SEARCH = "Elasticsearch";
List<SearchResultData> searchResultData = new ArrayList<>();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
if (searchHit.getIndex().equals(KYLO_DATA)) {
searchResultData.add(getTableSearchResultData(searchHit));
} else if (searchHit.getIndex().equals(SearchIndex.DATASOURCES)) {
searchResultData.add(getSchemaSearchResultData(searchHit));
} else if (searchHit.getIndex().contains(KYLO_FEEDS)) {
// This is dependent upon ModeShape configuration. The prefix will remain the same.
// Hence only checking part of index name.
searchResultData.add(getFeedMetadataSearchResultData(searchHit));
} else if (searchHit.getIndex().contains(KYLO_CATEGORIES)) {
// This is dependent upon ModeShape configuration. The prefix will remain the same.
// Hence only checking part of index name.
searchResultData.add(getCategoryMetadataSearchResultData(searchHit));
} else {
searchResultData.add(getUnknownTypeSearchResultData(searchHit));
}
}
SearchResult elasticSearchResult = new SearchResult();
elasticSearchResult.setQuery(query);
elasticSearchResult.setTotalHits(searchResponse.getHits().getTotalHits());
elasticSearchResult.setFrom((long) (start + 1));
elasticSearchResult.setTo((long) (start + size));
if (elasticSearchResult.getTotalHits() < (start + size)) {
elasticSearchResult.setTo(elasticSearchResult.getTotalHits());
}
if (elasticSearchResult.getTotalHits() == 0) {
elasticSearchResult.setFrom(0L);
}
elasticSearchResult.setTookInMillis(searchResponse.getTookInMillis());
elasticSearchResult.setEngine(ELASTIC_SEARCH);
elasticSearchResult.setSearchResults(searchResultData);
elasticSearchResult.setSearchResultsSummary(getSearchResultSummary());
return elasticSearchResult;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project uavstack by uavorg.
the class SlowOperQueryHandler method queryToList.
/**
* @param data
* @param queryBuilder
* @param postFilter
*/
@SuppressWarnings("rawtypes")
private void queryToList(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter, SortBuilder[] sorts) {
SearchResponse sr = query(data, queryBuilder, postFilter, sorts);
if (sr == null) {
return;
}
SearchHits shits = sr.getHits();
List<Map<String, Object>> records = new ArrayList<Map<String, Object>>();
for (SearchHit sh : shits) {
Map<String, Object> record = sh.getSourceAsMap();
if (record == null) {
continue;
}
records.add(record);
}
data.putResponse("rs", JSONHelper.toString(records));
// 返回总的条数
data.putResponse("count", shits.getTotalHits() + "");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project snow-owl by b2ihealthcare.
the class EsDocumentSearcher method search.
@Override
public <T> Hits<T> search(Query<T> query) throws IOException {
Stopwatch w = Stopwatch.createStarted();
admin.log().trace("Executing query '{}'", query);
final EsClient client = admin.client();
final List<DocumentMapping> mappings = admin.mappings().getDocumentMapping(query);
final DocumentMapping primaryMapping = Iterables.getFirst(mappings, null);
// Restrict variables to the theoretical maximum
final int limit = query.getLimit();
final int toRead = Ints.min(limit, resultWindow);
// TODO support multiple document mappings during query building
final EsQueryBuilder esQueryBuilder = new EsQueryBuilder(primaryMapping, admin.settings(), admin.log());
final QueryBuilder esQuery = esQueryBuilder.build(query.getWhere());
final SearchRequest req = new SearchRequest(admin.getTypeIndexes(mappings).toArray(length -> new String[length]));
// configure caching
req.requestCache(query.isCached());
final SearchSourceBuilder reqSource = req.source().size(toRead).query(esQuery).trackScores(esQueryBuilder.needsScoring()).trackTotalHitsUpTo(Integer.MAX_VALUE);
// field selection
final boolean fetchSource = applySourceFiltering(query.getFields(), primaryMapping, reqSource);
// ES internals require loading the _id field when we require the _source
if (fetchSource) {
reqSource.storedFields(STORED_FIELDS_ID_ONLY);
} else {
reqSource.storedFields(STORED_FIELDS_NONE);
}
// paging config
final boolean isLocalStreaming = limit > resultWindow;
final boolean isLiveStreaming = !Strings.isNullOrEmpty(query.getSearchAfter());
if (isLocalStreaming) {
checkArgument(!isLiveStreaming, "Cannot use searchAfter when requesting more items (%s) than the configured result window (%s).", limit, resultWindow);
} else if (isLiveStreaming) {
reqSource.searchAfter(fromSearchAfterToken(query.getSearchAfter()));
}
// sorting config with a default sort field based on scroll config
addSort(primaryMapping, reqSource, query.getSortBy());
// disable explain explicitly, just in case
reqSource.explain(false);
// disable version field explicitly, just in case
reqSource.version(false);
// perform search
SearchResponse response = null;
try {
response = client.search(req);
} catch (Exception e) {
if (e instanceof ElasticsearchStatusException && ((ElasticsearchStatusException) e).status() == RestStatus.BAD_REQUEST) {
throw new IllegalArgumentException(e.getMessage(), e);
}
admin.log().error("Couldn't execute query", e);
throw new IndexException("Couldn't execute query: " + e.getMessage(), null);
}
SearchHits responseHits = response.getHits();
final TotalHits total = responseHits.getTotalHits();
checkState(total.relation == Relation.EQUAL_TO, "Searches should always track total hits accurately");
final int totalHitCount = (int) total.value;
final SearchHit[] firstHits = responseHits.getHits();
final int firstCount = firstHits.length;
final int remainingCount = Math.min(limit, totalHitCount) - firstCount;
// Add the first set of results
final ImmutableList.Builder<SearchHit> allHits = ImmutableList.builder();
allHits.addAll(responseHits);
// If the client requested all data at once and there are more hits to retrieve, collect them all as part of the request
if (isLocalStreaming && remainingCount > 0) {
admin.log().warn("Returning all matches (totalHits: '{}') larger than the currently configured result_window ('{}') might not be the most " + "efficient way of getting the data. Consider using the index pagination API (searchAfter) instead.", totalHitCount, resultWindow);
while (true) {
// Extract searchAfter values for the next set of results
final SearchHit lastHit = Iterables.getLast(responseHits, null);
if (lastHit == null) {
break;
}
reqSource.searchAfter(lastHit.getSortValues());
// Request more search results, adding them to the list builder
response = client.search(req);
responseHits = response.getHits();
allHits.addAll(responseHits);
}
}
final Class<T> select = query.getSelection().getSelect();
final List<Class<?>> from = query.getSelection().getFrom();
final Hits<T> hits = toHits(select, from, query.getFields(), fetchSource, limit, totalHitCount, query.getSortBy(), allHits.build());
admin.log().trace("Executed query '{}' in '{}'", query, w);
return hits;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project sonarqube by SonarSource.
the class EsTester method getDocuments.
private List<SearchHit> getDocuments(SearchRequest req) {
req.scroll(new TimeValue(60000));
req.source().size(100).sort("_doc", SortOrder.ASC);
SearchResponse response = ES_REST_CLIENT.search(req);
List<SearchHit> result = newArrayList();
while (true) {
Iterables.addAll(result, response.getHits());
response = ES_REST_CLIENT.scroll(new SearchScrollRequest(response.getScrollId()).scroll(new TimeValue(600000)));
// Break condition: No hits are returned
if (response.getHits().getHits().length == 0) {
ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
clearScrollRequest.addScrollId(response.getScrollId());
ES_REST_CLIENT.clearScroll(clearScrollRequest);
break;
}
}
return result;
}
Aggregations