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

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

Introduction

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

Prototype

public IndexSearcher(IndexReaderContext context) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:br.bireme.mlts.MoreLikeThat.java

License:Open Source License

/**
 * Constructor of the class//ww w.j  a v  a 2  s .c  o m
 * @param dir directory of the Lucene index
 * @param analyz  analyzer used during search and similar
 * @throws IOException
 */
public MoreLikeThat(final File dir, final Analyzer analyz) throws IOException {
    if (dir == null) {
        throw new NullPointerException("dir");
    }
    if (analyz == null) {
        throw new NullPointerException("analyz");
    }

    if (!dir.isDirectory()) {
        throw new IllegalArgumentException(dir.getCanonicalPath() + " is not a directory");
    }
    directory = new SimpleFSDirectory(dir); /* RAMDirectory(new SimpleFSDirectory(dir)); //MMapDirectory(dir);*/
    ir = IndexReader.open(directory);
    if (ir.numDocs() == 0) {
        throw new IllegalArgumentException("zero document index");
    }
    is = new IndexSearcher(ir);
    analyzer = analyz;
    stopwords = (analyzer instanceof StopwordAnalyzerBase) ? ((StopwordAnalyzerBase) analyzer).getStopwordSet()
            : null;
    defaultValues();
}

From source file:br.bireme.ngrams.CompareResults.java

private static void compare(final String resultFile, final String indexPath, final String outputFile,
        final String encoding) throws IOException {
    assert resultFile != null;
    assert indexPath != null;
    assert outputFile != null;
    assert encoding != null;

    try (DirectoryReader ireader = DirectoryReader.open(new MMapDirectory(new File(indexPath).toPath()))) {
        final IndexSearcher isearcher = new IndexSearcher(ireader);

        try (BufferedReader breader = Files.newBufferedReader(new File(resultFile).toPath(),
                Charset.forName(encoding));
                BufferedWriter bwriter = Files.newBufferedWriter(new File(outputFile).toPath(),
                        Charset.forName(encoding))) {
            boolean first = true; // first line

            while (true) {
                final String line = breader.readLine();
                if (line == null)
                    break;
                if (first) {
                    first = false; // drop header line (first)
                } else {
                    final String line2 = line.trim();
                    if (!line2.isEmpty()) {
                        final String[] split = line2.split("\\|");
                        checkDocs(split[1], split[2], split[3], isearcher, bwriter);
                    }//from  w  w  w .j a v  a2s.com
                }
            }
        }
    }
}

From source file:br.bireme.ngrams.NGIndex.java

private IndexSearcher getIndexSearcher(final String indexPath) throws IOException {

    final DirectoryReader ireader = DirectoryReader.open(
            //FSDirectory.open(new File(indexPath).toPath()));
            new MMapDirectory(new File(indexPath).toPath()));
    //new RAMDirectory(FSDirectory.open(new File(indexPath).toPath()), IOContext.DEFAULT));
    //new RAMDirectory(FSDirectory.open(new File(indexPath).toPath()), IOContext.READONCE));

    return new IndexSearcher(ireader);
}

From source file:br.com.crawlerspring.model.Searcher.java

public List<br.com.crawlerspring.model.Document> parametrizeDocuments(String parameters) throws Exception {
    List<br.com.crawlerspring.model.Document> parametrizedDocuments = new ArrayList<br.com.crawlerspring.model.Document>();

    RegexQuery q = new RegexQuery(new Term("title", ".*" + parameters + ".*"));
    int hitsPerPage = 10;
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
    searcher.search(q, collector);/*from   ww  w .j a v a  2s.c om*/
    ScoreDoc[] hits = collector.topDocs().scoreDocs;

    for (int cont = 0; cont < hits.length; ++cont) {
        br.com.crawlerspring.model.Document document = new br.com.crawlerspring.model.Document();
        int docId = hits[cont].doc;
        org.apache.lucene.document.Document luceneDocument = searcher.doc(docId);
        document.setTitle(luceneDocument.get("title"));
        document.setContent(luceneDocument.get("content"));

        parametrizedDocuments.add(document);
    }
    return parametrizedDocuments;
}

From source file:br.ufmt.harmonizacao.implementer.PatenteeSearcher.java

