Example usage for org.apache.lucene.search Sort Sort

List of usage examples for org.apache.lucene.search Sort Sort

Introduction

In this page you can find the example usage for org.apache.lucene.search Sort Sort.

Prototype

public Sort(SortField... fields) 

Source Link

Document

Sets the sort to the given criteria in succession: the first SortField is checked first, but if it produces a tie, then the second SortField is used to break the tie, etc.

Usage

From source file:org.elasticsearch.test.unit.index.fielddata.StringFieldDataTests.java

License:Apache License

@Test
public void testMultiValueAllSet() throws Exception {
    fillMultiValueAllSet();//from   ww  w.  j a  v a 2s .c o  m
    IndexFieldData indexFieldData = getForField("value");
    AtomicFieldData fieldData = indexFieldData.load(refreshReader());

    assertThat(fieldData.getNumDocs(), equalTo(3));

    BytesValues bytesValues = fieldData.getBytesValues();

    assertThat(bytesValues.isMultiValued(), equalTo(true));

    assertThat(bytesValues.hasValue(0), equalTo(true));
    assertThat(bytesValues.hasValue(1), equalTo(true));
    assertThat(bytesValues.hasValue(2), equalTo(true));

    assertThat(bytesValues.getValue(0), equalTo(new BytesRef(two())));
    assertThat(bytesValues.getValue(1), equalTo(new BytesRef(one())));
    assertThat(bytesValues.getValue(2), equalTo(new BytesRef(three())));

    BytesRef bytesRef = new BytesRef();
    assertThat(bytesValues.getValueScratch(0, bytesRef), equalTo(new BytesRef(two())));
    assertThat(bytesRef, equalTo(new BytesRef(two())));
    assertThat(bytesValues.getValueScratch(1, bytesRef), equalTo(new BytesRef(one())));
    assertThat(bytesRef, equalTo(new BytesRef(one())));
    assertThat(bytesValues.getValueScratch(2, bytesRef), equalTo(new BytesRef(three())));
    assertThat(bytesRef, equalTo(new BytesRef(three())));

    BytesRefArrayRef bytesRefArrayRef = bytesValues.getValues(0);
    assertThat(bytesRefArrayRef.size(), equalTo(2));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start], equalTo(new BytesRef(two())));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start + 1], equalTo(new BytesRef(four())));

    bytesRefArrayRef = bytesValues.getValues(1);
    assertThat(bytesRefArrayRef.size(), equalTo(1));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start], equalTo(new BytesRef(one())));

    bytesRefArrayRef = bytesValues.getValues(2);
    assertThat(bytesRefArrayRef.size(), equalTo(1));
    assertThat(bytesRefArrayRef.values[bytesRefArrayRef.start], equalTo(new BytesRef(three())));

    BytesValues.Iter bytesValuesIter = bytesValues.getIter(0);
    assertThat(bytesValuesIter.hasNext(), equalTo(true));
    assertThat(bytesValuesIter.next(), equalTo(new BytesRef(two())));
    assertThat(bytesValuesIter.hasNext(), equalTo(true));
    assertThat(bytesValuesIter.next(), equalTo(new BytesRef(four())));
    assertThat(bytesValuesIter.hasNext(), equalTo(false));

    bytesValues.forEachValueInDoc(0, new BytesValuesVerifierProc(0).addExpected(two()).addExpected(four()));
    bytesValues.forEachValueInDoc(1, new BytesValuesVerifierProc(1).addExpected(one()));
    bytesValues.forEachValueInDoc(2, new BytesValuesVerifierProc(2).addExpected(three()));

    HashedBytesValues hashedBytesValues = fieldData.getHashedBytesValues();

    assertThat(hashedBytesValues.hasValue(0), equalTo(true));
    assertThat(hashedBytesValues.hasValue(1), equalTo(true));
    assertThat(hashedBytesValues.hasValue(2), equalTo(true));

    assertThat(hashedBytesValues.getValue(0), equalTo(new HashedBytesRef(two())));
    assertThat(hashedBytesValues.getValue(1), equalTo(new HashedBytesRef(one())));
    assertThat(hashedBytesValues.getValue(2), equalTo(new HashedBytesRef(three())));

    HashedBytesValues.Iter hashedBytesValuesIter = hashedBytesValues.getIter(0);
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(true));
    assertThat(hashedBytesValuesIter.next(), equalTo(new HashedBytesRef(two())));
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(true));
    assertThat(hashedBytesValuesIter.next(), equalTo(new HashedBytesRef(four())));
    assertThat(hashedBytesValuesIter.hasNext(), equalTo(false));

    hashedBytesValues.forEachValueInDoc(0,
            new HashedBytesValuesVerifierProc(0).addExpected(two()).addExpected(four()));
    hashedBytesValues.forEachValueInDoc(1, new HashedBytesValuesVerifierProc(1).addExpected(one()));
    hashedBytesValues.forEachValueInDoc(2, new HashedBytesValuesVerifierProc(2).addExpected(three()));

    StringValues stringValues = fieldData.getStringValues();

    assertThat(stringValues.hasValue(0), equalTo(true));
    assertThat(stringValues.hasValue(1), equalTo(true));
    assertThat(stringValues.hasValue(2), equalTo(true));

    assertThat(stringValues.getValue(0), equalTo(two()));
    assertThat(stringValues.getValue(1), equalTo(one()));
    assertThat(stringValues.getValue(2), equalTo(three()));

    StringArrayRef stringArrayRef;
    stringArrayRef = stringValues.getValues(0);
    assertThat(stringArrayRef.size(), equalTo(2));
    assertThat(stringArrayRef.values[stringArrayRef.start], equalTo(two()));
    assertThat(stringArrayRef.values[stringArrayRef.start + 1], equalTo(four()));

    stringArrayRef = stringValues.getValues(1);
    assertThat(stringArrayRef.size(), equalTo(1));
    assertThat(stringArrayRef.values[stringArrayRef.start], equalTo(one()));

    stringArrayRef = stringValues.getValues(2);
    assertThat(stringArrayRef.size(), equalTo(1));
    assertThat(stringArrayRef.values[stringArrayRef.start], equalTo(three()));

    StringValues.Iter stringValuesIter = stringValues.getIter(0);
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(two()));
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(four()));
    assertThat(stringValuesIter.hasNext(), equalTo(false));

    stringValuesIter = stringValues.getIter(1);
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(one()));
    assertThat(stringValuesIter.hasNext(), equalTo(false));

    stringValuesIter = stringValues.getIter(2);
    assertThat(stringValuesIter.hasNext(), equalTo(true));
    assertThat(stringValuesIter.next(), equalTo(three()));
    assertThat(stringValuesIter.hasNext(), equalTo(false));

    stringValues.forEachValueInDoc(0, new StringValuesVerifierProc(0).addExpected(two()).addExpected(four()));
    stringValues.forEachValueInDoc(1, new StringValuesVerifierProc(1).addExpected(one()));
    stringValues.forEachValueInDoc(2, new StringValuesVerifierProc(2).addExpected(three()));

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, true));
    TopFieldDocs topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MIN))));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs.length, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(1));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(2));

    topDocs = searcher.search(new MatchAllDocsQuery(), 10,
            new Sort(new SortField("value", indexFieldData.comparatorSource(null, SortMode.MAX), true)));
    assertThat(topDocs.totalHits, equalTo(3));
    assertThat(topDocs.scoreDocs.length, equalTo(3));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(1));
}

