Example usage for org.apache.lucene.index IndexReader close

List of usage examples for org.apache.lucene.index IndexReader close

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader close.

Prototype

@Override
public final synchronized void close() throws IOException 

Source Link

Document

Closes files associated with this index.

Usage

From source file:back.Searcher.java

License:Apache License

/** Simple command-line based search demo. */
public static void search(String query, boolean stopword, boolean stemming, int consulta) throws Exception {

    String index = null;// w ww  . ja v a  2 s . com
    Analyzer analyzer = null;
    if (!stopword && !stemming) {
        index = ".\\indexed";
        analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT,
                new CharArraySet(Version.LUCENE_CURRENT, 0, false));
        System.out.println("Nenhum Marcado");
    } else if (stopword && !stemming) {
        index = ".\\indexedNoStpWrd";
        analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
        System.out.println("Primeiro Marcado");

    } else if (!stopword && stemming) {
        index = ".\\indexedStemming";
        analyzer = new EnglishAnalyzer(Version.LUCENE_CURRENT,
                new CharArraySet(Version.LUCENE_CURRENT, 0, false));
        System.out.println("Segundo Marcado");

    } else if (stopword && stemming) {
        index = ".\\indexedTreated";
        analyzer = new EnglishAnalyzer(Version.LUCENE_CURRENT);
        System.out.println("Dois Marcados");

    }
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String queryString = query;
    int hitsPerPage = 200;

    CSVReader CSVreader = new CSVReader(new FileReader(".\\matriz.csv"));
    List<String[]> myEntries = CSVreader.readAll();

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    IndexSearcher searcher = new IndexSearcher(reader);

    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"));
    }
    QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
    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 query1 = parser.parse(line);
        System.out.println("Searching for: " + query1.toString(field));

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

        doPagingSearch(in, searcher, query1, hitsPerPage, raw, queries == null && queryString == null,
                myEntries, consulta);

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

From source file:bajavista.Buscador.java

public ArrayList<Informacion> buscarContenido(String busqueda) throws IOException, ParseException {
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);

    File indexDirES = new File(dirIndexES);
    Directory indexES = FSDirectory.open(indexDirES);
    //File indexDirNONES = new File(dirIndexNONES);
    //Directory indexNONES = FSDirectory.open(indexDirNONES);

    // 2. Query/*w w  w  .  j a  va2s .c  o  m*/
    String querystr = busqueda;

    Query q = new QueryParser(Version.LUCENE_43, "text", analyzer).parse(querystr);
    //Query qNONES = new QueryParser(Version.LUCENE_43, "contenido", analyzer).parse(querystr);

    // 3. Search
    int hitsPage = 1024;
    IndexReader reader = DirectoryReader.open(indexES);
    IndexSearcher searcher = new IndexSearcher(reader);

    //IndexReader readerNONES = DirectoryReader.open(indexNONES);
    //IndexSearcher searcherNONES = new IndexSearcher(readerNONES);
    TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPage, true);
    //TopScoreDocCollector collectorNONES = TopScoreDocCollector.create(hitsPage, true);

    searcher.search(q, collector);
    //searcherNONES.search(q, collectorNONES);

    ScoreDoc[] hits = collector.topDocs().scoreDocs;
    // ScoreDoc[] hitsNONES = collectorNONES.topDocs().scoreDocs;

    // 4. Return results
    for (int i = 0; i < hits.length; ++i) {
        int docId = hits[i].doc;
        Document data = searcher.doc(docId);
        info = new Informacion(Integer.parseInt(data.get("idUser")), Long.parseLong(data.get("timestamp")),
                data.get("text"), Double.parseDouble(data.get("objective")),
                Double.parseDouble(data.get("subjective")), Double.parseDouble(data.get("positive")),
                Double.parseDouble(data.get("negative")), Integer.parseInt(data.get("need")));
        listaInfo.add(info);
    }

    /*System.out.println("No ES Found " + hitsNONES.length + " hits.");
     for(int i=0;i<hitsNONES.length;++i) {
     int docId = hitsNONES[i].doc;
     Document d = searcherNONES.doc(docId);
     System.out.println((i + 1) + ". " + d.get("es") + "\t" + d.get("contenido"));
     }*/
    reader.close();
    //readerNONES.close();

    return listaInfo;
}

From source file:BlockBuilding.AbstractBlockBuilding.java

License:Apache License

protected void closeReader(IndexReader iReader) {
    try {//from   w  ww . ja v a  2s  .  com
        iReader.close();
    } catch (IOException ex) {
        LOGGER.log(Level.SEVERE, null, ex);
    }
}

From source file:BlockBuilding.Utilities.java

License:Open Source License

