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:com.codecrate.shard.search.HibernateObjectSearcher.java

License:Apache License

public Collection search(Class target, String query) {
    Searcher searcher = null;/*from   w w  w  .  j  a  v  a  2 s  .c  o m*/
    Collection results = new ArrayList();

    try {
        searcher = new IndexSearcher(directory);
        BooleanQuery masterQuery = new BooleanQuery();
        masterQuery.add(new TermQuery(new Term(FIELD_CLASS, target.getName())), REQUIRED, NOT_PROHIBITED);
        masterQuery.add(QueryParser.parse(makeWildcardQuery(query), FIELD_TEXT, analyzer), REQUIRED,
                NOT_PROHIBITED);

        LOG.debug("Searching for " + masterQuery);

        Hits hits = searcher.search(masterQuery);
        LOG.debug("Found " + hits.length() + " matches");
        for (int x = 0; x < hits.length(); x++) {
            Document document = hits.doc(x);
            String id = document.getField(FIELD_ID).stringValue();
            Object result = this.get(target, id);
            if (null == result) {
                LOG.warn("Search index is out of synch with database.  Unable to find object " + target
                        + " with id " + id);
            } else {
                results.add(result);
            }
        }

    } catch (Exception e) {
        LOG.error("Error searching directory " + directory + " for type " + target + " using query " + query,
                e);
    } finally {
        closeSearcher(searcher);
    }

    return results;
}

From source file:com.codenvy.test.lucene.DeleteFilesWithSameName.java

License:Open Source License

public static void searchAndPrintResult(Path indexPath) throws Exception {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(indexPath));
    IndexSearcher searcher = new IndexSearcher(reader);

    Query query = new PrefixQuery(new Term(PATH, filesDirPath + "/File1"));
    TopDocs topDocs = searcher.search(query, 100);

    System.out.println();// w ww .j  a  va  2s.co  m
    System.out.println("=========================== search =================================");
    final String[] result = new String[topDocs.scoreDocs.length];
    for (int i = 0, length = result.length; i < length; i++) {
        result[i] = searcher.doc(topDocs.scoreDocs[i].doc).getField(PATH).stringValue();
        System.out.println(result[i]);
    }

    reader.close();
}

From source file:com.codeReading.core.opengrok.SearchHelper.java

License:Open Source License

/**
 * Create the searcher to use wrt. to currently set parameters and the given
 * projects. Does not produce any {@link #redirect} link. It also does
 * nothing if {@link #redirect} or {@link #errorMsg} have a
 * none-{@code null} value. <p> Parameters which should be populated/set at
 * this time: <ul> <li>{@link #builder}</li> <li>{@link #dataRoot}</li>
 * <li>{@link #order} (falls back to relevance if unset)</li>
 * <li>{@link #parallel} (default: false)</li> </ul> Populates/sets: <ul>
 * <li>{@link #query}</li> <li>{@link #searcher}</li> <li>{@link #sort}</li>
 * <li>{@link #projects}</li> <li>{@link #errorMsg} if an error occurs</li>
 * </ul>/*from   w  w w .j  a va2  s . c om*/
 *
 * @param projects project to use query. If empty, a none-project opengrok
 * setup is assumed (i.e. DATA_ROOT/index will be used instead of possible
 * multiple DATA_ROOT/$project/index).
 * @return this instance
 */
