Search in sources :

Example 1 with ElasticSearchException

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;
}
Also used : JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Identity(org.exoplatform.social.core.identity.model.Identity) ElasticSearchException(org.exoplatform.commons.search.es.ElasticSearchException) Profile(org.exoplatform.social.core.identity.model.Profile)

Example 2 with ElasticSearchException

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());
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) ElasticSearchException(org.exoplatform.commons.search.es.ElasticSearchException)

Example 3 with ElasticSearchException

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;
}
Also used : JSONArray(org.json.simple.JSONArray) ParseException(org.json.simple.parser.ParseException) ElasticSearchException(org.exoplatform.commons.search.es.ElasticSearchException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Identity(org.exoplatform.social.core.identity.model.Identity) ElasticSearchException(org.exoplatform.commons.search.es.ElasticSearchException)

Example 4 with ElasticSearchException

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;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) SearchResult(org.exoplatform.commons.api.search.data.SearchResult) Profile(org.exoplatform.social.core.identity.model.Profile) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) ParseException(org.json.simple.parser.ParseException) Identity(org.exoplatform.social.core.identity.model.Identity) HashMap(java.util.HashMap) Map(java.util.Map) ElasticSearchException(org.exoplatform.commons.search.es.ElasticSearchException)

Example 5 with ElasticSearchException

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;
}
Also used : Space(org.exoplatform.social.core.space.model.Space) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) SearchResult(org.exoplatform.commons.api.search.data.SearchResult) Date(java.util.Date) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) ElasticSearchException(org.exoplatform.commons.search.es.ElasticSearchException)

Aggregations

ElasticSearchException (org.exoplatform.commons.search.es.ElasticSearchException)6 JSONObject (org.json.simple.JSONObject)6 JSONParser (org.json.simple.parser.JSONParser)6 ParseException (org.json.simple.parser.ParseException)6 JSONArray (org.json.simple.JSONArray)5 ArrayList (java.util.ArrayList)4 Identity (org.exoplatform.social.core.identity.model.Identity)4 Map (java.util.Map)3 HashMap (java.util.HashMap)2 SearchResult (org.exoplatform.commons.api.search.data.SearchResult)2 Profile (org.exoplatform.social.core.identity.model.Profile)2 Date (java.util.Date)1 List (java.util.List)1 ActivitySearchResult (org.exoplatform.social.core.activity.model.ActivitySearchResult)1 Space (org.exoplatform.social.core.space.model.Space)1