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.ecyrd.jspwiki.search.LuceneSearchProvider.java

License:Apache License

/**
 *  Searches pages using a particular combination of flags.
 *
 *  @param query The query to perform in Lucene query language
 *  @param flags A set of flags/*from  w  w w . ja  v a2 s .  co  m*/
 *  @return A Collection of SearchResult instances
 *  @throws ProviderException if there is a problem with the backend
 */
public Collection findPages(String query, int flags) throws ProviderException {
    Searcher searcher = null;
    ArrayList<SearchResult> list = null;
    Highlighter highlighter = null;

    try {
        String[] queryfields = { LUCENE_PAGE_CONTENTS, LUCENE_PAGE_NAME, LUCENE_AUTHOR, LUCENE_ATTACHMENTS };
        QueryParser qp = new MultiFieldQueryParser(queryfields, getLuceneAnalyzer());

        //QueryParser qp = new QueryParser( LUCENE_PAGE_CONTENTS, getLuceneAnalyzer() );
        Query luceneQuery = qp.parse(query);

        if ((flags & FLAG_CONTEXTS) != 0) {
            highlighter = new Highlighter(new SimpleHTMLFormatter("<span class=\"searchmatch\">", "</span>"),
                    new SimpleHTMLEncoder(), new QueryScorer(luceneQuery));
        }

        try {
            searcher = new IndexSearcher(m_luceneDirectory);
        } catch (Exception ex) {
            log.info("Lucene not yet ready; indexing not started", ex);
            return null;
        }

        Hits hits = searcher.search(luceneQuery);

        list = new ArrayList<SearchResult>(hits.length());
        for (int curr = 0; curr < hits.length(); curr++) {
            Document doc = hits.doc(curr);
            String pageName = doc.get(LUCENE_ID);
            WikiPage page = m_engine.getPage(pageName, WikiPageProvider.LATEST_VERSION);

            if (page != null) {
                if (page instanceof Attachment) {
                    // Currently attachments don't look nice on the search-results page
                    // When the search-results are cleaned up this can be enabled again.
                }

                int score = (int) (hits.score(curr) * 100);

                // Get highlighted search contexts
                String text = doc.get(LUCENE_PAGE_CONTENTS);

                String[] fragments = new String[0];
                if (text != null && highlighter != null) {
                    TokenStream tokenStream = getLuceneAnalyzer().tokenStream(LUCENE_PAGE_CONTENTS,
                            new StringReader(text));
                    fragments = highlighter.getBestFragments(tokenStream, text, MAX_FRAGMENTS);

                }

                SearchResult result = new SearchResultImpl(page, score, fragments);
                list.add(result);
            } else {
                log.error("Lucene found a result page '" + pageName
                        + "' that could not be loaded, removing from Lucene cache");
                pageRemoved(new WikiPage(m_engine, pageName));
            }
        }
    } catch (IOException e) {
        log.error("Failed during lucene search", e);
    } catch (InstantiationException e) {
        log.error("Unable to get a Lucene analyzer", e);
    } catch (IllegalAccessException e) {
        log.error("Unable to get a Lucene analyzer", e);
    } catch (ClassNotFoundException e) {
        log.error("Specified Lucene analyzer does not exist", e);
    } catch (ParseException e) {
        log.info("Broken query; cannot parse", e);

        throw new ProviderException("You have entered a query Lucene cannot process: " + e.getMessage());
    } finally {
        if (searcher != null) {
            try {
                searcher.close();
            } catch (IOException e) {
            }
        }
    }

    return list;
}

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.  co  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.edgenius.test.lucene.TestLucene.java

License:Open Source License

@Before
public void setUp() throws Exception {
    initWriter();//from  ww w .ja v  a 2  s  . c  om
    buildIndex();
    writer.optimize();
    writer.close();

    //searcher initial must after writer close!
    searcher = new IndexSearcher(FSDirectory.open(new File(indexDir)));
}

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 .  j a  v a 2s . co m
    }

    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.emental.mindraider.core.search.SearchEngine.java

License:Apache License

/**
 * Execute search using index./*ww  w . ja  v a 2s . co m*/
 * 
 * @param indexPath
 *            the index path
 * @param queryString
 *            the query string
 * @return Returns an array of <code>SearchResultEntry</code> otherwise
 *         null.
 */
