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

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

Introduction

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

Prototype

public SortField(String field, FieldComparatorSource comparator) 

Source Link

Document

Creates a sort with a custom comparison function.

Usage

From source file:syslogSearch.java

License:Open Source License

public void run() {

    try {//from w  ww . j  a v  a  2 s  .com

        String searchQuery = (new BufferedReader(new InputStreamReader(searchSocket.getInputStream())))
                .readLine().trim();

        IndexReader reader = writer.getReader();
        Searcher searcher = new IndexSearcher(reader);

        QueryParser indexParser = new QueryParser(Version.LUCENE_30, "data", analyzer);

        SortField hitSortField = new SortField("date", SortField.LONG);
        Sort hitSort = new Sort(hitSortField);

        TopFieldDocs hits = searcher.search(indexParser.parse(searchQuery), null, 1000, hitSort);

        PrintWriter searchReply = new PrintWriter(searchSocket.getOutputStream(), true);

        searchReply.println(hits.totalHits + " Hits for " + searchQuery);

        for (int i = 0; i < hits.totalHits; i++) {
            Document document = searcher.doc(hits.scoreDocs[i].doc);

            String host = document.get("hostname");
            String date = document.get("date");
            String data = document.get("data");

            searchReply.print("host: " + host + ", date: " + date + ", data: " + data + "\n\n");
        }

        searchReply.close();
        searcher.close();
        reader.close();
        searchSocket.close();
    } catch (Exception ex) {
        System.out.print("Exception: " + ex + "\n");
    }

}

From source file:DVBench.java

License:Apache License

static long search(String description, IndexSearcher searcher, String field, int iters, boolean quiet)
        throws Exception {
    int count = 0;
    Query query = new MatchAllDocsQuery();
    Sort sort = new Sort(new SortField(field, SortField.Type.LONG));
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < iters; i++) {
        TopDocs td = searcher.search(query, 20, sort);
        count += td.totalHits;/*from   w  ww  .  j  av  a  2  s.co m*/
    }
    long endTime = System.currentTimeMillis();
    if (!quiet) {
        double delta = endTime - startTime;
        double avg = delta / iters;
        double QPS = 1000d / avg;
        System.out.println(description + ": " + QPS);
    }
    return count;
}

From source file:aos.lucene.search.advanced.FunctionQueryTest.java

License:Apache License

public void testRecency() throws Throwable {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader r = DirectoryReader.open(dir);
    IndexSearcher s = new IndexSearcher(r);
    s.setDefaultFieldSortScoring(true, true);

    QueryParser parser = new QueryParser(Version.LUCENE_46, "contents",
            new StandardAnalyzer(Version.LUCENE_46));
    Query q = parser.parse("java in action"); // #A
    Query q2 = new RecencyBoostingQuery(q, // #B
            2.0, 2 * 365, "pubmonthAsDay");
    Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("title2", SortField.STRING) });
    TopDocs hits = s.search(q2, null, 5, sort);

    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document doc = r.document(hits.scoreDocs[i].doc);
        LOGGER.info((1 + i) + ": " + doc.get("title") + ": pubmonth=" + doc.get("pubmonth") + " score="
                + hits.scoreDocs[i].score);
    }/*from  ww  w .ja v  a 2s  .co  m*/
    s.close();
    r.close();
    dir.close();
}

From source file:aos.lucene.search.advanced.SortingExample.java

License:Apache License

public static void main(String[] args) throws Exception {
    Query allBooks = new MatchAllDocsQuery();

    QueryParser parser = new QueryParser(Version.LUCENE_46, //
            "contents", //
            new StandardAnalyzer( //
                    Version.LUCENE_46)); //
    BooleanQuery query = new BooleanQuery(); //
    query.add(allBooks, BooleanClause.Occur.SHOULD); //
    query.add(parser.parse("java OR action"), BooleanClause.Occur.SHOULD); //

    Directory directory = TestUtil.getBookIndexDirectory(); //
    SortingExample example = new SortingExample(directory); //

    example.displayResults(query, Sort.RELEVANCE);

    example.displayResults(query, Sort.INDEXORDER);

    example.displayResults(query, new Sort(new SortField("category", SortField.STRING)));

    example.displayResults(query, new Sort(new SortField("pubmonth", SortField.INT, true)));

    example.displayResults(query, new Sort(new SortField("category", SortField.STRING), SortField.FIELD_SCORE,
            new SortField("pubmonth", SortField.INT, true)));

    example.displayResults(query,//w ww  . j  ava  2s  .c o m
            new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("category", SortField.STRING) }));
    directory.close();
}

From source file:aos.lucene.search.ext.sorting.DistanceSortingTest.java

License:Apache License

public void testNearestRestaurantToHome() throws Exception {
    Sort sort = new Sort(new SortField("unused", new DistanceComparatorSource(0, 0)));

    TopDocs hits = searcher.search(query, null, 10, sort);

    assertEquals("closest", "El Charro", searcher.doc(hits.scoreDocs[0].doc).get("name"));
    assertEquals("furthest", "Los Betos", searcher.doc(hits.scoreDocs[3].doc).get("name"));
}

