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.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

/**
 * @return false if the index entry was not updated because it
 * was already current; true otherwise.//from  w  w  w . j  av  a2 s  . co m
 */
public boolean addToIndex(final App app) throws IOException {

    boolean needNewEntry = true;

    String key = getKey(app);
    String url = app.getURL().toExternalForm();
    String title = app.getTitle();
    String description = app.getDescription();

    IndexReader reader = IndexReader.open(indexDir);
    TermDocs termDocs = reader.termDocs(new Term("key", key));
    while (termDocs.next()) {
        Document existingDoc = reader.document(termDocs.doc());
        if (areEqual("app", existingDoc.get("class")) && areEqual(url, existingDoc.get("url"))
                && areEqual(title, existingDoc.get("title"))
                && areEqual(description, existingDoc.get("description"))) {
            needNewEntry = false;
        }
    }
    termDocs.close();
    reader.close();

    if (needNewEntry) {
        Document newDoc = new Document();
        newDoc.add(new Field("key", key, Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("class", "app", Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("url", url, Field.Store.YES, Field.Index.TOKENIZED));
        if (title != null)
            newDoc.add(new Field("title", title, Field.Store.YES, Field.Index.TOKENIZED));
        if (description != null)
            newDoc.add(new Field("description", description, Field.Store.YES, Field.Index.TOKENIZED));

        IndexWriter writer = null;
        try {
            writer = new IndexWriter(indexDir, analyzer, false);
            writer.deleteDocuments(new Term("key", key)); // Delete old entry, if present
            writer.addDocument(newDoc);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (Exception e) {
                }
            ;
        }

        log.trace(String.format("Indexed app[url=%s,title=%s,description=%s]", url, title, description));
    }

    return needNewEntry;
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

/**
 * @return false if the index entry was not updated because it
 * was already current; true otherwise.//from w  w w.j a  v  a2s.co m
 */
public boolean addToIndex(final Group group) throws IOException {

    boolean needNewEntry = true;

    String key = getKey(group);
    String handle = group.getHandle();
    String name = group.getName();
    String description = group.getDescription();

    IndexReader reader = IndexReader.open(indexDir);
    TermDocs termDocs = reader.termDocs(new Term("key", key));
    while (termDocs.next()) {
        Document existingDoc = reader.document(termDocs.doc());
        if (areEqual("group", existingDoc.get("class")) && areEqual(handle, existingDoc.get("handle"))
                && areEqual(name, existingDoc.get("name"))
                && areEqual(description, existingDoc.get("description"))) {
            needNewEntry = false;
        }
    }
    termDocs.close();
    reader.close();

    if (needNewEntry) {
        Document newDoc = new Document();
        newDoc.add(new Field("key", key, Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("class", "group", Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("handle", handle, Field.Store.YES, Field.Index.TOKENIZED));
        newDoc.add(new Field("name", name, Field.Store.YES, Field.Index.TOKENIZED));
        if (description != null)
            newDoc.add(new Field("description", description, Field.Store.YES, Field.Index.TOKENIZED));

        IndexWriter writer = null;
        try {
            writer = new IndexWriter(indexDir, analyzer, false);
            writer.deleteDocuments(new Term("key", key)); // Delete old entry, if present
            writer.addDocument(newDoc);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (Exception e) {
                }
            ;
        }

        log.trace(String.format("Indexed group[handle=%s,name=%s,description=%s]", name, handle, description));
    }

    return needNewEntry;
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

/**
 * @return false if the index entry was not updated because it
 * was already current; true otherwise.//from  ww w  .ja  v a  2 s .co  m
 */
public boolean addToIndex(final Profile profile) throws IOException {

    boolean needNewEntry = true;

    String key = getKey(profile);
    String userId = profile.getUserId();
    String firstName = profile.getFirstName();
    String middleName = profile.getMiddleName();
    String lastName = profile.getLastName();
    String nickName = profile.getNickName();
    String primaryEmail = profile.getPrimaryEmail();
    String displayName = profile.getDisplayName();

    IndexReader reader = IndexReader.open(indexDir);
    TermDocs termDocs = reader.termDocs(new Term("key", key));
    while (termDocs.next()) {
        Document existingDoc = reader.document(termDocs.doc());
        if (areEqual("profile", existingDoc.get("class")) && areEqual(userId, existingDoc.get("userId"))
                && areEqual(firstName, existingDoc.get("firstName"))
                && areEqual(middleName, existingDoc.get("middleName"))
                && areEqual(lastName, existingDoc.get("lastName"))
                && areEqual(nickName, existingDoc.get("nickName"))
                && areEqual(primaryEmail, existingDoc.get("primaryEmail"))
                && areEqual(displayName, existingDoc.get("displayName"))) {
            needNewEntry = false;
        }
    }
    termDocs.close();
    reader.close();

    if (needNewEntry) {
        Document newDoc = new Document();
        newDoc.add(new Field("key", key, Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("class", "profile", Field.Store.YES, Field.Index.UN_TOKENIZED));
        newDoc.add(new Field("userId", userId, Field.Store.YES, Field.Index.UN_TOKENIZED));
        if (firstName != null)
            newDoc.add(new Field("firstName", firstName, Field.Store.YES, Field.Index.TOKENIZED));
        if (middleName != null)
            newDoc.add(new Field("middleName", middleName, Field.Store.YES, Field.Index.TOKENIZED));
        if (lastName != null)
            newDoc.add(new Field("lastName", lastName, Field.Store.YES, Field.Index.TOKENIZED));
        if (nickName != null)
            newDoc.add(new Field("nickName", nickName, Field.Store.YES, Field.Index.TOKENIZED));
        if (primaryEmail != null)
            newDoc.add(new Field("primaryEmail", primaryEmail, Field.Store.YES, Field.Index.UN_TOKENIZED));
        if (displayName != null)
            newDoc.add(new Field("displayName", displayName, Field.Store.YES, Field.Index.TOKENIZED));

        IndexWriter writer = null;
        try {
            writer = new IndexWriter(indexDir, analyzer, false);
            writer.deleteDocuments(new Term("key", key)); // Delete old entry, if present
            writer.addDocument(newDoc);
        } finally {
            if (writer != null)
                try {
                    writer.close();
                } catch (Exception e) {
                }
            ;
        }

        log.trace(String.format(
                "Indexed profile[userId=%s,firstName=%s,lastName=%s,nickName=%s,primaryEmail=%s,displayName=%s]",
                userId, firstName, lastName, nickName, primaryEmail, displayName));
    }

    return needNewEntry;
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public List<App> getApps(AppManager appManager, int offset, int length, String queryString)
        throws SocialSiteException {
    IndexReader reader = null;
    Searcher searcher = null;//from   w ww  .  j a v a2s  .c o m
    try {
        reader = IndexReader.open(indexDir);
        searcher = new IndexSearcher(reader);
        Hits hits = getAppHits(reader, searcher, queryString);
        int endIndex = ((length != -1) ? Math.min(offset + length, hits.length()) : hits.length());
        int numResults = Math.max(endIndex - offset, 0);
        List<App> apps = new ArrayList<App>(numResults);
        for (int i = offset; i < endIndex; i++) {
            Document doc = hits.doc(i);
            apps.add(appManager.getAppByURL(new URL(doc.get("url"))));
        }
        return apps;
    } catch (Exception e) {
        throw ((e instanceof SocialSiteException) ? (SocialSiteException) (e) : new SocialSiteException(e));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        ;
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public int getTotalApps(String queryString) throws SocialSiteException {
    IndexReader reader = null;
    Searcher searcher = null;/*from   w  ww .  j a  v a2s.  c  o  m*/
    try {
        reader = IndexReader.open(indexDir);
        searcher = new IndexSearcher(reader);
        Hits hits = getAppHits(reader, searcher, queryString);
        return hits.length();
    } catch (Exception e) {
        throw ((e instanceof SocialSiteException) ? (SocialSiteException) (e) : new SocialSiteException(e));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        ;
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public List<Group> getGroups(GroupManager groupManager, int offset, int length, String queryString)
        throws SocialSiteException {
    IndexReader reader = null;
    Searcher searcher = null;/*from   w w w .j a v  a 2  s  .  co m*/
    try {
        reader = IndexReader.open(indexDir);
        searcher = new IndexSearcher(reader);
        Hits hits = getGroupHits(reader, searcher, queryString);
        int endIndex = ((length != -1) ? Math.min(offset + length, hits.length()) : hits.length());
        int numResults = Math.max(endIndex - offset, 0);
        List<Group> groups = new ArrayList<Group>(numResults);
        for (int i = offset; i < endIndex; i++) {
            Document doc = hits.doc(i);
            groups.add(groupManager.getGroupByHandle(doc.get("handle")));
        }
        return groups;
    } catch (Exception e) {
        throw ((e instanceof SocialSiteException) ? (SocialSiteException) (e) : new SocialSiteException(e));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        ;
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public int getTotalGroups(String queryString) throws SocialSiteException {
    IndexReader reader = null;
    Searcher searcher = null;//from w ww  .ja v a  2s .  co  m
    try {
        reader = IndexReader.open(indexDir);
        searcher = new IndexSearcher(reader);
        Hits hits = getGroupHits(reader, searcher, queryString);
        return hits.length();
    } catch (Exception e) {
        throw ((e instanceof SocialSiteException) ? (SocialSiteException) (e) : new SocialSiteException(e));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        ;
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public List<Profile> getProfiles(ProfileManager profileManager, int offset, int length, String queryString)
        throws SocialSiteException {
    IndexReader reader = null;
    Searcher searcher = null;//from  w w w  .j  a v  a2 s  . c  om
    try {
        reader = IndexReader.open(indexDir);
        searcher = new IndexSearcher(reader);
        Hits hits = getProfileHits(reader, searcher, queryString);
        int endIndex = ((length != -1) ? Math.min(offset + length, hits.length()) : hits.length());
        int numResults = Math.max(endIndex - offset, 0);
        List<Profile> profiles = new ArrayList<Profile>(numResults);
        for (int i = offset; i < endIndex; i++) {
            Document doc = hits.doc(i);
            Profile profile = profileManager.getProfileByUserId(doc.get("userId"));
            if (profile != null) {
                profiles.add(profile);
            } else {
                String msg = String.format("Could not find profile for userId: %s", doc.get("userId"));
                log.warn(msg);
            }
        }
        return profiles;
    } catch (Exception e) {
        throw ((e instanceof SocialSiteException) ? (SocialSiteException) (e) : new SocialSiteException(e));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        ;
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:com.sun.socialsite.business.impl.LuceneSearchManagerImpl.java

License:Open Source License

public int getTotalProfiles(String queryString) throws SocialSiteException {
    IndexReader reader = null;
    Searcher searcher = null;//  w w  w . j a  v a2s. c om
    try {
        reader = IndexReader.open(indexDir);
        searcher = new IndexSearcher(reader);
        Hits hits = getProfileHits(reader, searcher, queryString);
        return hits.length();
    } catch (Exception e) {
        throw ((e instanceof SocialSiteException) ? (SocialSiteException) (e) : new SocialSiteException(e));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        ;
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
        ;
    }
}

From source file:com.sxc.lucene.index.IndexingTest.java

License:Apache License

protected int getHitCount(String fieldName, String searchString) throws IOException {
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader); // 4
    Term t = new Term(fieldName, searchString);
    Query query = new TermQuery(t); // 5
    int hitCount = TestUtil.hitCount(searcher, query); // 6
    reader.close();
    return hitCount;
}