public SearchResultEntry[] search(String queryString) {
    ArrayList<SearchResultEntry> result = new ArrayList<SearchResultEntry>();

    try {
        Searcher searcher = new IndexSearcher(indexPath);
        Analyzer analyzer = new StandardAnalyzer();

        String line = queryString;

        logger.debug("Query: " + line);
        QueryParser queryParser = new QueryParser("contents", analyzer);
        Query query = queryParser.parse(line);

        logger.debug("\nSearching for: '" + query.toString("contents") + "'");
        Hits hits = searcher.search(query);
        logger.debug("Search result: " + hits.length() + " total matching documents");

        for (int i = 0; i < hits.length(); i++) {
            Document doc = hits.doc(i);
            String path = doc.get("path");

            if (path != null) {
                logger.debug(i + ". " + path);
                // i'm interested only in concepts so filter out
                // non-concepts (HACK)
                // TODO this filter is here because of obsolete indexes -
                // there should be deleted
                if (path.indexOf(File.separator + "concepts" + File.separator) >= 0) {
                    result.add(new SearchResultEntry(doc.get("outlineLabel"), doc.get("conceptLabel"),
                            doc.get("path")));
                    //                   logger.debug("path:\n"+doc.get("path"));
                    //                   logger.debug("modified:\n"+doc.get("modified"));
                    //                   logger.debug("notebook:\n"+doc.get("outlineLabel"));
                    //                   logger.debug("concept:\n"+doc.get("conceptLabel"));
                }

            } else {
                String url = doc.get("url");
                if (url != null) {
                    logger.debug(i + ". " + url);
                    logger.debug("   - " + doc.get("title"));
                } else {
                    logger.debug(i + ". " + "No path nor URL for this document");
                }
            }
        }
        searcher.close();

        return (SearchResultEntry[]) result.toArray(new SearchResultEntry[result.size()]);
    } catch (Exception e) {
        logger.error("Caught a " + e.getClass() + "\n with message: " + e.getMessage(), e);
    }
    return null;
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Queries a feature index of a project, specified by ID
 *
 * @param projectId ID of a project, which index to work with
 * @param query a query to search in index
 * @return a {List} of {@code FeatureIndexEntry} objects that satisfy index query
 * @deprecated/*from w  w  w .  ja v  a2  s .  c  o m*/
 * @throws IOException
 */
@Deprecated
private IndexSearchResult searchLuceneIndexForProject(final long projectId, Query query,
        List<String> vcfInfoFields, Integer maxResultsCount, Sort sort) throws IOException {
    Map<Integer, FeatureIndexEntry> entryMap = new LinkedHashMap<>();

    int totalHits = 0;
    try (Directory index = fileManager.getIndexForProject(projectId);
            IndexReader reader = DirectoryReader.open(index)) {
        if (reader.numDocs() == 0) {
            return new IndexSearchResult(Collections.emptyList(), false, 0);
        }

        IndexSearcher searcher = new IndexSearcher(reader);
        final TopDocs docs;
        int resultsCount = maxResultsCount == null ? reader.numDocs() : maxResultsCount;
        if (sort == null) {
            docs = searcher.search(query, resultsCount);
        } else {
            docs = searcher.search(query, resultsCount, sort);
        }

        totalHits = docs.totalHits;
        final ScoreDoc[] hits = docs.scoreDocs;

        Map<Long, BookmarkIndexEntry> foundBookmarkEntries = new HashMap<>(); // for batch bookmarks loading
        createIndexEntries(hits, entryMap, foundBookmarkEntries, searcher, vcfInfoFields);
        setBookmarks(foundBookmarkEntries);
    } catch (IOException e) {
        LOGGER.error(MessageHelper.getMessage(MessagesConstants.ERROR_FEATURE_INDEX_SEARCH_FAILED), e);
        return new IndexSearchResult(Collections.emptyList(), false, 0);
    }

    return new IndexSearchResult(new ArrayList<>(entryMap.values()),
            maxResultsCount != null && totalHits > maxResultsCount, totalHits);
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Queries a feature index of a list of files
 *
 * @param files a {@link List} of {@link FeatureFile}, which indexes to search
 * @param query a query to search in index
 * @param vcfInfoFields list of info fields to retrieve
 * @param maxResultsCount specifies a maximum number of search results to get
 * @param sort specifies sorting// w ww.  j a va  2s  . c  o m
 * @return a {List} of {@code FeatureIndexEntry} objects that satisfy index query
 * @throws IOException if something is wrong in the filesystem
 */
public <T extends FeatureIndexEntry> IndexSearchResult<T> searchFileIndexes(List<? extends FeatureFile> files,
        Query query, List<String> vcfInfoFields, Integer maxResultsCount, Sort sort) throws IOException {
    if (CollectionUtils.isEmpty(files)) {
        return new IndexSearchResult<>(Collections.emptyList(), false, 0);
    }

    Map<Integer, FeatureIndexEntry> entryMap = new LinkedHashMap<>();

    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return new IndexSearchResult<>(Collections.emptyList(), false, 0);
        }

        IndexSearcher searcher = new IndexSearcher(reader);
        final TopDocs docs = performSearch(searcher, query, reader, maxResultsCount, sort);

        int totalHits = docs.totalHits;
        final ScoreDoc[] hits = docs.scoreDocs;

        Map<Long, BookmarkIndexEntry> foundBookmarkEntries = new HashMap<>(); // for batch bookmarks loading
        createIndexEntries(hits, entryMap, foundBookmarkEntries, searcher, vcfInfoFields);
        setBookmarks(foundBookmarkEntries);

        return new IndexSearchResult<>(new ArrayList<T>((Collection<? extends T>) entryMap.values()),
                maxResultsCount != null && totalHits > maxResultsCount, totalHits);
    } finally {
        for (SimpleFSDirectory index : indexes) {
            IOUtils.closeQuietly(index);
        }
    }
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Queries a feature index of a list of files, returning specified page of specified size.
 * If no paging parameters are passed, returns all results
 *
 * @param files a {@link List} of {@link FeatureFile}, which indexes to search
 * @param query a query to search in index
 * @param vcfInfoFields list of info fields to retrieve
 * @param page number of a page to display
 * @param pageSize number of entries per page
 * @param orderBy object, that specifies sorting
 * @return a {List} of {@code FeatureIndexEntry} objects that satisfy index query
 * @throws IOException if something is wrong in the filesystem
 *///from   w  w  w . j av a 2 s. c o m
public <T extends FeatureIndexEntry> IndexSearchResult<T> searchFileIndexesPaging(
        List<? extends FeatureFile> files, Query query, List<String> vcfInfoFields, Integer page,
        Integer pageSize, List<VcfFilterForm.OrderBy> orderBy) throws IOException {

    if (CollectionUtils.isEmpty(files)) {
        return new IndexSearchResult<>(Collections.emptyList(), false, 0);
    }

    List<FeatureIndexEntry> entries;

    int totalHits = 0;
    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return new IndexSearchResult<>(Collections.emptyList(), false, 0);
        }

        IndexSearcher searcher = new IndexSearcher(reader);
        GroupingSearch groupingSearch = new GroupingSearch(FeatureIndexFields.UID.fieldName);
        setSorting(orderBy, groupingSearch, files);

        TopGroups<String> topGroups = groupingSearch.search(searcher, query,
                page == null ? 0 : (page - 1) * pageSize, page == null ? reader.numDocs() : pageSize);

        final ScoreDoc[] hits = new ScoreDoc[topGroups.groups.length];
        for (int i = 0; i < topGroups.groups.length; i++) {
            hits[i] = topGroups.groups[i].scoreDocs[0];
        }

        entries = new ArrayList<>(hits.length);
        for (ScoreDoc hit : hits) {
            entries.add(createIndexEntry(hit, new HashMap<>(), searcher, vcfInfoFields));
        }
    } finally {
        for (SimpleFSDirectory index : indexes) {
            IOUtils.closeQuietly(index);
        }
    }

    return new IndexSearchResult<>((List<T>) entries, false, totalHits);
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