From source file:org.elasticsearch.test.unit.index.search.nested.AbstractNumberNestedSortingTests.java

License:Apache License

@Test
public void testNestedSorting() throws Exception {
    List<Document> docs = new ArrayList<Document>();
    Document document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);//w ww . j a  v  a 2 s.  co  m
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 1, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();

    docs.clear();
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 2, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 2, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    docs.clear();
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 1, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 3, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    docs.clear();
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 4, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 4, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();

    docs.clear();
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 5, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 5, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    docs.clear();
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 6, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 6, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    // This doc will not be included, because it doesn't have nested docs
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 7, Field.Store.NO));
    writer.addDocument(document);

    docs.clear();
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 3, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(createField("field2", 7, Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(createField("field1", 8, Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();

    // Some garbage docs, just to check if the NestedFieldComparator can deal with this.
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);

    SortMode sortMode = SortMode.SUM;
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    IndexFieldData.XFieldComparatorSource innerFieldComparator = createInnerFieldComparator("field2", sortMode);
    Filter parentFilter = new TermFilter(new Term("__type", "parent"));
    Filter childFilter = new NotFilter(parentFilter);
    NestedFieldComparatorSource nestedComparatorSource = new NestedFieldComparatorSource(sortMode,
            innerFieldComparator, parentFilter, childFilter);
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(
            new XFilteredQuery(new MatchAllDocsQuery(), childFilter), new CachingWrapperFilter(parentFilter),
            ScoreMode.None);

    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(11));

    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(13));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(11));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(10));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9));

    childFilter = new AndFilter(
            Arrays.asList(new NotFilter(parentFilter), new TermFilter(new Term("filter_1", "T"))));
    nestedComparatorSource = new NestedFieldComparatorSource(sortMode, innerFieldComparator, parentFilter,
            childFilter);
    query = new ToParentBlockJoinQuery(new XFilteredQuery(new MatchAllDocsQuery(), childFilter),
            new CachingWrapperFilter(parentFilter), ScoreMode.None);
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(6));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(23));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(12));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(9));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));

    sort = new Sort(new SortField("field2", nestedComparatorSource));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(6));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(28));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(7));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(8));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(9));

    // Moved to method, because floating point based XFieldComparatorSource have different outcome for SortMode avg,
    // than integral number based implementations...
    assertAvgScoreMode(parentFilter, searcher, innerFieldComparator);
    searcher.getIndexReader().close();
}

