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

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

Introduction

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

Prototype

public void setSort(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.janusgraph.diskstorage.lucene.LuceneIndex.java

License:Apache License

private static Sort getSortOrder(List<IndexQuery.OrderEntry> orders) {
    final Sort sort = new Sort();
    if (!orders.isEmpty()) {
        final SortField[] fields = new SortField[orders.size()];
        for (int i = 0; i < orders.size(); i++) {
            final IndexQuery.OrderEntry order = orders.get(i);
            SortField.Type sortType = null;
            final Class dataType = order.getDatatype();
            if (AttributeUtil.isString(dataType))
                sortType = SortField.Type.STRING;
            else if (AttributeUtil.isWholeNumber(dataType))
                sortType = SortField.Type.LONG;
            else if (AttributeUtil.isDecimal(dataType))
                sortType = SortField.Type.DOUBLE;
            else if (dataType.equals(Instant.class) || dataType.equals(Date.class))
                sortType = SortField.Type.LONG;
            else//from   w ww  .  j av  a2s .c o m
                Preconditions.checkArgument(false,
                        "Unsupported order specified on field [%s] with datatype [%s]", order.getKey(),
                        dataType);

            fields[i] = new SortField(order.getKey(), sortType, order.getOrder() == Order.DESC);
        }
        sort.setSort(fields);
    }
    return sort;
}

From source file:org.meresco.lucene.LuceneTest.java

License:Open Source License

@Test
public void testSorting() throws Throwable {
    Document doc1 = new Document();
    doc1.add(new StringField("field1", "AA", Store.NO));
    lucene.addDocument("id1", doc1);

    Document doc2 = new Document();
    doc2.add(new StringField("field1", "BB", Store.NO));
    lucene.addDocument("id2", doc2);

    Document doc3 = new Document();
    doc3.add(new StringField("field1", "CC", Store.NO));
    lucene.addDocument("id3", doc3);

    Sort sort = new Sort();
    sort.setSort(new SortField("field1", SortField.Type.STRING, false));
    QueryData q = new QueryData();
    q.sort = sort;// ww  w . j  a v  a 2 s . c  o m
    LuceneResponse result = lucene.executeQuery(q);
    assertEquals(3, result.total);
    assertEquals("id1", result.hits.get(0).id);
    assertEquals("id2", result.hits.get(1).id);
    assertEquals("id3", result.hits.get(2).id);

    sort = new Sort();
    sort.setSort(new SortField("field1", SortField.Type.STRING, true));
    q.sort = sort;
    result = lucene.executeQuery(q);
    assertEquals(3, result.total);
    assertEquals("id3", result.hits.get(0).id);
    assertEquals("id2", result.hits.get(1).id);
    assertEquals("id1", result.hits.get(2).id);
}

From source file:org.spirit.spring.search.EntityLinkSearchHandler.java

License:BSD License

/**
 * Default search, sort by score and date
 *//*from ww w. j a v a 2 s. c om*/
public List search(String queryLine) throws Exception {
    Sort sort = new Sort();
    SortField fields[] = { SortField.FIELD_SCORE, new SortField("yyyymmdd", SortField.STRING, true) };
    sort.setSort(fields);
    return this.search(queryLine, sort);
}

From source file:searcher.Searcher.java

public ArrayList<Noticia> busquedaTexto(String cadena)
        throws IOException, ParseException, org.apache.lucene.queryparser.classic.ParseException {

    ArrayList<Noticia> noticias = new ArrayList();
    FSDirectory directory = FSDirectory.open(new File(Searcher.indexLocation));
    DirectoryReader ireader = DirectoryReader.open(directory);
    IndexSearcher isearcher = new IndexSearcher(ireader);
    Sort sort;
    TopDocs hits;/*from w w w.j av a 2s  . c o m*/

    QueryParser parser = new QueryParser(Version.LUCENE_43, "Text", analyzer);
    Query consulta = parser.parse(cadena);

    sort = new Sort();
    sort.setSort(SortField.FIELD_DOC);
    hits = isearcher.search(consulta, max, sort);

    ScoreDoc[] arrayResultados = hits.scoreDocs;

    for (int i = 0; i < arrayResultados.length; i++) {

        Document doc = isearcher.doc(arrayResultados[i].doc);

        Noticia noticia = new Noticia();
        // Aadimos la informacin del documento
        noticia.setTitle(doc.get("Title"));
        noticia.setDate(Integer.parseInt(doc.get("Date")));
        noticia.setText(doc.get("Text"));
        noticias.add(noticia);
    }
    return noticias;
}

From source file:searcher.Searcher.java

public ArrayList<Noticia> consultaRango(Integer rangoA, Integer rangoB) throws IOException {
    FSDirectory directory = FSDirectory.open(new File(Searcher.indexLocation));
    DirectoryReader ireader = DirectoryReader.open(directory);
    IndexSearcher isearcher = new IndexSearcher(ireader);
    TopDocs hits;//from www  . j  ava2  s . c  o m
    Sort sort;
    ArrayList<Noticia> noticias = new ArrayList();

    Query q = NumericRangeQuery.newIntRange("Date", rangoA, rangoB, true, true);

    sort = new Sort();
    sort.setSort(SortField.FIELD_DOC);
    hits = isearcher.search(q, max, sort);

    ScoreDoc[] arrayResultados = hits.scoreDocs;

    String cabecera = ("Hemos obtenido " + hits.totalHits + " resultados : \n");
    String cuerpo = "Cuerpo de la salida";

    for (int i = 0; i < arrayResultados.length; i++) {
        Document doc = isearcher.doc(arrayResultados[i].doc);

        Noticia noticia = new Noticia();
        // Aadimos la informacin del documento
        noticia.setTitle(doc.get("Title"));
        noticia.setDate(Integer.parseInt(doc.get("Date")));
        noticia.setText(doc.get("Text"));
        noticias.add(noticia);
    }
    // miInterfaz.setSalida(cabecera, cuerpo);  
    return noticias;
}