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

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

Introduction

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

Prototype

public TopFieldDocs search(Query query, int n, Sort sort, boolean doDocScores) throws IOException 

Source Link

Document

Search implementation with arbitrary sorting, plus control over whether hit scores and max score should be computed.

Usage

From source file:CFX_GoatSearch.java

License:Open Source License

/**
 * Classes that implement this interface can be specified in the CLASS attribute of the Java CFX tag. For example, in a class MyCustomTag, which implements this interface, the following CFML code calls the MyCustomTag.processRequest method.
 *
 * @param request/*from  w  w w  . jav  a 2s. co m*/
 * @param response
 * @throws Exception
 */
public void processRequest(Request request, Response response) throws Exception {

    Date startTime = new Date();
    String indexPath = null;
    String queryName = null;
    String searchString = null;
    String sortField = null;
    String sortDirection = null;
    int hitsPerPage = 0;
    int pageNumber = 0;
    Vector errors = new Vector();

    if (request.attributeExists("INDEXPATH")) {
        indexPath = request.getAttribute("INDEXPATH");
    } else {
        errors.add("The cfx_lucene tag requires an attribute called 'INDEXPATH'.");
    }

    if (request.attributeExists("HITSPERPAGE")) {
        hitsPerPage = request.getIntAttribute("HITSPERPAGE");
    }

    if (request.attributeExists("PAGENUMBER")) {
        pageNumber = request.getIntAttribute("PAGENUMBER");
    }

    if (request.attributeExists("QUERYNAME")) {
        queryName = request.getAttribute("QUERYNAME");
    } else {
        errors.add("The cfx_lucene tag requires an attribute called 'QUERYNAME'.");
    }

    if (request.attributeExists("SEARCHSTRING")) {
        searchString = request.getAttribute("SEARCHSTRING");
    } else {
        errors.add("The cfx_lucene tag requires an attribute called 'SEARCHSTRING'.");
    }

    //Sorting
    if (request.attributeExists("SORTFIELD")) {
        sortField = request.getAttribute("SORTFIELD");
    }

    if (request.attributeExists("SORTDIRECTION")) {
        sortDirection = request.getAttribute("SORTDIRECTION");
    }

    //Errors
    if (!errors.isEmpty()) {
        response.write("<h2 style=\"color: #FF0000\">CFX Goat Error:</h2>");
        for (int i = 0; i < errors.size(); i++) {
            response.write("<p>Error: " + errors.get(i) + "</p>\n");
        }
        //return;
    } else {

        try {

            IndexReader reader = IndexReader.open(indexPath);
            IndexSearcher searcher = new IndexSearcher(indexPath);
            if (searcher == null) {
                errors.add("Unable to open index");
            }

            XMLReader readerXML = new XMLReader(); //XML Reader Class
            String configFile = ConfigFiles.getSchemaFile(indexPath);
            String[] indexTypeArray = new String[Integer.parseInt(readerXML.getTotalNodes(configFile))];
            String[] columnNamesArray = new String[Integer.parseInt(readerXML.getTotalNodes(configFile))]; //Add Column Names
            int totalNodes = columnNamesArray.length;
            String nodeName = "";
            //Sort .:.  Index Type must be PrimaryKey,Keyword,Date
            Sort sortby = new Sort();
            if (sortField != null)
                sortField.trim().toLowerCase(); //Change Field TO LowerCase

            Analyzer analyzer = new StandardAnalyzer();
            QueryParser parser = new MultiFieldQueryParser(columnNamesArray, analyzer);
            Query query = parser.parse(searchString);
            if (query == null) {
                errors.add("Unable to build Query");
            }
            //Build Query Here             
            //Get Column Names
            for (int i = 0; i < totalNodes; i++) {
                columnNamesArray[i] = readerXML.getNodeValueByFile(configFile, i, "columnname");
                indexTypeArray[i] = readerXML.getNodeValueByFile(configFile, i, "indextype");

                /* Make Sure Field Can Be Seached */
                if (columnNamesArray[i].equalsIgnoreCase(sortField)
                        && (indexTypeArray[i].equalsIgnoreCase("PrimaryKey")
                                || indexTypeArray[i].equalsIgnoreCase("Keyword")
                                || indexTypeArray[i].equalsIgnoreCase("Date"))) {
                    //Sort Ascending 
                    if (sortDirection != null && sortDirection.equalsIgnoreCase("desc")) {
                        System.out.println("desc");
                        sortby = new Sort(sortField, true);
                    } else if (sortDirection != null && sortDirection.equalsIgnoreCase("asc")) {
                        System.out.println("asc");
                        sortby = new Sort(sortField, false);
                    }

                }

            }

            if (hitsPerPage < 1)
                hitsPerPage = 1;
            int pageNum = pageNumber;
            int recordSet = (pageNum * hitsPerPage) + 100;

            TopFieldDocs resultDocs = searcher.search(query, null, recordSet, sortby);
            ScoreDoc[] hits = resultDocs.scoreDocs;
            int numTotalHits = resultDocs.totalHits;

            //Start
            int start = (pageNum - 1);
            if (start < 0)
                start = 0;

            if (pageNum > 1) {
                start = (pageNum * hitsPerPage) - hitsPerPage;
            }

            int end = (pageNum * hitsPerPage);
            end = Math.min(hits.length, start + hitsPerPage);

            //Coldfusion Query
            com.allaire.cfx.Query goatQuery = response.addQuery(queryName, columnNamesArray);

            for (int i = start; i < end; i++) {
                int row = goatQuery.addRow(); //Add Row
                int docId = hits[i].doc;
                Document d = searcher.doc(docId);

                for (int x = 0; x < totalNodes; x++) {
                    nodeName = columnNamesArray[x];
                    goatQuery.setData(row, (x + 1), d.get(nodeName)); //Insert Values .:. Set Data starts with 1                  

                }
            }

            //reader.close();  
            searcher.close();
            Date endTime = new Date();

            //Set other Values
            response.setVariable("goat.totaltime", Long.toString(endTime.getTime() - startTime.getTime()));
            response.setVariable("goat.totalresults", Integer.toString(numTotalHits));
            response.setVariable("goat.totalpages", Integer.toString((numTotalHits / hitsPerPage)));
        }

        catch (Exception e) {
            errors.add("Failure caught a " + e.getClass() + " with message: " + e.getMessage());
        }
    }

    //Output Final Errors If Needed
    if (!errors.isEmpty()) {
        response.write("<h2 style=\"color: #FF0000\">CFX Goat Error:</h2>");
        for (int i = 0; i < errors.size(); i++) {
            response.write("<p>Error: " + errors.get(i) + "</p>\n");
        }
    }

}

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);
    }//ww w.  ja v  a2s .c o  m
    s.close();
    r.close();
    dir.close();
}

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