public SearchHelper prepareExec(SortedSet<String> projects) {
    if (redirect != null || errorMsg != null) {
        return this;
    }
    // the Query created by the QueryBuilder
    try {
        indexDir = new File(dataRoot, "index");
        query = builder.build();
        if (projects == null) {
            errorMsg = "No project selected!";
            return this;
        }
        this.projects = projects;
        if (projects.isEmpty()) {
            //no project setup
            FSDirectory dir = FSDirectory.open(indexDir);
            searcher = new IndexSearcher(DirectoryReader.open(dir));
        } else if (projects.size() == 1) {
            // just 1 project selected
            FSDirectory dir = FSDirectory.open(new File(indexDir, projects.first()));
            searcher = new IndexSearcher(DirectoryReader.open(dir));
        } else {
            //more projects                                
            IndexReader[] subreaders = new IndexReader[projects.size()];
            int ii = 0;
            //TODO might need to rewrite to Project instead of
            // String , need changes in projects.jspf too
            for (String proj : projects) {
                FSDirectory dir = FSDirectory.open(new File(indexDir, proj));
                subreaders[ii++] = DirectoryReader.open(dir);
            }
            MultiReader searchables = new MultiReader(subreaders, true);
            if (parallel) {
                int noThreads = 2 + (2 * Runtime.getRuntime().availableProcessors()); //TODO there might be a better way for counting this
                executor = Executors.newFixedThreadPool(noThreads);
            }
            searcher = parallel ? new IndexSearcher(searchables, executor) : new IndexSearcher(searchables);
        }
        // TODO check if below is somehow reusing sessions so we don't
        // requery again and again, I guess 2min timeout sessions could be
        // usefull, since you click on the next page within 2mins, if not,
        // then wait ;)
        switch (order) {
        case LASTMODIFIED:
            sort = new Sort(new SortField("date", SortField.Type.STRING, true));
            break;
        case BY_PATH:
            sort = new Sort(new SortField("fullpath", SortField.Type.STRING));
            break;
        default:
            sort = Sort.RELEVANCE;
            break;
        }
        checker = new DirectSpellChecker();
    } catch (ParseException e) {
        errorMsg = PARSE_ERROR_MSG + e.getMessage();
    } catch (FileNotFoundException e) {
        //          errorMsg = "Index database(s) not found: " + e.getMessage();
        errorMsg = "Index database(s) not found.";
    } catch (Exception e) {
        errorMsg = e.getMessage();
    }
    return this;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Searches the index for an attribute with a matching value.
 * /*from   w w  w.j  a v  a  2s .  c  o m*/
 * @param feature
 *            - the attribute to match when searching
 * @param value
 *            - the value to match when searching
 * @return a list of search results
 * @throws IllegalArgumentException
 *             if any parameters are null, or if the attribute is not marked
 *             with the search annotation
 * @throws IOException
 *             if there are issues reading the index
 */
public List<SearchResult> search(EStructuralFeature feature, String value)
        throws IllegalArgumentException, IOException {

    if (feature == null) {
        throw new IllegalArgumentException("Attribute cannot be null");
    }
    if (value == null) {
        throw new IllegalArgumentException("Value cannot be null");
    }

    List<SearchResult> rvalue = new ArrayList<SearchResult>();
    boolean tokenize = false;
    EAnnotation annotation = feature.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation != null) {
        if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) {
            tokenize = true;
        }
    } else {
        // Bail out early if this feature should not be indexed
        throw new IllegalArgumentException("Attribute is not annotated to be indexed");
    }

    String key = EMFIndexUtil.getKey(feature);
    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {

        Query query = null;

        if (tokenize) {
            QueryParser parser = new QueryParser(version, key, analyzer);
            query = parser.parse(value);
        } else {
            Term term = new Term(key, value);
            query = new TermQuery(term);
        }

        ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs;

        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = searcher.doc(hits[i].doc);
            SearchResult result = new SearchResult(hitDoc);
            rvalue.add(result);
            logger.debug(hitDoc.toString());
        }
    } catch (ParseException e) {
        logger.error(e.getMessage());
    } finally {
        reader.close();
    }
    return rvalue;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Searches the index for an attribute with a matching value and a matching
 * eclass/*from  w  w w  .  ja  v a  2s. c  o m*/
 * 
 * @param eclass
 *            - the EClass to match when searching
 * @param attr
 *            - the EAttribute to match when searching
 * @param value
 *            - the value to match when searching
 * @return a list of search results
 * @throws IllegalArgumentException
 *             if any parameters are null, or if the attribute is not marked
 *             with the search annotation
 * @throws IOException
 *             if there are issues reading the index
 */