From source file:aos.lucene.search.ext.sorting.DistanceSortingTest.java

License:Apache License

public void testNeareastRestaurantToWork() throws Exception {
    Sort sort = new Sort(new SortField("unused", new DistanceComparatorSource(10, 10)));

    TopFieldDocs docs = searcher.search(query, null, 3, sort); //

    assertEquals(4, docs.totalHits); //
    assertEquals(3, docs.scoreDocs.length); //

    FieldDoc fieldDoc = (FieldDoc) docs.scoreDocs[0]; //

    assertEquals("(10,10) -> (9,6) = sqrt(17)", new Float(Math.sqrt(17)), fieldDoc.fields[0]); //

    Document document = searcher.doc(fieldDoc.doc); //
    assertEquals("Los Betos", document.get("name"));

    //dumpDocs(sort, docs);
}

From source file:aos.lucene.tools.SpatialLuceneExample.java

License:Apache License

public void findNear(String what, double latitude, double longitude, double radius)
        throws CorruptIndexException, IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    DistanceQueryBuilder dq;//from   w ww  .j ava  2s  . co  m
    dq = new DistanceQueryBuilder(latitude, // #A
            longitude, // #A
            radius, // #A
            latField, // #A
            lngField, // #A
            tierPrefix, // #A
            true); // #A

    Query tq;
    if (what == null)
        tq = new TermQuery(new Term("metafile", "doc")); // #B
    else
        tq = new TermQuery(new Term("name", what));

    DistanceFieldComparatorSource dsort; // #C
    dsort = new DistanceFieldComparatorSource( // #C
            dq.getDistanceFilter()); // #C
    Sort sort = new Sort(new SortField("foo", dsort)); // #C

    TopDocs hits = searcher.search(tq, dq.getFilter(), 10, sort);

    Map<Integer, Double> distances = // #D
            dq.getDistanceFilter().getDistances(); // #D

    LOGGER.info("Number of results: " + hits.totalHits);
    LOGGER.info("Found:");
    for (ScoreDoc sd : hits.scoreDocs) {
        int docID = sd.doc;
        Document d = searcher.doc(docID);

        String name = d.get("name");
        double rsLat = NumericUtils.prefixCodedToDouble(d.get(latField));
        double rsLng = NumericUtils.prefixCodedToDouble(d.get(lngField));
        Double geo_distance = distances.get(docID);

        System.out.printf(name + ": %.2f Miles\n", geo_distance);
        LOGGER.info("\t\t(" + rsLat + "," + rsLng + ")");
    }
}

From source file:ca.dracode.ais.indexer.FileSearcher.java

License:Open Source License

/**
 * Searches for matches in the contents of multiple files
 * <p>//  w  w  w. j a  v  a  2s  .co  m
 *     For example, a search with the term "Foo" and the constrainValues {"/Bar", "/Stool"}
 *     will return pages with contents related to "Foo" only from the directories or
 *     subdirectories or "/Bar" and "/Stool"
 * </p>
 * @param id Identifier for the instance of ClientService that spawned the search
 * @param term The search term for choosing and ranking results
 * @param field The field to search, i.e. "contents"
 * @param constrainValues The paths to which to constrain the search
 * @param constrainField The field used to constrain searches
 * @param maxResults The maximum number of results that will be returned
 * @param set The set number, e.g., searching set 0 returns the first n results,
 *            searching set 1 returns the 2nd n results. Set must be positive
 * @param type The type of the search, one of QUERY_BOOLEAN or QUERY_STANDARD
 * @return A SearchResult containing the results sorted by relevance and page
 */
public SearchResult findInFiles(int id, String term, String field, List<String> constrainValues,
        String constrainField, int maxResults, int set, int type) {
    if (this.interrupt == id) {
        this.interrupt = -1;
        return null;
    }
    Query qry = this.getQuery(term, field, type);
    if (qry != null) {
        Filter filter = this.getFilter(constrainField, constrainValues, type, -1, -1);
        ScoreDoc[] hits = null;
        try {
            if (type == QUERY_BOOLEAN) {
                Sort sort = new Sort(new SortField("path", SortField.Type.STRING),
                        new SortField("page", SortField.Type.INT));
                hits = indexSearcher.search(qry, filter, maxResults * set + maxResults, sort).scoreDocs;
            } else {
                hits = indexSearcher.search(qry, filter, maxResults * set + maxResults).scoreDocs;
            }

        } catch (IOException e) {
            Log.e(TAG, "Error ", e);
        }
        if (this.interrupt == id) {
            this.interrupt = -1;
            return null;
        }
        List<Document> docs = this.getDocs(maxResults, set, hits);
        Log.i(TAG, "Found instance of term in " + docs.size() + " documents");
        return this.getHighlightedResults(docs, qry, type, term, maxResults);
    } else {
        Log.e(TAG, "Query Type: " + type + " not recognised");
        return null;
    }
}

From source file:ca.dracode.ais.indexer.FileSearcher.java

License:Open Source License

