public boolean match(final Sort expected, final Sort acutal) {
      if (expected == acutal) {
        return true;
      } else if ((expected == null) || (acutal == null)) {
        return false;
      } else {
        final SortField[] expectedFields = expected.getSort();
        final SortField[] actualFields = acutal.getSort();
        if (expectedFields.length != actualFields.length) {
          return false;
        }

        final ArgumentMatcher<String> matcher = ArgumentMatchers.naturalMatcher();

        for (int i = 0; i < actualFields.length; i++) {
          final SortField actualField = actualFields[i];
          final SortField expectedField = expectedFields[i];
          if (!matcher.match(expectedField.getField(), actualField.getField())) {
            return false;
          }
          if (actualField.getType() != expectedField.getType()) {
            return false;
          }

          if (expectedField.getReverse() != actualField.getReverse()) {
            return false;
          }
        }
        return true;
      }
    }
 @Override
 public void parse(XContentParser parser, SearchContext context) throws Exception {
   XContentParser.Token token = parser.currentToken();
   List<SortField> sortFields = Lists.newArrayListWithCapacity(2);
   if (token == XContentParser.Token.START_ARRAY) {
     while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
       if (token == XContentParser.Token.START_OBJECT) {
         addCompoundSortField(parser, context, sortFields);
       } else if (token == XContentParser.Token.VALUE_STRING) {
         addSortField(context, sortFields, parser.text(), false, false, null, null, null, null);
       }
     }
   } else {
     addCompoundSortField(parser, context, sortFields);
   }
   if (!sortFields.isEmpty()) {
     // optimize if we just sort on score non reversed, we don't really need sorting
     boolean sort;
     if (sortFields.size() > 1) {
       sort = true;
     } else {
       SortField sortField = sortFields.get(0);
       if (sortField.getType() == SortField.Type.SCORE && !sortField.getReverse()) {
         sort = false;
       } else {
         sort = true;
       }
     }
     if (sort) {
       context.sort(new Sort(sortFields.toArray(new SortField[sortFields.size()])));
     }
   }
 }
  private SortSpec modifySortSpec(
      SortSpec current, boolean force, ElevationComparatorSource comparator) {
    boolean modify = false;
    SortField[] currentSorts = current.getSort().getSort();
    List<SchemaField> currentFields = current.getSchemaFields();

    ArrayList<SortField> sorts = new ArrayList<SortField>(currentSorts.length + 1);
    List<SchemaField> fields = new ArrayList<SchemaField>(currentFields.size() + 1);

    // Perhaps force it to always sort by score
    if (force && currentSorts[0].getType() != SortField.Type.SCORE) {
      sorts.add(new SortField("_elevate_", comparator, true));
      fields.add(null);
      modify = true;
    }
    for (int i = 0; i < currentSorts.length; i++) {
      SortField sf = currentSorts[i];
      if (sf.getType() == SortField.Type.SCORE) {
        sorts.add(new SortField("_elevate_", comparator, !sf.getReverse()));
        fields.add(null);
        modify = true;
      }
      sorts.add(sf);
      fields.add(currentFields.get(i));
    }
    if (modify) {
      SortSpec newSpec = new SortSpec(new Sort(sorts.toArray(new SortField[sorts.size()])), fields);
      newSpec.setOffset(current.getOffset());
      newSpec.setCount(current.getCount());
      return newSpec;
    }
    return null;
  }
예제 #4
0
 private static DocComparatorSource getComparatorSource(Browsable browser, SortField sf) {
   DocComparatorSource compSource = null;
   if (SortField.FIELD_DOC.equals(sf)) {
     compSource = new DocIdDocComparatorSource();
   } else if (SortField.FIELD_SCORE.equals(sf) || sf.getType() == SortField.SCORE) {
     // we want to do reverse sorting regardless for relevance
     compSource = new ReverseDocComparatorSource(new RelevanceDocComparatorSource());
   } else if (sf instanceof BoboCustomSortField) {
     BoboCustomSortField custField = (BoboCustomSortField) sf;
     DocComparatorSource src = custField.getCustomComparatorSource();
     assert src != null;
     compSource = src;
   } else {
     Set<String> facetNames = browser.getFacetNames();
     String sortName = sf.getField();
     if (facetNames.contains(sortName)) {
       FacetHandler<?> handler = browser.getFacetHandler(sortName);
       assert handler != null;
       compSource = handler.getDocComparatorSource();
     } else { // default lucene field
       logger.info("doing default lucene sort for: " + sf);
       compSource = getNonFacetComparatorSource(sf);
     }
   }
   boolean reverse = sf.getReverse();
   if (reverse) {
     compSource = new ReverseDocComparatorSource(compSource);
   }
   compSource.setReverse(reverse);
   return compSource;
 }
 @Override
 protected void sortFieldAssertions(
     GeoDistanceSortBuilder builder, SortField sortField, DocValueFormat format)
     throws IOException {
   assertEquals(SortField.Type.CUSTOM, sortField.getType());
   assertEquals(builder.order() == SortOrder.ASC ? false : true, sortField.getReverse());
   assertEquals(builder.fieldName(), sortField.getField());
 }
