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:com.dreamerpartner.codereview.lucene.SearchHelper.java

License:Apache License

/**
 * ?/*w  w w.j  a  va  2 s.  c  om*/
 * @param module ?
 * @param fields ?
 * @param queryStr ?
 * @param sort
 * @param pageNo
 * @param pageSize
 * @return
 */
@SuppressWarnings("deprecation")
public static PageBean<Document> search(String module, String[] fields, String[] queryStr, Sort sort,
        int pageNo, int pageSize) {
    if (ArrayUtils.isEmpty(fields) || ArrayUtils.isEmpty(queryStr))
        return null;

    IndexReader reader = null;
    try {
        reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module))));
        IndexSearcher searcher = new IndexSearcher(reader);

        BooleanClause.Occur[] flags = new BooleanClause.Occur[fields.length];
        for (int i = 0; i < fields.length; i++) {
            flags[i] = BooleanClause.Occur.SHOULD;
        }
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0);
        Query query = MultiFieldQueryParser.parse(Version.LUCENE_4_10_0, queryStr, fields, flags, analyzer);
        //
        return doPagingSearch(searcher, query, sort, pageNo, pageSize);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java

License:Apache License

/**
 *  /* w  w w .j  a  v  a 2s  .  c o m*/
 * @param groupField 
 * @param searchField 
 * @param searchStr 
 * @param pageNo
 * @param pageSize
 * @param orderField ?
 * @param orderFieldType ?
 * @param desc ? ??
 * @return
 */
@SuppressWarnings("deprecation")
public static Map<String, List<Document>> group(String module, String groupField, String searchField,
        String searchStr, int pageNo, int pageSize, String orderField, Type orderFieldType, boolean desc) {
    Map<String, List<Document>> result = new LinkedHashMap<String, List<Document>>(10);
    IndexReader reader = null;
    try {
        reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module))));
        IndexSearcher indexSearcher = new IndexSearcher(reader);
        GroupingSearch groupingSearch = new GroupingSearch(groupField);
        Sort sort = new Sort(new SortField(orderField, orderFieldType, desc));
        groupingSearch.setGroupSort(sort);
        groupingSearch.setSortWithinGroup(sort);
        groupingSearch.setFillSortFields(true);
        groupingSearch.setCachingInMB(4.0, true);
        groupingSearch.setAllGroups(true);
        //groupingSearch.setAllGroupHeads(true);
        groupingSearch.setGroupDocsLimit(pageSize);

        QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, searchField,
                new StandardAnalyzer(Version.LUCENE_4_10_0));
        Query query = parser.parse(searchStr);

        TopGroups<BytesRef> groupResult = groupingSearch.search(indexSearcher, query, (pageNo - 1) * pageSize,
                pageSize);
        System.out.println("?" + groupResult.totalHitCount + ", ?"
                + groupResult.groups.length);

        List<Document> groupData = null;
        for (GroupDocs<BytesRef> groupDocs : groupResult.groups) {
            groupData = new ArrayList<Document>(pageSize);
            String groupName = groupDocs.groupValue.utf8ToString();
            for (ScoreDoc scoreDoc : groupDocs.scoreDocs) {
                groupData.add(indexSearcher.doc(scoreDoc.doc));
            }
            result.put(groupName, groupData);
            groupData = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return result;
}

From source file:com.dreamerpartner.codereview.lucene.SearchHelper.java

License:Apache License

/** Simple command-line based search demo. */
@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
    String module = "test";
    String field = "contents";
    String queryString = "produce";
    int pageNo = 1;
    int pageSize = 10;
    Sort sort = new Sort(new SortField("createTime", Type.LONG));

    IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(LuceneUtil.getIndexPath(module))));
    IndexSearcher searcher = new IndexSearcher(reader);
    // :Post-Release-Update-Version.LUCENE_XY:
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_10_0);

    // :Post-Release-Update-Version.LUCENE_XY:
    QueryParser parser = new QueryParser(Version.LUCENE_4_10_0, field, analyzer);
    Query query = parser.parse(queryString);
    System.out.println("Searching for: " + query.toString(field));

    ////from  w  w  w  . j  a  v  a 2 s  .  co  m
    PageBean<Document> docs = doPagingSearch(searcher, query, sort, pageNo, pageSize);
    System.out.println(JsonUtil.toString(docs));
    reader.close();
}

From source file:com.duroty.lucene.bookmark.indexer.BookmarkIndexer.java

License:Open Source License

