Package org.apache.lucene.search

Examples of org.apache.lucene.search.Searcher


                          Field.Store.NO, Field.Index.ANALYZED));
      writer.addDocument(doc);
    }
    writer.optimize();
    writer.close();
    Searcher searcher = new IndexSearcher(indexStore, true);

    Sort sort = new Sort();
    Query queryX = new TermQuery(new Term ("contents", "x"));
    Query queryY = new TermQuery(new Term ("contents", "y"));
   
View Full Code Here


    public void testSearchTermHand() throws Exception {
        BooleanQuery combQuery = new BooleanQuery();
        String queryLine = "hand";
        IndexReader reader = IndexReader.open(FSDirectory.open(new File(SearchWorker.getIndexPath(null))), true);

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

        QueryParser parser = new QueryParser(Version.LUCENE_30, "content", analyzer);
        Query query = parser.parse(queryLine);
        combQuery.add(query, BooleanClause.Occur.MUST);

        TopScoreDocCollector collector = TopScoreDocCollector.create(10, false);
        searcher.search(combQuery, collector);

        assertEquals("Only 1 result expected from the testdata", 1, collector.getTotalHits());
    }
View Full Code Here

    Path localDir = new Path(getUnitTestdir(getName()), "index_" +
      Integer.toString(new Random().nextInt()));
    this.fs.copyToLocalFile(new Path(INDEX_DIR), localDir);
    FileSystem localfs = FileSystem.getLocal(conf);
    FileStatus [] indexDirs = localfs.listStatus(localDir);
    Searcher searcher = null;
    Scanner scanner = null;
    try {
      if (indexDirs.length == 1) {
        searcher = new IndexSearcher((new File(indexDirs[0].getPath().
          toUri())).getAbsolutePath());
      } else if (indexDirs.length > 1) {
        Searchable[] searchers = new Searchable[indexDirs.length];
        for (int i = 0; i < indexDirs.length; i++) {
          searchers[i] = new IndexSearcher((new File(indexDirs[i].getPath().
            toUri()).getAbsolutePath()));
        }
        searcher = new MultiSearcher(searchers);
      } else {
        throw new IOException("no index directory found");
      }

      HTable table = new HTable(conf, TABLE_NAME);
      scanner = table.getScanner(columns, HConstants.EMPTY_START_ROW);

      IndexConfiguration indexConf = new IndexConfiguration();
      String content = conf.get("hbase.index.conf");
      if (content != null) {
        indexConf.addFromXML(content);
      }
      String rowkeyName = indexConf.getRowkeyName();

      int count = 0;
      for (RowResult r : scanner) {
        String value = Bytes.toString(r.getRow());
        Term term = new Term(rowkeyName, value);
        int hitCount = searcher.search(new TermQuery(term)).length();
        assertEquals("check row " + value, 1, hitCount);
        count++;
      }
      LOG.debug("Searcher.maxDoc: " + searcher.maxDoc());
      LOG.debug("IndexReader.numDocs: " + ((IndexSearcher)searcher).getIndexReader().numDocs());     
      int maxDoc = ((IndexSearcher)searcher).getIndexReader().numDocs();
      assertEquals("check number of rows", maxDoc, count);
    } finally {
      if (null != searcher)
        searcher.close();
      if (null != scanner)
        scanner.close();
    }
  }
View Full Code Here

    /**
     *
     */
    public static Hits search(String line, File index_directory) throws Exception {
        Searcher searcher = new IndexSearcher(index_directory.getAbsolutePath());
        Analyzer analyzer = new StandardAnalyzer();

        Query query = QueryParser.parse(line, "contents", analyzer);
        System.out.println("Searching for: " + query.toString("contents"));

                Hits hits = searcher.search(query);
                System.out.println("Total matching documents: " + hits.length());

                final int HITS_PER_PAGE = 10;

                for (int start = 0; start < hits.length(); start += HITS_PER_PAGE) {
                    int end = Math.min(hits.length(), start + HITS_PER_PAGE);

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

                        if (path != null) {
                            System.out.println(i + ". " + path);
                        } else {
                            String url = doc.get("url");

                            if (url != null) {
                                System.out.println(i + ". " + url);
                                System.out.println("   - " + doc.get("title"));
                            } else {
                                System.out.println(i + ". " + "No path nor URL for this document");
                            }
                        }
                    }

                }
                searcher.close();
        return hits;
    }