예제 #6
0
 private static SortField convert(Browsable browser, SortField sort) {
   String field = sort.getField();
   FacetHandler<?> facetHandler = browser.getFacetHandler(field);
   if (facetHandler != null) {
     browser.getFacetHandler(field);
     BoboCustomSortField sortField =
         new BoboCustomSortField(field, sort.getReverse(), facetHandler.getDocComparatorSource());
     return sortField;
   } else {
     return sort;
   }
 }
 @Override
 protected void sortFieldAssertions(
     FieldSortBuilder builder, SortField sortField, DocValueFormat format) throws IOException {
   SortField.Type expectedType;
   if (builder.getFieldName().equals(FieldSortBuilder.DOC_FIELD_NAME)) {
     expectedType = SortField.Type.DOC;
   } else {
     expectedType = SortField.Type.CUSTOM;
   }
   assertEquals(expectedType, sortField.getType());
   assertEquals(builder.order() == SortOrder.ASC ? false : true, sortField.getReverse());
   if (expectedType == SortField.Type.CUSTOM) {
     assertEquals(builder.getFieldName(), sortField.getField());
   }
   assertEquals(DocValueFormat.RAW, format);
 }
예제 #8
0
  public static void writeTopDocs(StreamOutput out, TopDocs topDocs, int from) throws IOException {
    if (topDocs.scoreDocs.length - from < 0) {
      out.writeBoolean(false);
      return;
    }
    out.writeBoolean(true);
    if (topDocs instanceof TopFieldDocs) {
      out.writeBoolean(true);
      TopFieldDocs topFieldDocs = (TopFieldDocs) topDocs;

      out.writeVInt(topDocs.totalHits);
      out.writeFloat(topDocs.getMaxScore());

      out.writeVInt(topFieldDocs.fields.length);
      for (SortField sortField : topFieldDocs.fields) {
        if (sortField.getField() == null) {
          out.writeBoolean(false);
        } else {
          out.writeBoolean(true);
          out.writeString(sortField.getField());
        }
        if (sortField.getComparatorSource() != null) {
          writeSortType(
              out,
              ((IndexFieldData.XFieldComparatorSource) sortField.getComparatorSource())
                  .reducedType());
        } else {
          writeSortType(out, sortField.getType());
        }
        out.writeBoolean(sortField.getReverse());
      }

      out.writeVInt(topDocs.scoreDocs.length - from);
      int index = 0;
      for (ScoreDoc doc : topFieldDocs.scoreDocs) {
        if (index++ < from) {
          continue;
        }
        FieldDoc fieldDoc = (FieldDoc) doc;
        out.writeVInt(fieldDoc.fields.length);
        for (Object field : fieldDoc.fields) {
          if (field == null) {
            out.writeByte((byte) 0);
          } else {
            Class type = field.getClass();
            if (type == String.class) {
              out.writeByte((byte) 1);
              out.writeString((String) field);
            } else if (type == Integer.class) {
              out.writeByte((byte) 2);
              out.writeInt((Integer) field);
            } else if (type == Long.class) {
              out.writeByte((byte) 3);
              out.writeLong((Long) field);
            } else if (type == Float.class) {
              out.writeByte((byte) 4);
              out.writeFloat((Float) field);
            } else if (type == Double.class) {
              out.writeByte((byte) 5);
              out.writeDouble((Double) field);
            } else if (type == Byte.class) {
              out.writeByte((byte) 6);
              out.writeByte((Byte) field);
            } else if (type == Short.class) {
              out.writeByte((byte) 7);
              out.writeShort((Short) field);
            } else if (type == Boolean.class) {
              out.writeByte((byte) 8);
              out.writeBoolean((Boolean) field);
            } else if (type == BytesRef.class) {
              out.writeByte((byte) 9);
              out.writeBytesRef((BytesRef) field);
            } else {
              throw new IOException("Can't handle sort field value of type [" + type + "]");
            }
          }
        }

        out.writeVInt(doc.doc);
        out.writeFloat(doc.score);
      }
    } else {
      out.writeBoolean(false);
      out.writeVInt(topDocs.totalHits);
      out.writeFloat(topDocs.getMaxScore());

      out.writeVInt(topDocs.scoreDocs.length - from);
      int index = 0;
      for (ScoreDoc doc : topDocs.scoreDocs) {
        if (index++ < from) {
          continue;
        }
        out.writeVInt(doc.doc);
        out.writeFloat(doc.score);
      }
    }
  }