public List<SearchResult> search(EClass eclass, EAttribute attr, String value)
        throws IllegalArgumentException, IOException {

    if (eclass == null) {
        throw new IllegalArgumentException("EClass cannot be null");
    }
    if (attr == null) {
        throw new IllegalArgumentException("Attribute cannot be null");
    }
    if (value == null) {
        throw new IllegalArgumentException("Value cannot be null");
    }

    EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation == null) {
        // Bail out early if this feature should not be indexed
        throw new IllegalArgumentException("EClass is not annotated to be indexed");
    }
    List<SearchResult> rvalue = new ArrayList<SearchResult>();
    boolean tokenize = false;
    annotation = attr.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation != null) {
        if (annotation.getDetails().containsKey(EMFIndexUtil.TOKENIZE_KEY)) {
            tokenize = true;
        }
    } else {
        // Bail out early if this feature should not be indexed
        throw new IllegalArgumentException("Attribute is not annotated to be indexed");
    }

    String key = EMFIndexUtil.getKey(attr);

    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {

        BooleanQuery bquery = new BooleanQuery();

        TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName()));
        bquery.add(classQuery, Occur.MUST);

        Query query = null;
        if (tokenize) {
            QueryParser parser = new QueryParser(version, key, analyzer);
            query = parser.parse(value);
        } else {
            Term term = new Term(key, value);
            query = new TermQuery(term);
        }
        bquery.add(query, Occur.MUST);

        ScoreDoc[] hits = searcher.search(bquery, null, MAX_SEARCH_RESULT).scoreDocs;
        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = searcher.doc(hits[i].doc);
            SearchResult result = new SearchResult(hitDoc);
            rvalue.add(result);
            logger.debug(hitDoc.toString());
        }
    } catch (ParseException e) {
        logger.error(e.getMessage());
    } finally {
        reader.close();
    }
    return rvalue;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Searches the index for a matching eclass
 * //from  ww w .j av a2  s.c  om
 * @param eclass
 *            - the EClass to match when searching
 * @return a list of search results
 * @throws IllegalArgumentException
 *             if the eclass is null or is not annotated for indexing
 * @throws IOException
 *             if there are issues reading the index
 */
public List<SearchResult> search(EClass eclass) throws IllegalArgumentException, IOException {

    if (eclass == null) {
        throw new IllegalArgumentException("EClass cannot be null");
    }
    EAnnotation annotation = eclass.getEAnnotation(EMFIndexUtil.SOURCE);
    if (annotation == null) {
        throw new IllegalArgumentException("EClass is not annotated for indexing");
    }
    List<SearchResult> rvalue = new ArrayList<SearchResult>();

    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {
        TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, eclass.getName()));

        ScoreDoc[] hits = searcher.search(classQuery, null, MAX_SEARCH_RESULT).scoreDocs;
        // Iterate through the results:
        for (int i = 0; i < hits.length; i++) {
            Document hitDoc = searcher.doc(hits[i].doc);
            SearchResult result = new SearchResult(hitDoc);
            rvalue.add(result);
            logger.debug(hitDoc.toString());
        }
    } finally {
        reader.close();
    }
    return rvalue;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Finds a document matching the object and returns the query used
 * /*from  w w w .ja va2 s . co m*/
 * @param object
 *            to search for in the index
 * @return the query that returns the document
 * @throws IllegalArgumentException
 *             if the object is null
 * @throws IOException
 *             if there are problems searching the index
 */