From source file:org.elasticsearch.test.unit.index.search.nested.AbstractNumberNestedSortingTests.java

License:Apache License

protected void assertAvgScoreMode(Filter parentFilter, IndexSearcher searcher,
        IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException {
    SortMode sortMode = SortMode.AVG;/*from   www  . ja v  a 2s  .  c  o m*/
    Filter childFilter = new NotFilter(parentFilter);
    NestedFieldComparatorSource nestedComparatorSource = new NestedFieldComparatorSource(sortMode,
            innerFieldComparator, parentFilter, childFilter);
    Query query = new ToParentBlockJoinQuery(new XFilteredQuery(new MatchAllDocsQuery(), childFilter),
            new CachingWrapperFilter(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}

From source file:org.elasticsearch.test.unit.index.search.nested.DoubleNestedSortingTests.java

License:Apache License

protected void assertAvgScoreMode(Filter parentFilter, IndexSearcher searcher,
        IndexFieldData.XFieldComparatorSource innerFieldComparator) throws IOException {
    SortMode sortMode = SortMode.AVG;/* w w  w.j  a v  a  2  s.co  m*/
    Filter childFilter = new NotFilter(parentFilter);
    NestedFieldComparatorSource nestedComparatorSource = new NestedFieldComparatorSource(sortMode,
            innerFieldComparator, parentFilter, childFilter);
    Query query = new ToParentBlockJoinQuery(new XFilteredQuery(new MatchAllDocsQuery(), childFilter),
            new CachingWrapperFilter(parentFilter), ScoreMode.None);
    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(11));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).intValue(), equalTo(2));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(3));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).intValue(), equalTo(3));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((Number) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).intValue(), equalTo(3));
}

From source file:org.elasticsearch.test.unit.index.search.nested.NestedSortingTests.java

License:Apache License