public int getTotalVariationsCountFacet(List<? extends FeatureFile> files, Query query) throws IOException {
    if (CollectionUtils.isEmpty(files)) {
        return 0;
    }/*  www. ja va 2  s. com*/

    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return 0;
        }

        FacetsCollector facetsCollector = new FacetsCollector();
        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.search(query, facetsCollector);

        Facets facets = new SortedSetDocValuesFacetCounts(
                new DefaultSortedSetDocValuesReaderState(reader, FeatureIndexFields.FACET_UID.fieldName),
                facetsCollector);
        FacetResult res = facets.getTopChildren(reader.numDocs(), FeatureIndexFields.F_UID.getFieldName());
        if (res == null) {
            return 0;
        }

        return res.childCount;
    } finally {
        for (SimpleFSDirectory index : indexes) {
            IOUtils.closeQuietly(index);
        }
    }
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Groups variations from specified {@link List} of {@link VcfFile}s by specified field
 * @param files a {@link List} of {@link FeatureFile}, which indexes to search
 * @param query a query to search in index
 * @param groupBy a field to perform grouping
 * @return a {@link List} of {@link Group}s, mapping field value to number of variations, having this value
 * @throws IOException if something goes wrong with the file system
 *///from w  ww  . ja  va  2s.  co  m
public List<Group> groupVariations(List<VcfFile> files, Query query, String groupBy) throws IOException {
    List<Group> res = new ArrayList<>();

    if (CollectionUtils.isEmpty(files)) {
        return Collections.emptyList();
    }

    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return Collections.emptyList();
        }

        IndexSearcher searcher = new IndexSearcher(reader);
        AbstractGroupFacetCollector groupedFacetCollector = TermGroupFacetCollector
                .createTermGroupFacetCollector(FeatureIndexFields.UID.fieldName,
                        getGroupByField(files, groupBy), false, null, GROUP_INITIAL_SIZE);
        searcher.search(query, groupedFacetCollector); // Computing the grouped facet counts
        TermGroupFacetCollector.GroupedFacetResult groupedResult = groupedFacetCollector
                .mergeSegmentResults(reader.numDocs(), 1, false);
        List<AbstractGroupFacetCollector.FacetEntry> facetEntries = groupedResult.getFacetEntries(0,
                reader.numDocs());
        for (AbstractGroupFacetCollector.FacetEntry facetEntry : facetEntries) {
            res.add(new Group(facetEntry.getValue().utf8ToString(), facetEntry.getCount()));
        }
    } finally {
        for (SimpleFSDirectory index : indexes) {
            IOUtils.closeQuietly(index);
        }
    }

    return res;
}