View Full Code Here

  }

  public Explanation explain(int documentId) {
    Explanation explanation = null;
    SearchFactoryImplementor searchFactoryImplementor = getSearchFactoryImplementor();
    Searcher searcher = buildSearcher( searchFactoryImplementor );
    if (searcher == null) {
      throw new SearchException("Unable to build explanation for document id:"
          + documentId + ". no index found");
    }
    try {
      org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
      buildFilters();
      explanation = searcher.explain( query, documentId );
    }
    catch (IOException e) {
      throw new HibernateException( "Unable to query Lucene index and build explanation", e );
    }
    finally {
View Full Code Here

    }
  }

  public Explanation explain(int documentId) {
    Explanation explanation = null;
    Searcher searcher = buildSearcher( searchFactoryImplementor );
    if ( searcher == null ) {
      throw new SearchException(
          "Unable to build explanation for document id:"
              + documentId + ". no index found"
      );
    }
    try {
      org.apache.lucene.search.Query query = filterQueryByClasses( luceneQuery );
      buildFilters();
      explanation = searcher.explain( query, documentId );
    }
    catch ( IOException e ) {
      throw new HibernateException( "Unable to query Lucene index and build explanation", e );
    }
    finally {
View Full Code Here

    IndexAccessor accessor = factory.getAccessor(ramdir);
    assertNotNull(accessor);

    IndexReader reader = accessor.getReader(true);
    Searcher searcher = accessor.getSearcher(reader);

    accessor.release(searcher);
  }
View Full Code Here

    IndexAccessor accessor = factory.getAccessor(ramdir);
    assertNotNull(accessor);

    IndexReader reader = accessor.getReader(true);
    Searcher searcher = accessor.getSearcher();

    accessor.release(searcher);
  }
View Full Code Here

    IndexAccessor accessor = factory.getAccessor(ramdir);
    assertNotNull(accessor);

    IndexReader reader = accessor.getReader(true);
    Searcher searcher = accessor.getSearcher();

    Searcher searcher2 = accessor.getSearcher();

    assertEquals(searcher, searcher2);

    accessor.release(searcher);
    accessor.release(searcher2);
View Full Code Here

   */
  @SuppressWarnings("unchecked")
  public final HashMap<String, Object> search(final String query, final String[] searchedAttributes, final int count, final int start,
      final boolean explain, final String[] sorting, final CRRequest request) throws IOException, CRException {

    Searcher searcher;
    Analyzer analyzer;
    // Collect count + start hits
    int hits = count + start;  // we want to retreive the startcount (start) to endcount (hits)

    LuceneIndexLocation idsLocation = LuceneIndexLocation.getIndexLocation(config);

    // TODO: maybe we should make this configurable. Imho this should not be a problem if enabled by default as once a search
    // has been executed the Factory is opened and we only need to make sure that on shutdown it is released.
    boolean reopenIndexFactory = true;
    IndexAccessor indexAccessor = idsLocation.getAccessor(reopenIndexFactory);
    if (indexAccessor == null) {
      log.error("IndexAccessor is null. Search will not work.");
    }

    // Resources needed for faceted search
    TaxonomyAccessor taAccessor = null;
    TaxonomyReader taReader = null;
    IndexReader facetsIndexReader = null;
    IndexReader uniqueMimeTypesIndexReader = null;
   
    List<String> uniqueMimeTypes = null;
    if (retrieveUniqueMimeTypes) {
      // retrieve all possible file types
      uniqueMimeTypesIndexReader = indexAccessor.getReader(false);
      final TermEnum termEnum = uniqueMimeTypesIndexReader.terms(new Term(LUCENE_INDEX_MIMETYPE, ""));
      uniqueMimeTypes = new ArrayList<String>();
      while (termEnum.next() && termEnum.term().field().equals(LUCENE_INDEX_MIMETYPE)) {
        uniqueMimeTypes.add(termEnum.term().text());
      }
    }

    // get accessors and reader only if facets are activated
    if (facetsSearch.useFacets()) {
      facetsIndexReader = indexAccessor.getReader(false);
      taAccessor = idsLocation.getTaxonomyAccessor();
      taReader = taAccessor.getTaxonomyReader();
    }

    searcher = indexAccessor.getPrioritizedSearcher();
    Object userPermissionsObject = request.get(CRRequest.PERMISSIONS_KEY);
    String[] userPermissions = new String[0];
    if (userPermissionsObject instanceof String[]) {
      userPermissions = (String[]) userPermissionsObject;
    }
    TopDocsCollector<?> collector = createCollector(searcher, hits, sorting, computescores, userPermissions);
    HashMap<String, Object> result = null;
    try {
      analyzer = LuceneAnalyzerFactory.createAnalyzer(config);

      if (searchedAttributes != null && searchedAttributes.length > 0 && query != null && !query.equals("")) {
        QueryParser parser = CRQueryParserFactory.getConfiguredParser(searchedAttributes, analyzer, request, config);

        Query parsedQuery = parser.parse(query);
        // GENERATE A NATIVE QUERY

        parsedQuery = searcher.rewrite(parsedQuery);

        result = new HashMap<String, Object>(3);
        result.put(RESULT_QUERY_KEY, parsedQuery);

        // when facets are active create a FacetsCollector
View Full Code Here

TOP

Related Classes of org.apache.lucene.search.Searcher

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.