@Test
public void testNestedSorting() throws Exception {
    List<Document> docs = new ArrayList<Document>();
    Document document = new Document();
    document.add(new StringField("field2", "a", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);//  w w w. java 2s. com
    document = new Document();
    document.add(new StringField("field2", "b", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "c", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "a", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();

    docs.clear();
    document = new Document();
    document.add(new StringField("field2", "c", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "d", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "e", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "b", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    docs.clear();
    document = new Document();
    document.add(new StringField("field2", "e", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "f", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "g", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "c", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    docs.clear();
    document = new Document();
    document.add(new StringField("field2", "g", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "h", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "i", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "d", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();

    docs.clear();
    document = new Document();
    document.add(new StringField("field2", "i", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "j", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "k", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "f", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    docs.clear();
    document = new Document();
    document.add(new StringField("field2", "k", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "l", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "m", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "g", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);

    // This doc will not be included, because it doesn't have nested docs
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "h", Field.Store.NO));
    writer.addDocument(document);

    docs.clear();
    document = new Document();
    document.add(new StringField("field2", "m", Field.Store.NO));
    document.add(new StringField("filter_1", "T", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "n", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("field2", "o", Field.Store.NO));
    document.add(new StringField("filter_1", "F", Field.Store.NO));
    docs.add(document);
    document = new Document();
    document.add(new StringField("__type", "parent", Field.Store.NO));
    document.add(new StringField("field1", "i", Field.Store.NO));
    docs.add(document);
    writer.addDocuments(docs);
    writer.commit();

    // Some garbage docs, just to check if the NestedFieldComparator can deal with this.
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);
    document = new Document();
    document.add(new StringField("fieldXXX", "x", Field.Store.NO));
    writer.addDocument(document);

    SortMode sortMode = SortMode.MIN;
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(writer, false));
    PagedBytesIndexFieldData indexFieldData = getForField("field2");
    BytesRefFieldComparatorSource innerSource = new BytesRefFieldComparatorSource(indexFieldData, sortMode);
    Filter parentFilter = new TermFilter(new Term("__type", "parent"));
    Filter childFilter = new NotFilter(parentFilter);
    NestedFieldComparatorSource nestedComparatorSource = new NestedFieldComparatorSource(sortMode, innerSource,
            parentFilter, childFilter);
    ToParentBlockJoinQuery query = new ToParentBlockJoinQuery(
            new XFilteredQuery(new MatchAllDocsQuery(), childFilter), new CachingWrapperFilter(parentFilter),
            ScoreMode.None);

    Sort sort = new Sort(new SortField("field2", nestedComparatorSource));
    TopFieldDocs topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(3));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("a"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("c"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(11));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("e"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("g"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(19));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("i"));

    sortMode = SortMode.MAX;
    nestedComparatorSource = new NestedFieldComparatorSource(sortMode, innerSource, parentFilter, childFilter);
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(7));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(28));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("o"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(23));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("m"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(19));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("k"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("i"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(11));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("g"));

    childFilter = new AndFilter(
            Arrays.asList(new NotFilter(parentFilter), new TermFilter(new Term("filter_1", "T"))));
    nestedComparatorSource = new NestedFieldComparatorSource(sortMode, innerSource, parentFilter, childFilter);
    query = new ToParentBlockJoinQuery(new XFilteredQuery(new MatchAllDocsQuery(), childFilter),
            new CachingWrapperFilter(parentFilter), ScoreMode.None);
    sort = new Sort(new SortField("field2", nestedComparatorSource, true));
    topDocs = searcher.search(query, 5, sort);
    assertThat(topDocs.totalHits, equalTo(6));
    assertThat(topDocs.scoreDocs.length, equalTo(5));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(23));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[0]).fields[0]).utf8ToString(), equalTo("m"));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(28));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[1]).fields[0]).utf8ToString(), equalTo("m"));
    assertThat(topDocs.scoreDocs[2].doc, equalTo(11));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[2]).fields[0]).utf8ToString(), equalTo("g"));
    assertThat(topDocs.scoreDocs[3].doc, equalTo(15));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[3]).fields[0]).utf8ToString(), equalTo("g"));
    assertThat(topDocs.scoreDocs[4].doc, equalTo(7));
    assertThat(((BytesRef) ((FieldDoc) topDocs.scoreDocs[4]).fields[0]).utf8ToString(), equalTo("e"));

    searcher.getIndexReader().close();
}

From source file:org.eurekastreams.server.persistence.mappers.LuceneSearchMapper.java

License:Apache License

/**
 * Execute the mapper./*from ww w  .jav  a2 s . c om*/
 * 
 * @param inRequest
 *            the request.
 * @return the items.
 */
@SuppressWarnings("unchecked")
@Override
public PagedSet<T> execute(final LuceneSearchRequest inRequest) {
    /**
     * Convoluted like this to make testing possible.
     */
    FullTextEntityManager sessionEntityManager = fullTextEntityManager;

    if (sessionEntityManager == null) {
        sessionEntityManager = Search.getFullTextEntityManager(getEntityManager());
    }

    QueryParser parser = queryBuilder.buildQueryParser();

    List<T> results = null;

    PagedSet<T> pagedSet = null;

    try {
        StringBuilder query = new StringBuilder();

        // TODO Escape!!
        for (Entry<String, Float> entry : inRequest.getFields().entrySet()) {
            query.append(entry.getKey());
            query.append(":");
            query.append("(%1$s)^");
            query.append(Float.toString(entry.getValue()));
            query.append(" ");
        }

        String luceneQueryString = String.format(query.toString(), inRequest.getSearchString());
        org.apache.lucene.search.Query luceneQuery = parser.parse(luceneQueryString);

        FullTextQuery fullTextQuery = sessionEntityManager.createFullTextQuery(luceneQuery,
                inRequest.getObjectType());

        fullTextQuery.setFirstResult(inRequest.getFirstResult());
        fullTextQuery.setMaxResults(inRequest.getMaxResults());

        SortField[] fields = new SortField[inRequest.getSortFields().size()];

        for (int i = 0; i < inRequest.getSortFields().size(); i++) {
            fields[i] = new SortField(inRequest.getSortFields().get(i), true);
        }

        Sort sort = new Sort(fields);

        fullTextQuery.setSort(sort);

        results = fullTextQuery.getResultList();

        /**
         * GWT can't serialize EMPTY_LIST.
         */
        if (results.equals(Collections.EMPTY_LIST)) {
            results = new ArrayList<T>();
        }

        pagedSet = new PagedSet<T>(inRequest.getFirstResult(), inRequest.getFirstResult() + results.size() - 1,
                fullTextQuery.getResultSize(), results);
    } catch (ParseException e) {
        log.error(e);
    }

    return pagedSet;
}