/**
 * DOCUMENT ME!/*from ww w . ja  v  a 2s.c o  m*/
 *
 * @param path DOCUMENT ME!
 * @param field DOCUMENT ME!
 * @param id DOCUMENT ME!
 *
 * @throws Exception DOCUMENT ME!
 */
public void deleteDocument(String path, String field, String id) throws Exception {
    String simplePath = null;
    String optimizedPath = null;

    if (!path.endsWith(File.separator)) {
        simplePath = path + File.separator + SIMPLE_PATH_NAME + File.separator;
        optimizedPath = path + File.separator + OPTIMIZED_PATH_NAME + File.separator;
    } else {
        simplePath = path + SIMPLE_PATH_NAME + File.separator;
        optimizedPath = path + OPTIMIZED_PATH_NAME + File.separator;
    }

    IndexReader reader = null;

    try {
        //La primera cosa que faig al borrar es controlar els simple paths
        File simple = new File(simplePath + id);

        //Miro si est lock
        boolean lock;

        try {
            lock = FileUtilities.isLockDir(simple);

            if (!lock) {
                FileUtils.deleteDirectory(simple);

                return;
            } else {
                Thread.sleep(sleepTime);

                if (countDelete > 5) {
                    throw new Exception("The index lucene MainIndexer is locked in delete document");
                }

                countDelete++;

                deleteDocument(path, field, id);
            }
        } catch (IOException ioe) {
            //vol dir que no existeix i per tant he de mirar al optimitzat
        }

        if (IndexReader.isLocked(optimizedPath)) {
            Thread.sleep(sleepTime);

            if (countDelete > 5) {
                throw new Exception("The index lucene MainIndexer is locked in delete document");
            }

            countDelete++;

            deleteDocument(path, field, id);

            return;
        }

        //En primer lloc borro del index optimitzat
        reader = IndexReader.open(optimizedPath);
        reader.deleteDocuments(new Term(field, id));
        reader.close();
        reader = null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.ecyrd.jspwiki.search.LuceneSearchProvider.java

License:Apache License

/**
 *  {@inheritDoc}/*from  w  ww  .ja  v a2s . co m*/
 */
public void pageRemoved(WikiPage page) {
    try {
        // Must first remove existing version of page.
        IndexReader reader = IndexReader.open(m_luceneDirectory);
        reader.deleteDocuments(new Term(LUCENE_ID, page.getName()));
        reader.close();
    } catch (IOException e) {
        log.error("Unable to update page '" + page.getName() + "' from Lucene index", e);
    }
}

From source file:com.eden.lucene.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);/*  w  w w  . ja  va 2 s . c  o  m*/
    }

    String basePath = "D:/test/lucene";

    String index = basePath + "/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++;
        }
      }*/

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

    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_46, 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);
        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, null, 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:com.edu.lucene.SearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
    String usage = "Usage:search [-index dir] [-field f] [-repeat n] [-queries file] [-raw] [-norms field] [-paging hitsPerPage]";
    usage += "\n\tSpecify 'false' for hitsPerPage to use streaming instead of paging search.";
    if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
        System.out.println(usage);
        System.exit(0);/* w ww .  ja  va 2s  .  c om*/
    }

    String index = "index";
    String field = "contents";
    String queries = null;
    int repeat = 0;
    boolean raw = false;
    String normsField = null;
    boolean paging = true;
    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 ("-repeat".equals(args[i])) {
            repeat = Integer.parseInt(args[i + 1]);
            i++;
        } else if ("-raw".equals(args[i])) {
            raw = true;
        } else if ("-norms".equals(args[i])) {
            normsField = args[i + 1];
            i++;
        } else if ("-paging".equals(args[i])) {
            if (args[i + 1].equals("false")) {
                paging = false;
            } else {
                hitsPerPage = Integer.parseInt(args[i + 1]);
                if (hitsPerPage == 0) {
                    paging = false;
                }
            }
            i++;
        }
    }

    IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)), true); // only searching, so
    // read-only=true

    if (normsField != null)
        reader = new OneNormsReader(reader, normsField);

    Searcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

    BufferedReader in = null;
    if (queries != null) {
        in = new BufferedReader(new FileReader(queries));
    } else {
        in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
    }
    QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, analyzer);
    while (true) {
        if (queries == null) // prompt the user
            System.out.println("Enter query: ");

        String line = in.readLine();

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

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

        Query query = parser.parse(line);
        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, null, 100);
            }
            Date end = new Date();
            System.out.println("Time: " + (end.getTime() - start.getTime()) + "ms");
        }

        if (paging) {

            doPagingSearch(in, searcher, query, hitsPerPage, raw, queries == null);
        } else {
            doStreamingSearch(searcher, query);
        }
    }
    reader.close();
}