/**
 * Searches for matches in the contents of a single file
 * <p>/*  www.ja  v  a 2s  .  c o m*/
 *     For example, a search with the term "Foo" and the constrainValue "/Bar.txt"
 *     will return pages with contents related to "Foo" only from inside the file "/Bar.txt"
 * </p>
 * @param id Identifier for the instance of ClientService that spawned the search
 * @param term The search term for choosing and ranking results
 * @param field The field to search, i.e. "contents"
 * @param constrainValue The path to which to constrain the search
 * @param constrainField The field used to constrain searches
 * @param maxResults The maximum number of results that will be returned
 * @param set The set number, e.g., searching set 0 returns the first n results,
 *            searching set 1 returns the 2nd n results. A negative set can be used to search
 *            backwards from a page.
 * @param type The type of the search, one of QUERY_BOOLEAN or QUERY_STANDARD
 * @return A SearchResult containing the results sorted by relevance and page
 */
public SearchResult findInFile(int id, String term, String field, String constrainValue, String constrainField,
        int maxResults, int set, int type, final int page) {

    Query qry = this.getQuery(term, field, type);
    Log.i(TAG, "Query: " + term + " " + field + " " + type + " " + constrainValue);
    if (this.interrupt == id) {
        this.interrupt = -1;
        return null;
    }
    if (qry != null) {
        String[] values = { constrainValue };
        Filter filter;
        ScoreDoc[] hits = null;
        try {
            Log.i(TAG, "Searching...");
            Sort sort;
            if (type == QUERY_STANDARD) {
                sort = new Sort();
                filter = this.getFilter(constrainField, Arrays.asList(values), type, page, Integer.MAX_VALUE);
                hits = indexSearcher.search(qry, filter, maxResults * set + maxResults, sort).scoreDocs;
            } else {
                if (set >= 0) {
                    sort = new Sort(new SortField("page", SortField.Type.INT));
                    filter = this.getFilter(constrainField, Arrays.asList(values), type, page,
                            Integer.MAX_VALUE);
                    hits = indexSearcher.search(qry, filter, maxResults * set + maxResults, sort).scoreDocs;
                    if (hits.length < maxResults) {
                        filter = this.getFilter(constrainField, Arrays.asList(values), type, 0, page - 1);
                        hits = concat(hits, indexSearcher.search(qry, filter, maxResults, sort).scoreDocs);
                    }
                } else {
                    sort = new Sort(new SortField("page", SortField.Type.INT, true));
                    filter = this.getFilter(constrainField, Arrays.asList(values), type, 0, page - 1);
                    hits = indexSearcher.search(qry, filter, Integer.MAX_VALUE, sort).scoreDocs;
                    if (hits.length < maxResults) {
                        filter = this.getFilter(constrainField, Arrays.asList(values), type, page,
                                Integer.MAX_VALUE);
                        hits = concat(hits,
                                indexSearcher.search(qry, filter, maxResults - hits.length, sort).scoreDocs);
                    } else {
                        ScoreDoc[] tmp = hits;
                        hits = new ScoreDoc[maxResults * -(set + 1) + maxResults];
                        System.arraycopy(tmp, 0, hits, 0, maxResults * -(set + 1) + maxResults);
                    }
                }
            }
        } catch (IOException e) {
            Log.e(TAG, "Error ", e);
        }
        if (this.interrupt == id) {
            this.interrupt = -1;
            return null;
        }
        if (hits != null) {
            Log.i(TAG, "Found instance of term in " + hits.length + " documents");
            return this.getHighlightedResults(this.getDocs(maxResults, set, hits), qry, type, term, maxResults);
        }
    } else {
        Log.e(TAG, "Query Type: " + type + " not recognised");
        return null;
    }
    return null;
}

From source file:com.aliasi.lingmed.medline.IndexMedline.java

License:Lingpipe license

private String getLastUpdate(File index) throws IOException {
    System.out.println("Got index" + index);
    IndexReader reader = null;//from  w w  w . j  a  v  a2  s  .co m
    IndexSearcher searcher = null;
    try {
        if (isNewDirectory(mIndex))
            return LOW_SORT_STRING;
        Directory fsDir = FSDirectory.open(mIndex);
        reader = IndexReader.open(fsDir);
        searcher = new IndexSearcher(reader);

        Term term = new Term(Fields.MEDLINE_DIST_FIELD, Fields.MEDLINE_DIST_VALUE);
        SortField sortField = new SortField(Fields.MEDLINE_FILE_FIELD, SortField.STRING);
        Sort sort = new Sort(sortField);
        Query query = new TermQuery(term);
        TopFieldDocs results = searcher.search(query, null, 1, sort);
        if (results.totalHits == 0) {
            searcher.close();
            reader.close();
            return LOW_SORT_STRING;
        }
        //            if (mLogger.isDebugEnabled())
        //                mLogger.debug("num MEDLINE_FILE docs: " + results.totalHits);
        Document d = searcher.doc(results.scoreDocs[0].doc);
        return d.get(Fields.MEDLINE_FILE_FIELD);
    } finally {
        if (searcher != null)
            searcher.close();
        if (reader != null)
            reader.close();
    }
}