protected Query findDocument(EObject object) throws IllegalArgumentException, IOException {

    if (object == null) {
        throw new IllegalArgumentException("EObject cannot be null");
    }

    Query rvalue = null;

    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {

        BooleanQuery bquery = new BooleanQuery();

        TermQuery classQuery = new TermQuery(new Term(EMFIndexUtil.ETYPE_KEY, object.eClass().getName()));
        bquery.add(classQuery, Occur.MUST);

        TermQuery uriQuery = new TermQuery(
                new Term(EMFIndexUtil.DOCUMENT_URI_KEY, object.eResource().getURI().toString()));
        bquery.add(uriQuery, Occur.MUST);

        TermQuery fragmentQuery = new TermQuery(
                new Term(EMFIndexUtil.FRAGMENT_URI_KEY, object.eResource().getURIFragment(object)));
        bquery.add(fragmentQuery, Occur.MUST);

        ScoreDoc[] hits = searcher.search(bquery, null, 1).scoreDocs;
        // Iterate through the results:
        if (hits.length > 0) {
            rvalue = bquery;
        }
    } finally {
        reader.close();
    }
    return rvalue;
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Deletes a document matching the EObject
 * // www . j  a  v  a  2 s. c  o  m
 * @param obj
 * @throws IllegalArgumentException
 *             if EObject is null or the EObject is not contained in a
 *             resource
 * @throws IOException
 *             if there are issues saving the index
 */
public void deleteDocument(EObject obj) throws IllegalArgumentException, IOException {
    if (obj == null) {
        throw new IllegalArgumentException("EObject cannot be null");
    }
    if (obj.eResource() == null) {
        throw new IllegalArgumentException("EObject must be contained in a Resource");
    }

    Query query = findDocument(obj);
    if (query != null) {
        logger.debug("Deleting existing index for {}:{}", obj.eResource().getURI(),
                obj.eResource().getURIFragment(obj));
        writer.deleteDocuments(query);
        if (!holdCommits) {
            writer.commit();
        }
    }

    DirectoryReader reader = DirectoryReader.open(fsDir);

    ArrayList<String> names = new ArrayList<String>();
    for (AtomicReaderContext context : reader.leaves()) {
        for (FieldInfo fi : context.reader().getFieldInfos()) {
            if (!names.contains(fi.name)) {
                names.add(fi.name);
            }

        }
    }
    if (names.size() > 0) {
        MultiFieldQueryParser parser = new MultiFieldQueryParser(version, names.toArray(new String[] {}),
                analyzer);
        try {
            query = parser.parse(obj.eResource().getURIFragment(obj));
            IndexSearcher searcher = new IndexSearcher(reader);

            ScoreDoc[] hits = searcher.search(query, null, MAX_SEARCH_RESULT).scoreDocs;
            for (ScoreDoc hit : hits) {
                Document hitDoc = searcher.doc(hit.doc);
                logger.debug("Hanging reference in: {}",
                        hitDoc.getField(EMFIndexUtil.DOCUMENT_URI_KEY).stringValue());
            }
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }

    }
}

From source file:com.cohesionforce.search.EMFIndex.java

License:Open Source License

/**
 * Deletes a document matching the URI//from  w  w  w .j a  v a2s .co m
 * 
 * @param uri - the URI of the object reference to delete
 * @throws IllegalArgumentException
 *             if the uri is null
 * @throws IOException
 *             if there are issues saving the index
 */
public void deleteDocument(URI uri) throws IllegalArgumentException, IOException {
    if (uri == null) {
        throw new IllegalArgumentException("URI cannot be null");
    }

    DirectoryReader reader = DirectoryReader.open(fsDir);
    IndexSearcher searcher = new IndexSearcher(reader);
    try {

        TermQuery uriQuery = new TermQuery(new Term(EMFIndexUtil.DOCUMENT_URI_KEY, uri.toString()));

        ScoreDoc[] hits = searcher.search(uriQuery, null, 1).scoreDocs;
        // Iterate through the results:
        if (hits.length > 0) {
            writer.deleteDocuments(uriQuery);
            if (!holdCommits) {
                writer.commit();
            }
        }
    } finally {
        reader.close();
    }
}

From source file:com.common.search.IKAnalyzerDemo.java

License:Apache License

public static void main(String[] args) {
    // Lucene Document
    String fieldName = "text";
    // //from w  w w . j  a  v  a  2  s.c  o m
    String text = "IK Analyzer ";
    // IKAnalyzer
    Analyzer analyzer = new IKAnalyzer();
    Directory directory = null;
    IndexWriter iwriter = null;
    IndexReader ireader = null;
    IndexSearcher isearcher = null;
    try {
        // 
        directory = new RAMDirectory();
        // IndexWriterConfig
        IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer);
        iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
        iwriter = new IndexWriter(directory, iwConfig);
        // 
        Document doc = new Document();
        doc.add(new Field("ID", "10000", Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.add(new Field(fieldName, text, Field.Store.YES, Field.Index.ANALYZED));
        iwriter.addDocument(doc);
        iwriter.close();
        // **********************************
        // 
        ireader = IndexReader.open(directory);
        isearcher = new IndexSearcher(ireader);
        String keyword = "";
        // QueryParserQuery
        QueryParser qp = new QueryParser(Version.LUCENE_34, fieldName, analyzer);
        qp.setDefaultOperator(QueryParser.AND_OPERATOR);
        Query query = qp.parse(keyword);
        // 5
        TopDocs topDocs = isearcher.search(query, 5);
        System.out.println(" " + topDocs.totalHits);
        // 
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.totalHits; i++) {
            Document targetDoc = isearcher.doc(scoreDocs[i].doc);
            System.out.println(" " + targetDoc.toString());
        }
    } catch (CorruptIndexException e) {
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    } finally {
        if (ireader != null) {
            try {
                ireader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (directory != null) {
            try {
                directory.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}