From source file:com.ephesoft.dcma.fuzzydb.FuzzyLuceneEngine.java

License:Open Source License

public void cleanUpResource(IndexReader reader) {
    if (reader != null) {
        try {/*  w w  w  .  j  av a2  s .  com*/
            reader.close();
        } catch (IOException e) {
            LOGGER.info("There was a problem in closing the Index reader.");
        }
    }
}

From source file:com.esri.gpt.catalog.arcgis.agportal.publication.ItemInfoLuceneAdapter.java

License:Apache License

/**
 * Returns the field values associated with a document
 * /*from   w ww.j a  va 2s  . c  o  m*/
 * @param context
 *          the operation context
 * @param fields
 *          array of fields to fetch
 * @param uuid
 *          the document uuid
 * @return the field values (null if not found)
 * @throws CorruptIndexException
 *           if the index is corrupt
 * @throws IOException
 *           if an I/O exception occurs
 */
public ESRI_ItemInformation makeItemInfoByUuid(RequestContext context, String[] fields, String uuid)
        throws CorruptIndexException, IOException {
    TermDocs termDocs = null;
    IndexReader reader = null;
    MapFieldSelector selector = null;
    try {
        uuid = Val.chkStr(uuid);
        if (uuid.length() > 0) {
            IndexSearcher srch = this.getSearcher(context);
            reader = srch.getIndexReader();
            if (fields != null) {
                selector = new MapFieldSelector(fields);
            }
            termDocs = reader.termDocs();
            termDocs.seek(new Term(Storeables.FIELD_UUID, uuid));
            if (termDocs.next()) {
                Document document = reader.document(termDocs.doc(), selector);
                List<Fieldable> flds = document.getFields();

                ESRI_ItemInformation itemInfo = new ESRI_ItemInformation();
                itemInfo.setType("Map Service");
                itemInfo.setTags(Arrays.asList(new String[] { "ArcGIS", "Server map", "service" }));

                Envelope extent = new Envelope();
                for (Fieldable fld : flds) {
                    String fieldName = fld.name();
                    String[] vals = document.getValues(fieldName);
                    if (fieldName.contains("uuid")) {
                        itemInfo.setId(vals[0].replaceAll("^\\{|\\}$|\\-", ""));
                    } else if (fieldName.contains("title")) {
                        itemInfo.setTitle(vals[0]);
                        itemInfo.setName(vals[0]);
                    } else if (fieldName.contains("resource.url")) {
                        itemInfo.setUrl(vals[0]);
                        String type = Val.chkStr(guessServiceTypeFromUrl(vals[0]));
                        if (type.length() > 0) {
                            itemInfo.setType(type);
                        }
                    } else if (fieldName.contains("contentType")) {
                    } else if (fieldName.contains("keywords")) {
                        itemInfo.setTypeKeywords(Arrays.asList(vals));
                        itemInfo.setTags(Arrays.asList(vals));
                    } else if (fieldName.contains("dataTheme")) {
                        itemInfo.setTags(Arrays.asList(vals));
                    } else if (fieldName.contains("abstract")) {
                        itemInfo.setDescription(vals[0]);
                        //                  } else if (fieldName.contains("xml")) {
                        //                     itemInfo.setXml(vals[0]);
                    } else if (fieldName.contains("minx")) {
                        extent.setMinX(vals[0]);
                    } else if (fieldName.contains("miny")) {
                        extent.setMinY(vals[0]);
                    } else if (fieldName.contains("maxx")) {
                        extent.setMaxX(vals[0]);
                    } else if (fieldName.contains("maxy")) {
                        extent.setMaxY(vals[0]);
                    } else if (fieldName.contains("thumbnail.url")) {
                        itemInfo.setThumbnailUrl(vals[0]);
                    }
                }
                itemInfo.setExtent(extent);
                return itemInfo;
            }
        }
    } finally {
        try {
            if (termDocs != null)
                termDocs.close();
            if (reader != null)
                reader.close();
        } catch (Exception ef) {
        }
    }
    return null;
}

From source file:com.esri.gpt.server.assertion.index.AsnBaseIndexAdapter.java

License:Apache License

/**
 * Closes an index reader.//  w  w w  . j a  v  a  2  s.co m
 * @param reader the reader to close
 */
public void closeReader(IndexReader reader) {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "IndexReader failed to close.", e);
        }
    }
}