コード例 #1
0
  @Override
  public RecommendResponse recommend(RecommendQuery query, Integer maxReuslts) {
    productsToFilter = new ArrayList<String>();

    String queryString = "*:*";
    String sortCriteria = "user_count_purchased desc";
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    QueryResponse response = null;
    RecommendResponse searchResponse = new RecommendResponse();

    try {
      String filterQueryString =
          RecommendationQueryUtils.buildFilterForContentBasedFiltering(contentFilter);

      if (query.getProductIds() != null && query.getProductIds().size() > 0) {
        productsToFilter.addAll(query.getProductIds());
      }
      if (query.getUser() != null) {
        if (alreadyBoughtProducts != null) {
          productsToFilter.addAll(alreadyBoughtProducts);
        }
      }

      if (productsToFilter != null && productsToFilter.size() > 0) {
        if (filterQueryString.length() > 0) {
          filterQueryString += " OR ";
        }
        filterQueryString +=
            RecommendationQueryUtils.buildFilterForAlreadyBoughtProducts(productsToFilter);
      }
      solrParams.set("q", queryString);
      solrParams.set("fq", filterQueryString);
      solrParams.set("sort", sortCriteria);
      solrParams.set("rows", maxReuslts);

      response =
          SolrServiceContainer.getInstance()
              .getRecommendService()
              .getSolrServer()
              .query(solrParams);
      // fill response object
      List<String> extractedRecommendations =
          RecommendationQueryUtils.extractRecommendationIds(
              response.getBeans(CustomerAction.class));
      searchResponse.setResultItems(extractedRecommendations);
      searchResponse.setElapsedTime(response.getElapsedTime());
      SolrDocumentList docResults = response.getResults();
      searchResponse.setNumFound(docResults.getNumFound());
    } catch (SolrServerException e) {
      e.printStackTrace();
      searchResponse.setNumFound(0);
      searchResponse.setResultItems(new ArrayList<String>());
      searchResponse.setElapsedTime(-1);
    }

    return searchResponse;
  }
コード例 #2
0
  @Override
  public RecommendResponse recommend(RecommendQuery query, Integer maxReuslts) {
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    QueryResponse response = null;
    RecommendResponse searchResponse = new RecommendResponse();

    long step0ElapsedTime = 0;
    long step1ElapsedTime;
    List<String> recommendations = new ArrayList<String>();

    try {
      // STEP 0 - get products from a user
      if (query.getUser() != null) {
        if (query.getProductIds() == null || query.getProductIds().size() == 0) {
          if (alreadyBoughtProducts != null) {
            query.setProductIds(alreadyBoughtProducts);
          } else {
          }
        }
      }

      solrParams = getInteractionsFromMeAndUSersThatILikedOrCommented(query.getUser());

      response =
          SolrServiceContainer.getInstance()
              .getSocialActionService()
              .getSolrServer()
              .query(solrParams);
      step1ElapsedTime = response.getElapsedTime();

      List<SocialAction> socialUsers = response.getBeans(SocialAction.class);

      if (socialUsers.size() == 0) {
        searchResponse.setNumFound(0);
        searchResponse.setResultItems(recommendations);
        searchResponse.setElapsedTime(-1);
        return searchResponse;
      }

      SocialAction currentUserInteractions = socialUsers.get(0);
      if (currentUserInteractions.getUserId().equals(query.getUser())) {
        socialUsers.remove(0);
      } else {
        currentUserInteractions = null;
      }

      final Map<String, Integer> userInteractionMap = new HashMap<String, Integer>();

      if (currentUserInteractions != null) {
        List<String> usersThatPostedASnapshopToMe =
            currentUserInteractions.getUsersThatPostedASnapshopToMe();

        if (usersThatPostedASnapshopToMe != null) {
          fillInteractions(usersThatPostedASnapshopToMe, userInteractionMap);
        }
      }

      for (SocialAction socialUser : socialUsers) {
        Integer userInteraction = userInteractionMap.get(socialUser.getUserId());
        if (userInteraction == null) {
          userInteraction = 0;
        }

        if (socialUser.getUsersThatPostedASnapshopToMe() != null) {
          userInteraction +=
              Collections.frequency(socialUser.getUsersThatPostedASnapshopToMe(), query.getUser());
        }

        userInteractionMap.put(socialUser.getUserId(), userInteraction);
      }

      Comparator<String> interactionCountComparator =
          new Comparator<String>() {

            @Override
            public int compare(String a, String b) {
              if (userInteractionMap.get(a) > userInteractionMap.get(b)) {
                return -1;
              } else if (userInteractionMap.get(a).equals(userInteractionMap.get(b))) {
                return 0;
              } else {
                return 1;
              }
            }
          };

      TreeMap<String, Integer> sorted_map =
          new TreeMap<String, Integer>(interactionCountComparator);
      sorted_map.putAll(userInteractionMap);
      solrParams = getSTEP2Params(query, maxReuslts, sorted_map);
      // TODO Facet for confidence value
      response =
          SolrServiceContainer.getInstance().getResourceService().getSolrServer().query(solrParams);
      // fill response object
      List<Resource> beans = response.getBeans(Resource.class);
      searchResponse.setResultItems(RecommendationQueryUtils.extractRecommendationIds(beans));
      searchResponse.setElapsedTime(
          step0ElapsedTime + step1ElapsedTime + response.getElapsedTime());

      SolrDocumentList docResults = response.getResults();
      searchResponse.setNumFound(docResults.getNumFound());
    } catch (Exception e) {
      System.out.println(solrParams);
      e.printStackTrace();
      searchResponse.setNumFound(0);
      searchResponse.setResultItems(recommendations);
      searchResponse.setElapsedTime(-1);
    }

    return searchResponse;
  }