本文整理汇总了Java中io.searchbox.core.SearchResult类的典型用法代码示例。如果您正苦于以下问题:Java SearchResult类的具体用法?Java SearchResult怎么用?Java SearchResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SearchResult类属于io.searchbox.core包,在下文中一共展示了SearchResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetByModulePackage
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Test
public void testGetByModulePackage() throws Exception {
SearchResult result = client.execute(new Search.Builder(new SearchSourceBuilder()
.size(1)
.query(QueryBuilders.matchQuery("modulePackage", "testing-mod")).toString())
.addIndex(AddOnInfoAndVersions.ES_INDEX)
.build());
SearchResult resultNotFound = client.execute(new Search.Builder(new SearchSourceBuilder()
.size(1)
.query(QueryBuilders.matchQuery("modulePackage", "fake-mod")).toString())
.addIndex(AddOnInfoAndVersions.ES_INDEX)
.build());
List<AddOnInfoAndVersions> searchResult = result.getHits(AddOnInfoAndVersions.class).stream()
.map(sr -> sr.source)
.collect(Collectors.toList());
List<AddOnInfoAndVersions> searchResultNotFound = resultNotFound.getHits(AddOnInfoAndVersions.class).stream()
.map(sr -> sr.source)
.collect(Collectors.toList());
assertNotNull(searchResult);
assertThat(searchResultNotFound, IsEmptyCollection.empty());
System.out.println("Module Package Name: "+searchResult.get(0).getModulePackage());
}
示例2: elasticSearch
import io.searchbox.core.SearchResult; //导入依赖的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();
}
}
示例3: getDateHistogram
import io.searchbox.core.SearchResult; //导入依赖的package包/类
public List<Pair<String, Long>> getDateHistogram(UsageLogSearchQuery searchQuery) {
String query = getQuery(searchQuery);
Search search = new Search.Builder(query)
.addIndex(StatisticConstant.INDEX_NAME)
.addType(StatisticConstant.INDEX_TYPE)
.build();
List<Pair<String, Long>> data = new ArrayList<>();
try {
SearchResult result = client.execute(search);
MetricAggregation aggregation = result.getAggregations();
DateHistogramAggregation dateHistogram = aggregation.getDateHistogramAggregation("event_over_time");
for (DateHistogramAggregation.DateHistogram unit : dateHistogram.getBuckets()) {
data.add(new Pair(unit.getTimeAsString(), unit.getCount()));
}
} catch (IOException ioe) {
logger.log(Level.INFO, null, ioe);
}
return data;
}
开发者ID:pengchengluo,项目名称:Peking-University-Open-Research-Data-Platform,代码行数:20,代码来源:UsageLogSearchServiceBean.java
示例4: doInBackground
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
protected Pair<SearchResult, Boolean> doInBackground(String... search_parameters) {
verifySettings();
String typeId = search_parameters[0];
Search search = new Search.Builder("{ \"size\" : 100 } ").addIndex(INDEX).addType(typeId).build();
try {
SearchResult result = client.execute(search);
if (result.isSucceeded()) {
return new Pair<>(result, Boolean.TRUE);
} else {
Log.i("Error", "the search query failed to find any objects that matched");
}
}
catch (Exception e) {
Log.i("Error", "Something went wrong when we tried to communicate with the elasticsearch server!");
return new Pair<>(null, Boolean.FALSE);
}
return new Pair<>(null, Boolean.TRUE);
}
示例5: getListOf
import io.searchbox.core.SearchResult; //导入依赖的package包/类
/**
* Search for all records with one of the specified ids in the list
* @param typeId the type template id that all results must match
* @param tempClass the java class of the generic type T
* @param ids a list of IDs that an object could possibly have if it is returned
* @param <T> generic representing the java type corresponding to that type ID
* @return the list of all records matching that type ID that have one of the ids in the list
*/
public static <T extends ElasticSearchable> List<T> getListOf(String typeId, Class<T> tempClass, List<String> ids){
Pair<SearchResult, Boolean> result = searchByIds(typeId, getCompleteIdsListQuery(ids, typeId));
if (result.first != null && result.first.isSucceeded()) {
List<T> found = result.first.getSourceAsObjectList(tempClass);
// transform result into json
JsonObject jo = result.first.getJsonObject();
// get array of only the records in the database, and not the other parts of the result object
JsonArray array = jo.get("hits").getAsJsonObject().get("hits").getAsJsonArray();
// look at each record individually
for (int i = 0; i < array.size(); ++i){
JsonObject record = array.get(i).getAsJsonObject();
// set the id of this object since jest does not do it automatically
found.get(i).setId(record.get("_id").getAsString());
}
return found;
}
return new ArrayList<>();
}
示例6: getObject
import io.searchbox.core.SearchResult; //导入依赖的package包/类
/**
* Search for the record with the specified parameter values
* @param typeId the type template id of the result
* @param tempClass the java class of the generic type T
* @param values map where key=parameter and value=required record value for that parameter
* @param <T> generic representing the java type corresponding to that type ID
* @return (@nullable result, success) as a pair
*/
public static <T extends ElasticSearchable> Pair<T, Boolean> getObject(String typeId, Class<T> tempClass, Map<String, String> values){
Pair<SearchResult, Boolean> result = search(typeId, getQueryFromMap(values));
if (result.first != null && result.first.isSucceeded()) {
JsonObject jo = result.first.getJsonObject();
// array of all of the records that match the search parameters
JsonArray array = jo.get("hits").getAsJsonObject().get("hits").getAsJsonArray();
if (array.size() == 0)
return new Pair<>(null, Boolean.TRUE);
String foundId = array.get(array.size() - 1).getAsJsonObject().get("_id").getAsString();
// use the last object as the source
T obj = result.first.getSourceAsObjectList(tempClass).get(array.size() - 1);//result.getSourceAsObject(tempClass);
obj.setId(foundId);
return new Pair<>(obj, Boolean.TRUE);
}
return new Pair<>(null, result.first != null && result.first.isSucceeded());
}
示例7: doInBackground
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
protected ArrayList<HabitEvent> doInBackground(String... eids) {
verifySettings();
ArrayList<HabitEvent> habitEvents = new ArrayList<HabitEvent>();
String query = "{\"query\": {\"match\" : { \"eid\" : \"" + eids[0] + "\" }}}";
Search search = new Search.Builder(query).addIndex(db).addType(habitEventType).build();
try {
SearchResult result = client.execute(search);
if (result.isSucceeded()) {
List<HabitEvent> foundHabitEvent = result.getSourceAsObjectList(HabitEvent.class);
habitEvents.addAll(foundHabitEvent);
} else {
Log.i("Error2", "Something went wrong when we tried to communicate with the elasticsearch server");
}
}
catch (Exception e) {
Log.i("Error1", "Something went wrong when we tried to communicate with the elasticsearch server!");
}
return habitEvents;
}
示例8: doInBackground
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
protected ArrayList<Profile> doInBackground(String... search_parameters){
verifySettings();
ArrayList<Profile> profiles = new ArrayList<Profile>();
String query = "{\n"
+ " \"query\" : {\n"
+ " \"term\" : { \"username\" : \""+search_parameters[0]+"\"}\n"
+ " }\n"
+ "}";
Search search = new Search.Builder(query).addIndex(appESIndex).addType("profile").build();
try{
SearchResult result = client.execute(search);
if (result.isSucceeded()){
List<Profile> foundProfiles = result.getSourceAsObjectList(Profile.class);
profiles.addAll(foundProfiles);
}else{
Log.i("Error","ElasticSearch search query failed");
}
}catch (Exception e){
Log.i("Error","ElasticSearch filed to find profiles");
}
return profiles;
}
示例9: getByModulePackage
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
public AddOnInfoAndVersions getByModulePackage(String modulePackage) throws IOException {
SearchResult result = client.execute(new Search.Builder(new SearchSourceBuilder()
.size(1)
.query(QueryBuilders.matchQuery("modulePackage", modulePackage)).toString())
.addIndex(AddOnInfoAndVersions.ES_INDEX)
.build());
List<AddOnInfoAndVersions> modules = result.getHits(AddOnInfoAndVersions.class).stream()
.map(sr -> sr.source)
.collect(Collectors.toList());
if (modules.isEmpty()) {
return null;
}
return modules.get(0);
}
示例10: jestSearchModel
import io.searchbox.core.SearchResult; //导入依赖的package包/类
private static void jestSearchModel(JestClient client, String indexName, String type, String query) {
Search.Builder searchBuilder = new Search.Builder(query).addIndex(indexName).addType(type);
try {
SearchResult result = client.execute(searchBuilder.build());
System.out.println(result.getJsonObject());
List<Hit<Article, Void>> hits = result.getHits(Article.class);
for (Hit<Article, Void> hit : hits) {
Article talk = hit.source;
System.out.println("JEST Search model hit: " + talk.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例11: getOffers
import io.searchbox.core.SearchResult; //导入依赖的package包/类
/**
* Sub-task: grab the offers for a request and then populate a request with them.
* @see #populate(Request, List)
*/
private static void getOffers(RequestList foundRequests) {
for( Request request : foundRequests ) {
// TODO filter requests that will not have offering drivers?
String query =
"{ \"from\":0, \"size\":1000,\n" +
" \"query\": { \"match\": { \"requestID\" : \"" + request.getId() + "\" } }\n" +
"}";
Search search = new Search.Builder(query)
.addIndex("cmput301f16t01")
.addType("offer")
.build();
try {
SearchResult result = client.execute(search);
if (result.isSucceeded()) {
List<Offer> offers = result.getSourceAsObjectList(Offer.class);
populate( request, offers );
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例12: doInBackground
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
protected User doInBackground(String... search_parameters) {
verifySettings();
String search_string = "{\"from\": 0, \"size\": 1, \"query\": {\"match\": {\"username\": \"" + search_parameters[0] + "\"}}}";
Search search = new Search.Builder(search_string)
.addIndex("cmput301f16t01")
.addType("user")
.build();
User foundUser = null;
try {
SearchResult result = client.execute(search);
if (result.isSucceeded()) {
foundUser = result.getSourceAsObject(User.class);
} else {
return null;
}
} catch (IOException e) {
e.printStackTrace();
Log.i("Error", "Something went wrong when we tried to talk to elastic search");
}
return foundUser;
}
示例13: mapResults
import io.searchbox.core.SearchResult; //导入依赖的package包/类
public <T> AggregatedPage<T> mapResults(SearchResult response, Class<T> clazz, List<AbstractAggregationBuilder> aggregations, Pageable pageable) {
LinkedList<T> results = new LinkedList<>();
for (SearchResult.Hit<JsonObject, Void> hit : response.getHits(JsonObject.class)) {
if (hit != null) {
results.add(mapSource(hit.source, clazz));
}
}
String scrollId = null;
if (response instanceof ExtendedSearchResult) {
scrollId = ((ExtendedSearchResult) response).getScrollId();
}
return new AggregatedPageImpl<>(results, pageable, response.getTotal(), response.getAggregations(), scrollId);
}
示例14: mapResults
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
public <T> AggregatedPage<T> mapResults(SearchResult response, Class<T> clazz, Pageable pageable) {
String scrollId = ((ExtendedSearchResult) response).getScrollId();
List<T> result = new ArrayList<>();
for (SearchResult.Hit<T, Void> searchHit : response.getHits(clazz)) {
if (response.getHits(clazz).size() <= 0) {
return new AggregatedPageImpl<>(Collections.emptyList(), scrollId);
}
result.add(searchHit.source);
}
if (result.size() > 0) {
return new AggregatedPageImpl<>(result, scrollId);
}
return new AggregatedPageImpl<>(Collections.emptyList(), scrollId);
}
示例15: doInBackground
import io.searchbox.core.SearchResult; //导入依赖的package包/类
@Override
protected ArrayList<User> doInBackground(String... search_parameters) {
verifySettings();
ArrayList<User> users = new ArrayList<User>();
Search search = new Search.Builder(search_parameters[0]).addIndex(INDEX).addType(USER).build();
try {
SearchResult result = client.execute(search);
if (result.isSucceeded()) {
List<User> foundUsers = result.getSourceAsObjectList(User.class);
users.addAll(foundUsers);
} else {
Log.i("Error", "The search query failed to find users");
}
} catch (Exception e) {
Log.i("Error", "Something went wrong when communicating with the server!");
}
return users;
}