use of org.exoplatform.commons.search.es.ElasticSearchException in project social by Meeds-io.
the class ProfileSearchConnector method buildResult.
private List<Identity> buildResult(String jsonResponse) {
LOG.debug("Search Query response from ES : {} ", jsonResponse);
List<Identity> results = new ArrayList<>();
JSONParser parser = new JSONParser();
Map<?, ?> json = null;
try {
json = (Map<?, ?>) parser.parse(jsonResponse);
} catch (ParseException e) {
throw new ElasticSearchException("Unable to parse JSON response", e);
}
JSONObject jsonResult = (JSONObject) json.get("hits");
if (jsonResult == null)
return results;
//
JSONArray jsonHits = (JSONArray) jsonResult.get("hits");
Identity identity = null;
Profile p;
for (Object jsonHit : jsonHits) {
JSONObject hitSource = (JSONObject) ((JSONObject) jsonHit).get("_source");
String position = (String) hitSource.get("position");
String aboutMe = (String) hitSource.get("aboutMe");
String name = (String) hitSource.get("name");
String userName = (String) hitSource.get("userName");
String firstName = (String) hitSource.get("firstName");
String lastName = (String) hitSource.get("lastName");
String avatarUrl = (String) hitSource.get("avatarUrl");
String email = (String) hitSource.get("email");
String identityId = (String) ((JSONObject) jsonHit).get("_id");
identity = new Identity(OrganizationIdentityProvider.NAME, userName);
identity.setId(identityId);
p = new Profile(identity);
Profile profile = getIdentityProfile(userName);
if (profile == null) {
continue;
}
p.setId(identityId);
p.setAvatarUrl(avatarUrl);
p.setUrl(LinkProvider.getProfileUri(userName));
p.setProperty(Profile.FULL_NAME, name);
p.setProperty(Profile.FIRST_NAME, firstName);
p.setProperty(Profile.LAST_NAME, lastName);
p.setProperty(Profile.POSITION, position);
p.setProperty(Profile.EMAIL, email);
p.setProperty(Profile.USERNAME, userName);
p.setProperty(Profile.ABOUT_ME, aboutMe);
if ((String) profile.getProperty(Profile.EXTERNAL) != null && !((String) profile.getProperty(Profile.EXTERNAL)).isEmpty()) {
p.setProperty(Profile.EXTERNAL, (String) profile.getProperty(Profile.EXTERNAL));
}
if ((String) profile.getProperty(Profile.SYNCHRONIZED_DATE) != null && !((String) profile.getProperty(Profile.SYNCHRONIZED_DATE)).isEmpty()) {
p.setProperty(Profile.SYNCHRONIZED_DATE, (String) profile.getProperty(Profile.SYNCHRONIZED_DATE));
}
if ((String) profile.getProperty(Profile.ENROLLMENT_DATE) != null && !((String) profile.getProperty(Profile.ENROLLMENT_DATE)).isEmpty()) {
p.setProperty(Profile.ENROLLMENT_DATE, (String) profile.getProperty(Profile.ENROLLMENT_DATE));
}
identity.setProfile(p);
results.add(identity);
}
return results;
}
use of org.exoplatform.commons.search.es.ElasticSearchException in project social by Meeds-io.
the class ProfileSearchConnector method getCount.
private int getCount(String jsonResponse) {
LOG.debug("Search Query response from ES : {} ", jsonResponse);
JSONParser parser = new JSONParser();
Map<?, ?> json = null;
try {
json = (Map<?, ?>) parser.parse(jsonResponse);
} catch (ParseException e) {
throw new ElasticSearchException("Unable to parse JSON response", e);
}
JSONObject jsonResult = (JSONObject) json.get("hits");
if (jsonResult == null)
return 0;
Object totalSize = ((JSONObject) jsonResult.get("total")).get("value");
return totalSize == null ? 0 : Integer.parseInt(totalSize.toString());
}
use of org.exoplatform.commons.search.es.ElasticSearchException in project news by exoplatform.
the class NewsESSearchConnector method buildResult.
@SuppressWarnings("rawtypes")
private List<NewsESSearchResult> buildResult(String jsonResponse) {
LOG.debug("Search Query response from ES : {} ", jsonResponse);
List<NewsESSearchResult> results = new ArrayList<>();
JSONParser parser = new JSONParser();
Map json;
try {
json = (Map) parser.parse(jsonResponse);
} catch (ParseException e) {
throw new ElasticSearchException("Unable to parse JSON response", e);
}
JSONObject jsonResult = (JSONObject) json.get("hits");
if (jsonResult == null) {
return results;
}
//
JSONArray jsonHits = (JSONArray) jsonResult.get("hits");
for (Object jsonHit : jsonHits) {
try {
NewsESSearchResult newsSearchResult = new NewsESSearchResult();
JSONObject jsonHitObject = (JSONObject) jsonHit;
JSONObject hitSource = (JSONObject) jsonHitObject.get("_source");
String id = (String) hitSource.get("id");
String posterId = (String) hitSource.get("posterId");
String spaceDisplayName = (String) hitSource.get("spaceDisplayName");
String newsActivityId = (String) hitSource.get("newsActivityId");
Long postedTime = parseLong(hitSource, "postedTime");
Long lastUpdatedTime = parseLong(hitSource, "lastUpdatedTime");
String title = (String) hitSource.get("title");
String body = (String) hitSource.get("body");
JSONObject highlightSource = (JSONObject) jsonHitObject.get("highlight");
List<String> excerpts = new ArrayList<>();
if (highlightSource != null) {
JSONArray bodyExcepts = (JSONArray) highlightSource.get("body");
if (bodyExcepts != null) {
excerpts = Arrays.asList((String[]) bodyExcepts.toArray(new String[0]));
}
}
newsSearchResult.setId(id);
newsSearchResult.setTitle(title);
if (posterId != null) {
Identity posterIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, posterId);
newsSearchResult.setPoster(posterIdentity);
}
newsSearchResult.setPostedTime(postedTime);
newsSearchResult.setLastUpdatedTime(lastUpdatedTime);
newsSearchResult.setSpaceDisplayName(spaceDisplayName);
newsSearchResult.setActivityId(newsActivityId);
String portalName = PortalContainer.getCurrentPortalContainerName();
String portalOwner = CommonsUtils.getCurrentPortalOwner();
StringBuilder newsUrl = new StringBuilder("");
newsUrl.append("/").append(portalName).append("/").append(portalOwner).append("/activity?id=").append(newsActivityId);
newsSearchResult.setNewsUrl(newsUrl.toString());
newsSearchResult.setBody(body);
newsSearchResult.setExcerpts(excerpts);
results.add(newsSearchResult);
} catch (Exception e) {
LOG.warn("Error processing news search result item, ignore it from results", e);
}
}
return results;
}
use of org.exoplatform.commons.search.es.ElasticSearchException in project social by Meeds-io.
the class PeopleElasticUnifiedSearchServiceConnector method buildResult.
protected Collection<SearchResult> buildResult(String jsonResponse, SearchContext context) {
LOG.debug("Search Query response from ES : {} ", jsonResponse);
Collection<SearchResult> results = new ArrayList<>();
JSONParser parser = new JSONParser();
Map json;
try {
json = (Map) parser.parse(jsonResponse);
} catch (ParseException e) {
throw new ElasticSearchException("Unable to parse JSON response", e);
}
JSONObject jsonResult = (JSONObject) json.get("hits");
if (jsonResult == null) {
return results;
}
JSONArray jsonHits = (JSONArray) jsonResult.get("hits");
for (Object jsonHit : jsonHits) {
Identity identity = identityManager.getIdentity(((JSONObject) jsonHit).get("_id").toString(), true);
Profile profile = identity.getProfile();
if (identity.isDeleted()) {
continue;
}
Double score = (Double) ((JSONObject) jsonHit).get("_score");
//
StringBuilder sb = new StringBuilder();
//
if (profile.getEmail() != null) {
sb.append(profile.getEmail());
}
//
List<Map> phones = (List<Map>) profile.getProperty(Profile.CONTACT_PHONES);
if (phones != null && phones.size() > 0) {
sb.append(" - " + phones.get(0).get("value"));
}
//
if (profile.getProperty(Profile.GENDER) != null) {
sb.append(" - " + profile.getProperty(Profile.GENDER));
}
results.add(new SearchResult(profile.getUrl(), profile.getFullName(), profile.getPosition(), sb.toString(), profile.getAvatarUrl() != null ? profile.getAvatarUrl() : LinkProvider.PROFILE_DEFAULT_AVATAR_URL, profile.getCreatedTime(), // is part of the query
score.longValue()));
}
return results;
}
use of org.exoplatform.commons.search.es.ElasticSearchException in project social by Meeds-io.
the class SpaceElasticUnifiedSearchServiceConnector method buildResult.
protected Collection<SearchResult> buildResult(String jsonResponse, SearchContext context) {
LOG.debug("Search Query response from ES : {} ", jsonResponse);
Collection<SearchResult> results = new ArrayList<>();
JSONParser parser = new JSONParser();
Map json;
try {
json = (Map) parser.parse(jsonResponse);
} catch (ParseException e) {
throw new ElasticSearchException("Unable to parse JSON response", e);
}
JSONObject jsonResult = (JSONObject) json.get("hits");
if (jsonResult == null) {
return results;
}
JSONArray jsonHits = (JSONArray) jsonResult.get("hits");
for (Object jsonHit : jsonHits) {
JSONObject hitSource = (JSONObject) ((JSONObject) jsonHit).get("_source");
Space space = spaceService.getSpaceById(((JSONObject) jsonHit).get("_id").toString());
if (space == null) {
continue;
}
String title = getTitleFromJsonResult(hitSource);
String url = getUrlFromJsonResult(space, context);
Long lastUpdatedDate = (Long) hitSource.get("lastUpdatedDate");
if (lastUpdatedDate == null)
lastUpdatedDate = new Date().getTime();
Double score = (Double) ((JSONObject) jsonHit).get("_score");
//
StringBuilder sb = new StringBuilder(String.valueOf(space.getDisplayName()));
sb.append(String.format(" - %s Member(s)", space.getMembers().length));
if (Space.OPEN.equals(space.getRegistration())) {
sb.append(" - Free to Join");
} else if (Space.VALIDATION.equals(space.getRegistration())) {
sb.append(" - Register");
} else if (Space.CLOSED.equals(space.getRegistration())) {
sb.append(" - Invitation Only");
} else {
LOG.debug(space.getRegistration() + " registration unknown");
}
results.add(new SearchResult(url, title, space.getDescription(), sb.toString(), space.getAvatarUrl() != null ? space.getAvatarUrl() : LinkProvider.SPACE_DEFAULT_AVATAR_URL, lastUpdatedDate, // score must not be null as "track_scores" is part of the query
score.longValue()));
}
return results;
}
Aggregations