public static void closeReader(IndexReader iReader) {
    try {//from w ww . j  a v a2  s  .  c om
        iReader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:bostoncase.widgets.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   ww w  .j  av a  2 s . co  m*/
    }

    String index = "/Users/michaelhundt/Documents/Meine/Studium/MASTER/MasterProject/data/lucene_index/";
    String field = "content";
    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 ("-field".equals(args[i])) {
            field = 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(Paths.get(index)));
    //      IndexReader reader = StandardDirectoryReader.open(FSDirectory.open(Paths.get(index)));

    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();

    BufferedReader in = null;
    if (queries != null) {
        in = Files.newBufferedReader(Paths.get(queries), StandardCharsets.UTF_8);
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    }
    QueryParser parser = new QueryParser(field, analyzer);
    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 = parser.parse(line);
        if (line.equals("geo")) {
            //            query = new GeoPointInBBoxQuery("geo", 42.2279, 42.3969, -70.9235, -71.1908);
            //            query = new GeoPointInBBoxQuery("geo", -71.1908, -70.9235, 42.2279, 42.3969);
            query = new GeoPointInBBoxQuery("geo", 42.2279, 42.3969, -71.1908, -70.9235);
        }

        System.out.println("Searching for: " + query.toString(field));

        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:br.andrew.lucene.testing.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(final String[] args) throws Exception {
    final 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);/*w  w w  .  j a v a  2 s.c  o m*/
    }

    String index = "index";
    String field = "contents";
    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 ("-field".equals(args[i])) {
            field = 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++;
        }
    }

    final IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
    final IndexSearcher searcher = new IndexSearcher(reader);
    final Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

    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"));
    }
    final QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
    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;
        }

        final Query query = parser.parse(line);
        System.out.println("Searching for: " + query.toString(field));

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

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

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

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

public static void export(NGIndex index, final NGSchema schema, final String outFile,
        final String outFileEncoding) throws IOException {
    if (index == null) {
        throw new NullPointerException("index");
    }//from   w  ww  .  java 2s . c  o m
    if (schema == null) {
        throw new NullPointerException("schema");
    }
    if (outFile == null) {
        throw new NullPointerException("outFile");
    }
    if (outFileEncoding == null) {
        throw new NullPointerException("outFileEncoding");
    }
    final Parameters parameters = schema.getParameters();
    final TreeMap<Integer, String> fields = new TreeMap<>();
    final IndexReader reader = index.getIndexSearcher().getIndexReader();
    final int maxdoc = reader.maxDoc();
    //final Bits liveDocs = MultiFields.getLiveDocs(reader);
    final Bits liveDocs = MultiBits.getLiveDocs(reader);
    final BufferedWriter writer = Files.newBufferedWriter(Paths.get(outFile), Charset.forName(outFileEncoding),
            StandardOpenOption.CREATE, StandardOpenOption.WRITE);

    boolean first = true;

    for (Map.Entry<Integer, br.bireme.ngrams.Field> entry : parameters.sfields.entrySet()) {
        fields.put(entry.getKey(), entry.getValue().name + NOT_NORMALIZED_FLD);
    }

    for (int docID = 0; docID < maxdoc; docID++) {
        if ((liveDocs != null) && (!liveDocs.get(docID)))
            continue;
        final Document doc = reader.document(docID);

        if (first) {
            first = false;
        } else {
            writer.newLine();
        }
        writer.append(doc2pipe(doc, fields));
    }
    writer.close();
    reader.close();
}

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

public static boolean test(final IndexReader ireader, final NGSchema schema)
        throws IOException, ParserConfigurationException, SAXException {
    final Parameters parameters = schema.getParameters();
    final Map<String, Field> fields = parameters.getNameFields();
    boolean bad = false;

    for (int id = 0; id < ireader.maxDoc(); id++) {
        final Document doc = ireader.document(id);

        if (id % 100000 == 0)
            System.out.println("+++ " + id);
        bad = badDocument(doc, fields);//from   w  ww  . j  a  va 2 s .c  om
        if (bad) {
            System.out.println("BAD DOCUMENT => id: " + doc.get("id"));
            break;
        }
    }
    ireader.close();

    return !bad;
}

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  ww w .  ja v  a 2s.c  o 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

/**
 * Search corresponding GeoName for each location entity
 *
 * @param count Number of results for one locations
 * @param querystr it's the NER actually
 *
 * @return HashMap each name has a list of resolved entities
 * @throws IOException/*from  w w  w . j  a  va  2s .c  o m*/
 * @throws RuntimeException
 */
public HashMap<String, List<Location>> searchGeoName(String indexerPath, List<String> locationNameEntities,
        int count) throws IOException {

    if (locationNameEntities.size() == 0 || locationNameEntities.get(0).length() == 0) {
        return new HashMap<String, List<Location>>();
    }
    IndexReader reader = createIndexReader(indexerPath);
    HashMap<String, List<Location>> resolvedEntities = resolveEntities(locationNameEntities, count, reader);
    reader.close();
    return resolvedEntities;

}