License:Apache License

public void displayResults(Query query, Sort sort) //
        throws IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    searcher.setDefaultFieldSortScoring(true, false); //

    TopDocs results = searcher.search(query, null, //
            20, sort); //

    LOGGER.info("\nResults for: " + //
            query.toString() + " sorted by " + sort);

    LOGGER.info(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10)
            + StringUtils.center("id", 4) + StringUtils.center("score", 15));

    PrintStream out = new PrintStream(System.out, true, "UTF-8"); //

    DecimalFormat scoreFormatter = new DecimalFormat("0.######");
    for (ScoreDoc sd : results.scoreDocs) {
        int docID = sd.doc;
        float score = sd.score;
        Document doc = searcher.doc(docID);
        out.println(StringUtils.rightPad( //
                StringUtils.abbreviate(doc.get("title"), 29), 30) + //
                StringUtils.rightPad(doc.get("pubmonth"), 10) + //
                StringUtils.center("" + docID, 4) + //
                StringUtils.leftPad( //
                        scoreFormatter.format(score), 12)); //
        out.println("   " + doc.get("category"));
        //out.println(searcher.explain(query, docID));   //
    }/*from  w ww  .ja  v  a 2 s  . com*/

    searcher.close();
}

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  a  v  a  2 s .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:bzh.terrevirtuelle.navisu.gazetteer.impl.lucene.GeoNameResolver.java

License:Apache License

/**
 * Returns a list of location near a certain coordinate.
 *
 * @param latitude, @param longitude - Center of search area
 * @param distanceInMiles - Search Radius in miles
 * @param indexerPath - Path to Lucene index
 * @param count - Upper bound to number of results
 * @return - List of locations sorted by population
 * @throws IOException//from w  w  w  .j  av  a2s.co m
 */
public List<Location> searchNearby(Double latitude, Double longitude, Double distanceInMiles,
        String indexerPath, int count) throws IOException {

    double distanceInDeg = DistanceUtils.dist2Degrees(distanceInMiles,
            DistanceUtils.EARTH_EQUATORIAL_RADIUS_MI);
    SpatialArgs spatialArgs = new SpatialArgs(SpatialOperation.IsWithin,
            ctx.makeCircle(longitude, latitude, distanceInDeg));

    String key = latitude + "-" + longitude;
    Filter filter = strategy.makeFilter(spatialArgs);

    IndexSearcher searcher = new IndexSearcher(createIndexReader(indexerPath));
    Sort sort = new Sort(populationSort);
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), filter, count, sort);

    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>();

    getMatchingCandidates(searcher, allCandidates, key, scoreDocs);
    List<Location> results = allCandidates.get(key);

    return results;
}

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;//  w w w  . ja va2s . c o 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();
    }
}