public void setPath(String path) {
    try {//ww w.  java2 s .c o  m
        this.path = path + dirName;
        System.out.println(this.path);
        dir = new SimpleFSDirectory(new File(this.path));
        reader = DirectoryReader.open(dir);
        searcher = new IndexSearcher(reader);
    } catch (IOException ex) {
        Logger.getLogger(PatenteeSearcher.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:br.ufmt.harmonizacao.implementer.StandardSearcher.java

public void setPath(String path) {
    try {/*from   w  w  w .  ja  v a2s .c om*/
        this.path = path + dirName;
        System.out.println(this.path);
        dir = new SimpleFSDirectory(new File(this.path));
        reader = DirectoryReader.open(dir);
        searcher = new IndexSearcher(reader);
    } catch (IOException ex) {
        Logger.getLogger(PatenteeSearcher.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:br.usp.icmc.gazetteer.SemanticSearchTest.LuceneSearcher.java

License:Open Source License

public void search1(String q) throws IOException, ParseException {
    IndexReader ireader = IndexReader.open(dir); // read-only=true
    IndexSearcher reader = new IndexSearcher(ireader);
    reader.setSimilarity(similarityltc()); //ddd.qqq
    busca(reader, q);/*from  w  w  w. jav a  2 s .co m*/
    numQ++;
}

From source file:buscador.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/core/4_1_0/demo/ for details.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);//from   w  w  w. java  2 s . co  m
    }

    String index = "Zaguan1";
    String[] fields = { "title", "description", "identifier", "date", "creator" };
    BooleanClause.Occur[] flags = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };

    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = null;
    int hitsPerPage = 10;

    for (int i = 0; i < args.length; i++) {
        if ("-index".equals(args[i])) {
            index = args[i + 1];
            i++;

        } else if ("-queries".equals(args[i])) {
            queries = args[i + 1];
            i++;
        } else if ("-query".equals(args[i])) {
            queryString = args[i + 1];
            i++;
        } else if ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-paging".equals(args[i])) {
            hitsPerPage = Integer.parseInt(args[i + 1]);
            if (hitsPerPage <= 0) {
                System.err.println("There must be at least 1 hit per page.");
                System.exit(1);
            }
            i++;
        }
    }

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new SpanishAnalyzer(Version.LATEST);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(queries), "UTF-8"));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }

    while (true) {
        if (queries == null && queryString == null) { // prompt the user
            System.out.println("Enter query: ");
        }

        String line = queryString != null ? queryString : in.readLine();

        if (line == null || line.length() == -1) {
            break;
        }

        line = line.trim();
        if (line.length() == 0) {
            break;
        }

        Query query = MultiFieldQueryParser.parse(line, fields, flags, analyzer);

        if (repeat > 0) { // repeat & time as benchmark
            Date start = new Date();
            for (int i = 0; i < repeat; i++) {
                searcher.search(query, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null && queryString == null);

        if (queryString != null) {
            break;
        }
    }
    reader.close();
}

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

License:Apache License

private HashMap<String, List<Location>> resolveEntities(List<String> locationNames, int count,
        IndexReader reader) throws IOException {
    if (locationNames.size() >= 200) {
        hitsPerPage = 5; // avoid heavy computation
    }// ww  w  . j av a2 s.co m
    IndexSearcher searcher = new IndexSearcher(reader);
    Query q = null;

    HashMap<String, List<Location>> allCandidates = new HashMap<String, List<Location>>();

    for (String name : locationNames) {

        if (!allCandidates.containsKey(name)) {
            try {
                //query is wrapped in additional quotes (") to avoid query tokenization on space
                q = new MultiFieldQueryParser(new String[] { FIELD_NAME_NAME, FIELD_NAME_ALTERNATE_NAMES },
                        analyzer).parse(String.format("\"%s\"", name));

                Sort sort = new Sort(populationSort);
                //Fetch 3 times desired values, these will be sorted on code and only desired number will be kept
                ScoreDoc[] hits = searcher.search(q, hitsPerPage * 3, sort).scoreDocs;

                getMatchingCandidates(searcher, allCandidates, name, hits);
            } catch (org.apache.lucene.queryparser.classic.ParseException e) {
                e.printStackTrace();
            }
        }
    }

    HashMap<String, List<Location>> resolvedEntities = new HashMap<String, List<Location>>();
    pickBestCandidates(resolvedEntities, allCandidates, count);
    return resolvedEntities;
}