From source file:org.eurekastreams.server.service.actions.strategies.activity.datasources.LuceneDataSource.java

License:Apache License

/**
 * Fetch a page of search results, using the keywords in the input JSON request.
 * //  w  w  w  . j av a2s  .c o  m
 * @param inRequest
 *            the JSON request containing query->keywords
 * @param userEntityId
 *            the user entity ID.
 * @return the activity ids
 */
@Override
@SuppressWarnings("unchecked")
public List<Long> fetch(final JSONObject inRequest, final Long userEntityId) {
    JSONObject jsonQuery = inRequest.getJSONObject("query");

    Boolean hasTerm = false;

    for (String term : requestToField.keySet()) {
        if (jsonQuery.containsKey(term)) {
            hasTerm = true;
            break;
        }
    }

    // As an optimizing, the LuceneDataSource is not execute if there is not search term, and
    // the sort is by date, since it can just fall back to memcached.
    if (!hasTerm && (!jsonQuery.containsKey("sortBy")
            || (jsonQuery.containsKey("sortBy") && jsonQuery.getString("sortBy").equals("date")))) {
        log.debug("No search term found");
        return null;
    }

    String luceneQuery = "";
    FullTextQuery query = null;

    for (Entry<String, String> entry : requestToField.entrySet()) {
        String req = entry.getKey();

        if (jsonQuery.containsKey(req)) {
            String searchWord = jsonQuery.getString(req);
            // remove semicolons, which can be used to search other fields
            searchWord = searchWord.replace(":", "");

            if (transformers.containsKey(req)) {
                searchWord = (String) transformers.get(req).transform(jsonQuery, userEntityId);
            }

            // if this is activity content - keep track of it for special content-only handling
            if (req.toLowerCase().equals("keywords")) {
                if (searchWord.contains("NOT ") || searchWord.contains("!") || searchWord.contains("-")) {
                    // searching content with a NOT component
                    log.info("User is querying for activity content with:(" + searchWord
                            + ") and seems to be using a NOT/!/- component.  Lucene doesn't allow "
                            + "NOT queries with one component, so I'll add a constant keyword that "
                            + "I know is present: " + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT);

                    luceneQuery += "+" + entry.getValue() + ":("
                            + Activity.CONSTANT_KEYWORD_IN_EVERY_ACTIVITY_CONTENT + " " + searchWord + ") ";
                } else {
                    // searching content without NOT component
                    luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") ";
                }
            } else {
                // searching non-content
                luceneQuery += "+" + entry.getValue() + ":(" + searchWord + ") ";
            }
        }
    }

    if (luceneQuery.length() == 0) {
        log.debug("Returning all activity");
        query = unstemmedRequestBuilder.buildQueryFromNativeSearchString(
                "_hibernate_class:" + "org.eurekastreams.server.domain.stream.Activity");
    } else {
        query = searchRequestBuilder.buildQueryFromNativeSearchString(luceneQuery);
    }

    if (jsonQuery.containsKey("sortBy")) {
        query.setSort(new Sort(new SortField(jsonQuery.getString("sortBy"), true)));
    }

    if (log.isDebugEnabled()) {
        log.debug("Native Lucene Query: " + query.toString());
    }

    searchRequestBuilder.setPaging(query, 0, maxResults);

    List<Long> activityIds = query.getResultList();
    if (log.isInfoEnabled()) {
        log.info("Found " + activityIds.size() + " activities");
    }
    return activityIds;
}

From source file:org.eurekastreams.server.service.actions.strategies.directory.SortFieldBuilder.java

License:Apache License

/**
 * Get the SortField[] for the input ResourceSortCriteria.
 * /*  w w  w .  jav a2s. c  o  m*/
 * @param inSortCriteria
 *            the sort criteria to use
 * @return the Sort object describing the sorting
 */