From source file:com.berico.clavin.resolver.impl.lucene.LuceneLocationNameIndex.java

License:Apache License

/**
 * Return a list of Resolved Locations that best match the Location Occurrence
 * found in a document./*  w  ww  .j  a  v a  2s. co m*/
 * @param occurrence The Location Occurrence.
 * @param options Options for the index.
 * @return List of Resolved Locations matching the occurrence.
 */
@Override
public List<ResolvedLocation> search(LocationOccurrence occurrence, Options options) throws Exception {

    options = (options == null) ? new Options() : options;

    // Get the max number of records to return.
    int limit = options.getInt(KEY_DEFAULT_LIMIT, DEFAULT_LIMIT);

    // Get whether fuzzy matching is enabled.
    boolean useFuzzy = options.getBoolean(KEY_DEFAULT_USE_FUZZY, DEFAULT_USE_FUZZY);

    IndexSearcher searcher = lucene.getSearcherManager().acquire();

    boolean usedFuzzy = false;

    // We need to sanitize the name so it doesn't have unescaped Lucene syntax that
    // would throw off the search index.
    String escapedName = QueryParserBase.escape(occurrence.getText().toLowerCase());

    // Try an exact query
    Query query = getExactQuery(escapedName);

    // Gather the results.
    TopDocs results = searcher.search(query, null, limit, DEFAULT_SORTER);

    // If there are no results, and a fuzzy query was requested
    if (results.scoreDocs.length == 0 && useFuzzy) {

        usedFuzzy = true;

        // Attempt a fuzzy query
        query = getFuzzyQuery(escapedName);

        // Gather the results
        results = searcher.search(query, null, limit, DEFAULT_SORTER);
    }

    if (results.scoreDocs.length == 0)
        logger.info("Found no results for {}.", escapedName);

    return LuceneUtils.convertToLocations(occurrence, searcher, results, usedFuzzy);
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public int count(String kind, Query query, Filter filter, Sort sort, int max) {
    int tag = 0;/*from  ww  w . j ava  2 s .  c  om*/
    try {
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        tag = td.totalHits;
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int max) {
    List<SEntity> tag = new ArrayList<SEntity>();
    try {/*from ww w  .  j  a  va2  s  .c  o  m*/
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        for (int i = 0; i < td.totalHits; i++) {
            SEntity item = new SEntity(this);
            Document doc = searcher.doc(td.scoreDocs[i].doc);
            item.setSchema(doc.get(SEntity.SCHEMA));
            read(item, doc);
            tag.add(item);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}

From source file:com.bewsia.script.LuceneHandler.java

License:Open Source License

public List<SEntity> search(String kind, Query query, Filter filter, Sort sort, int pagesize, int pageno) {
    List<SEntity> tag = new ArrayList<SEntity>();
    try {/*from w ww.  j  av a2  s  . co m*/
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(dirIndex)));
        IndexSearcher searcher = new IndexSearcher(reader);
        BooleanQuery boolQuery = new BooleanQuery();
        boolQuery.add(new BooleanClause(new TermQuery(new Term(SEntity.KIND, kind)), Occur.MUST));
        if (query != null) {
            boolQuery.add(new BooleanClause(query, Occur.MUST));
        }
        if (pagesize <= 0)
            pagesize = 10;
        if (pageno <= 0)
            pageno = 1;
        int max = pageno * pagesize;
        TopDocs td = null;
        if (filter != null && sort != null) {
            td = searcher.search(boolQuery, filter, max, sort);
        } else if (filter != null) {
            td = searcher.search(boolQuery, filter, max);
        } else if (sort != null) {
            td = searcher.search(boolQuery, max, sort);
        } else {
            td = searcher.search(boolQuery, max);
        }
        for (int i = (pageno - 1) * pagesize; i < td.totalHits && i < max; i++) {
            SEntity item = new SEntity(this);
            Document doc = searcher.doc(td.scoreDocs[i].doc);
            item.setSchema(doc.get(SEntity.SCHEMA));
            read(item, doc);
            tag.add(item);
        }
        searcher.close();
        reader.close();
    } catch (Exception e) {
    }
    return tag;
}