List of usage examples for org.apache.lucene.search SortField getComparatorSource
public FieldComparatorSource getComparatorSource()
From source file:com.stratio.cassandra.lucene.service.TokenMapperGenericTest.java
License:Apache License
@Test @SuppressWarnings("unchecked") public void testSortFields() throws IOException { List<SortField> sortFields = mapper.sortFields(); assertNotNull("Sort fields should be not null", sortFields); assertEquals("Sort fields should contain a single element", 1, sortFields.size()); for (SortField sortField : sortFields) { FieldComparatorSource comparatorSource = sortField.getComparatorSource(); assertNotNull("Sort field comparator should be not null", comparatorSource); FieldComparator fieldComparator = comparatorSource.newComparator(TokenMapperGeneric.FIELD_NAME, 1, 0, true);//www . j a va 2s . c o m BytesRef value1 = mapper.bytesRef(token("k1")); BytesRef value2 = mapper.bytesRef(token("k2")); fieldComparator.compareValues(value1, value2); } }
From source file:org.apache.jackrabbit.core.query.lucene.Ordering.java
License:Apache License
/** * Creates an ordering from a JCR QOM ordering. * * @param ordering the JCR QOM ordering specification. * @param scs the sort comparator source from the search index. * @param nsMappings the index internal namespace mappings. * @return an ordering./*from w w w .jav a 2 s .co m*/ * @throws RepositoryException if an error occurs while translating the JCR * QOM ordering. */ public static Ordering fromQOM(final OrderingImpl ordering, final SharedFieldComparatorSource scs, final NamespaceMappings nsMappings) throws RepositoryException { final Name[] selectorName = new Name[1]; QOMTreeVisitor visitor = new DefaultTraversingQOMTreeVisitor() { public Object visit(LengthImpl node, Object data) throws Exception { PropertyValueImpl propValue = (PropertyValueImpl) node.getPropertyValue(); selectorName[0] = propValue.getSelectorQName(); return new SortField(propValue.getPropertyQName().toString(), new LengthSortComparator(nsMappings), !ordering.isAscending()); } public Object visit(LowerCaseImpl node, Object data) throws Exception { SortField sf = (SortField) ((DynamicOperandImpl) node.getOperand()).accept(this, data); selectorName[0] = node.getSelectorQName(); return new SortField(sf.getField(), new LowerCaseSortComparator(sf.getComparatorSource()), !ordering.isAscending()); } public Object visit(UpperCaseImpl node, Object data) throws Exception { SortField sf = (SortField) ((DynamicOperandImpl) node.getOperand()).accept(this, data); selectorName[0] = node.getSelectorQName(); return new SortField(sf.getField(), new UpperCaseSortComparator(sf.getComparatorSource()), !ordering.isAscending()); } public Object visit(FullTextSearchScoreImpl node, Object data) throws Exception { selectorName[0] = node.getSelectorQName(); return new SortField(null, SortField.SCORE, !ordering.isAscending()); } public Object visit(NodeLocalNameImpl node, Object data) throws Exception { selectorName[0] = node.getSelectorQName(); return new SortField(FieldNames.LOCAL_NAME, SortField.STRING, !ordering.isAscending()); } public Object visit(NodeNameImpl node, Object data) throws Exception { selectorName[0] = node.getSelectorQName(); return new SortField(FieldNames.LABEL, SortField.STRING, !ordering.isAscending()); } public Object visit(PropertyValueImpl node, Object data) throws Exception { selectorName[0] = node.getSelectorQName(); return new SortField(node.getPropertyQName().toString(), scs, !ordering.isAscending()); } public Object visit(OrderingImpl node, Object data) throws Exception { return ((DynamicOperandImpl) node.getOperand()).accept(this, data); } }; try { SortField field = (SortField) ordering.accept(visitor, null); return new Ordering(selectorName[0], field); } catch (Exception e) { throw new RepositoryException(e); } }
From source file:org.codelibs.elasticsearch.common.lucene.Lucene.java
License:Apache License
public static void writeTopDocs(StreamOutput out, TopDocs topDocs) throws IOException { if (topDocs instanceof TopFieldDocs) { out.writeBoolean(true);/*from w ww . ja v a 2s . c o m*/ TopFieldDocs topFieldDocs = (TopFieldDocs) topDocs; out.writeVInt(topDocs.totalHits); out.writeFloat(topDocs.getMaxScore()); out.writeVInt(topFieldDocs.fields.length); for (SortField sortField : topFieldDocs.fields) { if (sortField.getClass() == GEO_DISTANCE_SORT_TYPE_CLASS) { // for geo sorting, we replace the SortField with a SortField that assumes a double field. // this works since the SortField is only used for merging top docs SortField newSortField = new SortField(sortField.getField(), SortField.Type.DOUBLE); newSortField.setMissingValue(sortField.getMissingValue()); sortField = newSortField; } if (sortField.getClass() != SortField.class) { throw new IllegalArgumentException("Cannot serialize SortField impl [" + sortField + "]"); } if (sortField.getField() == null) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeString(sortField.getField()); } if (sortField.getComparatorSource() != null) { IndexFieldData.XFieldComparatorSource comparatorSource = (IndexFieldData.XFieldComparatorSource) sortField .getComparatorSource(); writeSortType(out, comparatorSource.reducedType()); writeMissingValue(out, comparatorSource.missingValue(sortField.getReverse())); } else { writeSortType(out, sortField.getType()); writeMissingValue(out, sortField.getMissingValue()); } out.writeBoolean(sortField.getReverse()); } out.writeVInt(topDocs.scoreDocs.length); for (ScoreDoc doc : topFieldDocs.scoreDocs) { writeFieldDoc(out, (FieldDoc) doc); } } else { out.writeBoolean(false); out.writeVInt(topDocs.totalHits); out.writeFloat(topDocs.getMaxScore()); out.writeVInt(topDocs.scoreDocs.length); for (ScoreDoc doc : topDocs.scoreDocs) { writeScoreDoc(out, doc); } } }
From source file:org.codelibs.elasticsearch.search.searchafter.SearchAfterBuilder.java
License:Apache License
private static Object convertValueFromSortField(Object value, SortField sortField, DocValueFormat format) { if (sortField.getComparatorSource() instanceof IndexFieldData.XFieldComparatorSource) { IndexFieldData.XFieldComparatorSource cmpSource = (IndexFieldData.XFieldComparatorSource) sortField .getComparatorSource();// www .ja v a2 s .c o m return convertValueFromSortType(sortField.getField(), cmpSource.reducedType(), value, format); } return convertValueFromSortType(sortField.getField(), sortField.getType(), value, format); }
From source file:org.elasticsearch.common.lucene.Lucene.java
License:Apache License
public static void writeTopDocs(StreamOutput out, TopDocs topDocs, int from) throws IOException { if (topDocs.scoreDocs.length - from < 0) { out.writeBoolean(false);/*from w ww. j a va 2 s .c om*/ 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); } } }