public Sort getSort(final ResourceSortCriteria inSortCriteria) {
    ResourceSortCriteria sortCriteria = overridingSortCriteria != null ? overridingSortCriteria
            : inSortCriteria;

    ArrayList<org.apache.lucene.search.SortField> sortFields = new ArrayList<org.apache.lucene.search.SortField>();
    for (ResourceSortCriterion criterion : sortCriteria.getCriteria()) {
        boolean reverse = criterion.getSortDirection() == SortDirection.DESCENDING;
        switch (criterion.getSortField()) {
        case DATE_ADDED:
            sortFields.add(new org.apache.lucene.search.SortField("dateAdded", reverse));
            break;
        case FOLLOWERS_COUNT:
            sortFields.add(new org.apache.lucene.search.SortField("followersCount", reverse));
            break;
        case UPDATES_COUNT:
            sortFields.add(new org.apache.lucene.search.SortField("updatesCount", reverse));
            break;
        default:
            // fall back on name
            sortFields.add(new org.apache.lucene.search.SortField("byName", reverse));
        }
    }
    return new Sort(sortFields.toArray(new org.apache.lucene.search.SortField[sortFields.size()]));
}

From source file:org.exoplatform.services.jcr.impl.core.query.lucene.SearchIndex.java

License:Apache License

/**
 * Executes the query on the search index.
 * //  w  w w.j  a  va  2s.  co  m
 * @param session
 *            the session that executes the query.
 * @param queryImpl
 *            the query impl.
 * @param query
 *            the lucene query.
 * @param orderProps
 *            name of the properties for sort order.
 * @param orderSpecs
 *            the order specs for the sort order properties.
 *            <code>true</code> indicates ascending order,
 *            <code>false</code> indicates descending.
 * @param resultFetchHint
 *            a hint on how many results should be fetched.
 * @return the query hits.
 * @throws IOException
 *             if an error occurs while searching the index.
 * @throws RepositoryException
 */
public MultiColumnQueryHits executeQuery(SessionImpl session, AbstractQueryImpl queryImpl, Query query,
        QPath[] orderProps, boolean[] orderSpecs, long resultFetchHint)
        throws IOException, RepositoryException {
    waitForResuming();

    checkOpen();

    workingThreads.incrementAndGet();

    try {
        FieldComparatorSource scs = queryImpl.isCaseInsensitiveOrder() ? this.sics : this.scs;
        Sort sort = new Sort(createSortFields(orderProps, orderSpecs, scs));
        final IndexReader reader = getIndexReader(queryImpl.needsSystemTree());
        JcrIndexSearcher searcher = new JcrIndexSearcher(session, reader, getContext().getItemStateManager());
        searcher.setSimilarity(getSimilarity());
        return new FilterMultiColumnQueryHits(
                searcher.execute(query, sort, resultFetchHint, QueryImpl.DEFAULT_SELECTOR_NAME)) {
            @Override
            public void close() throws IOException {
                try {
                    super.close();
                } finally {
                    PerQueryCache.getInstance().dispose();
                    Util.closeOrRelease(reader);
                }
            }
        };
    } finally {
        workingThreads.decrementAndGet();

        if (isSuspended.get() && workingThreads.get() == 0) {
            synchronized (workingThreads) {
                workingThreads.notifyAll();
            }
        }
    }
}

From source file:org.fao.geonet.kernel.search.LuceneSearcher.java

License:Open Source License

/**
 * Creates the Sort to use in the search.
 *
 * @param fields//from   w w  w  .j a  va  2s.  c  o  m
 * @param requestLanguage
 * @param sortRequestedLanguageOnTop
 * @return
 */
public static Sort makeSort(List<Pair<String, Boolean>> fields, String requestLanguage,
        boolean sortRequestedLanguageOnTop) {
    List<SortField> sortFields = new ArrayList<SortField>();
    if (sortRequestedLanguageOnTop && requestLanguage != null) {
        // Add a sort so the metadata defined in the requested language are the first metadata in results
        sortFields.add(new LangSortField(requestLanguage));
    }
    for (Pair<String, Boolean> sortBy : fields) {
        if (Log.isDebugEnabled(Geonet.SEARCH_ENGINE))
            Log.debug(Geonet.SEARCH_ENGINE, "Sorting by : " + sortBy);
        SortField sortField = LuceneSearcher.makeSortField(sortBy.one(), sortBy.two());
        if (sortField != null)
            sortFields.add(sortField);
    }
    sortFields.add(SortField.FIELD_SCORE);
    return new Sort(sortFields.toArray(new SortField[sortFields.size()]));
}