Beispiel #1
0
  @Test
  public void createNewSortObject() {

    String valueExpression = sort.getByValueExpression();
    SortBy.Order sortOrder = sort.getSortOrder();

    assertThat(valueExpression, is(byValueExpression));
    assertThat(sortOrder, is(bySortOrder));
  }
 @Override
 public List<Entry<T, Double>> getMostSimilarItems(
     T item, Collection<T> items, Collection<Property> properties, double treshold) {
   List<T> itemSet = new ArrayList<>(items);
   //	itemSet.remove(item);
   double similarity;
   Map<T, Double> map = new HashMap<T, Double>();
   for (T it : itemSet) {
     similarity = calculateSimiliraty(item, it, properties);
     if (similarity >= treshold) map.put(it, similarity);
   }
   return SortBy.sortByValue(map);
 }
  /** Parse the query string. */
  public ZimbraQuery(OperationContext octxt, SoapProtocol proto, Mailbox mbox, SearchParams params)
      throws ServiceException {
    this.octxt = octxt;
    this.protocol = proto;
    this.params = params;
    this.mailbox = mbox;

    // Parse the text using the JavaCC parser.
    try {
      QueryParser parser = new QueryParser(mbox, mbox.index.getAnalyzer());
      parser.setDefaultField(params.getDefaultField());
      parser.setTypes(params.getTypes());
      parser.setTimeZone(params.getTimeZone());
      parser.setLocale(params.getLocale());
      parser.setQuick(params.isQuick());
      clauses = parser.parse(params.getQueryString());

      if (parser.getSortBy() != null) {
        SortBy sort = SortBy.of(parser.getSortBy());
        if (sort == null) {
          throw ServiceException.PARSE_ERROR("INVALID_SORTBY: " + sort, null);
        }
        params.setSortBy(sort);
      }
    } catch (Error e) {
      throw ServiceException.PARSE_ERROR("PARSER_ERROR", e);
    }

    ZimbraLog.search.debug("%s,types=%s,sort=%s", this, params.getTypes(), params.getSortBy());

    // Build a parse tree and push all the "NOT's" down to the bottom level.
    // This is because we cannot invert result sets.
    parseTree = ParseTree.build(clauses).simplify();
    parseTree.pushNotsDown();

    // Check sort compatibility.
    switch (params.getSortBy().getKey()) {
      case RCPT:
      case ATTACHMENT:
      case FLAG:
      case PRIORITY:
        // We don't store these in Lucene.
        if (hasTextOperation()) {
          throw ServiceException.INVALID_REQUEST(
              "Sort '" + params.getSortBy().name() + "' can't be used with text query.", null);
        }
        break;
      default:
        break;
    }

    SearchParams.Cursor cursor = params.getCursor();
    if (cursor != null) {
      // Check cursor compatibility
      if (params.getCursor().isIncludeOffset() && hasTextOperation()) {
        throw ServiceException.INVALID_REQUEST(
            "cursor.includeOffset can't be used with text query.", null);
      }
      // Supplement sortValue
      if (cursor.getSortValue() == null) {
        ZimbraLog.search.debug(
            "Supplementing sortValue sort=%s,id=%s", params.getSortBy(), cursor.getItemId());
        try {
          MailItem item =
              mailbox.getItemById(null, cursor.getItemId().getId(), MailItem.Type.UNKNOWN);
          switch (params.getSortBy().getKey()) {
            case NAME:
              cursor.setSortValue(item.getName());
              break;
            case RCPT:
              cursor.setSortValue(item.getSortRecipients());
              break;
            case SENDER:
              cursor.setSortValue(item.getSortSender());
              break;
            case SIZE:
              cursor.setSortValue(String.valueOf(item.getSize()));
              break;
            case SUBJECT:
              cursor.setSortValue(item.getSortSubject());
              break;
            case DATE:
            default:
              cursor.setSortValue(String.valueOf(item.getDate()));
              break;
          }
        } catch (NoSuchItemException e) {
          params.setCursor(null); // clear cursor
        }
      }
    }
  }
 /**
  * Specifies the sorting strategy of the results
  *
  * @param sortBy - {@link com.indix.query.ApiParameters.SortBy}
  */
 public SearchQuery withSortBy(SortBy sortBy) {
   parameters.add(new BasicNameValuePair("sortBy", sortBy.name()));
   return this;
 }
  private void bufferAllHits() throws ServiceException {
    assert (mHitBuffer == null);
    mHitBuffer = new ArrayList<ZimbraHit>();

    // get the proper comparator
    Comparator<ZimbraHit> comp;
    switch (sort) {
      default:
      case TASK_DUE_ASC:
        comp =
            new Comparator<ZimbraHit>() {
              @Override
              public int compare(ZimbraHit lhs, ZimbraHit rhs) {
                return TaskHit.compareByDueDate(true, lhs, rhs);
              }
            };
        break;
      case TASK_DUE_DESC:
        comp =
            new Comparator<ZimbraHit>() {
              @Override
              public int compare(ZimbraHit lhs, ZimbraHit rhs) {
                return TaskHit.compareByDueDate(false, lhs, rhs);
              }
            };
        break;
      case TASK_STATUS_ASC:
        comp =
            new Comparator<ZimbraHit>() {
              @Override
              public int compare(ZimbraHit lhs, ZimbraHit rhs) {
                return TaskHit.compareByStatus(true, lhs, rhs);
              }
            };
        break;
      case TASK_STATUS_DESC:
        comp =
            new Comparator<ZimbraHit>() {
              @Override
              public int compare(ZimbraHit lhs, ZimbraHit rhs) {
                return TaskHit.compareByStatus(false, lhs, rhs);
              }
            };
        break;
      case TASK_PERCENT_COMPLETE_ASC:
        comp =
            new Comparator<ZimbraHit>() {
              @Override
              public int compare(ZimbraHit lhs, ZimbraHit rhs) {
                return TaskHit.compareByCompletionPercent(true, lhs, rhs);
              }
            };
        break;
      case TASK_PERCENT_COMPLETE_DESC:
        comp =
            new Comparator<ZimbraHit>() {
              @Override
              public int compare(ZimbraHit lhs, ZimbraHit rhs) {
                return TaskHit.compareByCompletionPercent(false, lhs, rhs);
              }
            };
        break;
      case NAME_LOCALIZED_ASC:
      case NAME_LOCALIZED_DESC:
        comp = sort.getHitComparator(params.getLocale());
        break;
    }

    int maxIfPresorted = MAX_BUFFERED_HITS;
    if (params != null && params.getCursor() == null) {
      maxIfPresorted = params.getLimit();
      if (maxIfPresorted > 0) {
        // 1 is added so that the 'more' setting will be correct.
        maxIfPresorted = maxIfPresorted + 1 + params.getOffset();
      }
    }
    ZimbraHit cur;
    while ((cur = results.getNext()) != null) {

      if (isTaskSort()) {
        if (!(cur instanceof TaskHit) && !(cur instanceof ProxiedHit)) {
          throw ServiceException.FAILURE("Invalid hit type, can only task-sort Tasks", null);
        }
      }

      boolean skipHit = false;

      boolean handleCursorFilteringForFirstHit = true;
      if (DebugConfig.enableContactLocalizedSort) {
        switch (sort) {
          case NAME_LOCALIZED_ASC:
          case NAME_LOCALIZED_DESC:
            handleCursorFilteringForFirstHit = false;
            break;
        }
      }

      // handle cursor filtering
      if (params != null && params.getCursor() != null) {
        ZimbraHit firstHit = null;
        if (params.getCursor().getSortValue() != null) {
          firstHit =
              new ResultsPager.CursorHit(
                  results,
                  params.getCursor().getSortValue(),
                  params.getCursor().getItemId().getId());
        }
        ZimbraHit endHit = null;
        if (params.getCursor().getEndSortValue() != null) {
          endHit = new ResultsPager.CursorHit(results, params.getCursor().getEndSortValue(), 0);
        }
        // fail if cur < first OR cur >= end
        if (handleCursorFilteringForFirstHit) {
          if (firstHit != null && comp.compare(cur, firstHit) < 0) {
            skipHit = true;
          }
        }
        if (endHit != null && comp.compare(cur, endHit) >= 0) {
          skipHit = true;
        }
      }

      if (!skipHit) {
        mHitBuffer.add(cur);
      }
      if (mHitBuffer.size() >= MAX_BUFFERED_HITS) {
        break;
      }
      // If it turns out that the results were sorted remotely, we can bail out early.
      if (results.isPreSorted() && mHitBuffer.size() >= maxIfPresorted) {
        break;
      }
    }

    if (!results.isPreSorted()) {
      Collections.sort(mHitBuffer, comp);
    }
  }