Package org.apache.lucene.search

Examples of org.apache.lucene.search.Searcher


    LuceneIndexLocation synonymLocation = LuceneIndexLocation
        .getIndexLocation(new CRConfigUtil(autoConf, "synonymlocation"));
   
   
    IndexAccessor ia = synonymLocation.getAccessor();
    Searcher synonymSearcher = ia.getPrioritizedSearcher();
    IndexReader synonymReader = ia.getReader(false);
   
   
    try {
      HashSet<String> searchedTerms = new HashSet<String>();
     
      //get all searched Terms out of query
      for (int i = 0; i < searchedAttributes.length; i++) {
        String subquery = query;
        while (subquery.indexOf(searchedAttributes[i] + ":") > 0) {
          subquery = subquery.substring(subquery.indexOf(searchedAttributes[i] + ":") + searchedAttributes[i].length() + 1);
          int substringUntil = -1;
          int pos1 = subquery.indexOf(")");
          int pos2 = subquery.indexOf(" ");
          if (pos1 != -1) {
            substringUntil = pos1;
          }
          if (pos2 != -1) {
            substringUntil = pos2;
          }
          if (pos1 != -1 && pos2 != -1) {
            if (pos1 <= pos2) {
              substringUntil = pos1;
            }
            else {
              substringUntil = pos2;
            }
          }
          if (substringUntil == -1) {
            substringUntil = subquery.length();
          }
          String addtoSet = subquery.substring(0, substringUntil).replaceAll("\\*", "").replaceAll("\\(", "").replaceAll("\\)", "");
          searchedTerms.add(addtoSet);
          subquery = subquery.substring(substringUntil);
        }
       
      }
     
      //create the query-String for synonym-Index with all searchedTerms
      Iterator<String> it = searchedTerms.iterator();
      String queryString = "";
      while (it.hasNext()) {
        queryString = queryString + "Deskriptor:" + it.next() + " ";
      }
      Query querySynonym;
      try {
        querySynonym = super.parse(queryString);
      } catch (ParseException e) {
        e.printStackTrace();
        log.debug("Error while parsing query for accessing the synonym Index.", e);
        return query;
      }
     
      //get all Synonyms from SynonymIndex and add them to searchQuery
      log.debug("Synonym Query String: " + querySynonym.toString());
      TopDocs docs = synonymSearcher.search(querySynonym, MAX_SYNONYMS);
      log.debug("total found synonyms: " + docs.totalHits)
      for (ScoreDoc doc : docs.scoreDocs) {
        Document d = synonymReader.document(doc.doc);
        for (int i = 0; i < searchedAttributes.length; i++) {
          query = query + " OR " + searchedAttributes[i] + ":" + d.get("Synonym");
View Full Code Here


    if (autocompletereopenupdate || useAutocompleteIndexExtension) {
      checkForUpdate();
    }

    IndexAccessor ia = autocompleteLocation.getAccessor();
    Searcher autoCompleteSearcher = ia.getPrioritizedSearcher();
    IndexReader autoCompleteReader = ia.getReader(false);
    try {
      Query query = new TermQuery(new Term(GRAMMED_WORDS_FIELD, term));
      Sort sort = new Sort(new SortField(COUNT_FIELD, SortField.LONG, true));
      TopDocs docs = autoCompleteSearcher.search(query, null, 5, sort);
      int id = 1;
      for (ScoreDoc doc : docs.scoreDocs) {
        CRResolvableBean bean = new CRResolvableBean(id++);
        Document d = autoCompleteReader.document(doc.doc);
        bean.set(SOURCE_WORD_FIELD, d.get(SOURCE_WORD_FIELD));
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);
    Path [] indexDirs = localfs.listPaths(new Path [] {localDir});
    Searcher searcher = null;
    HScannerInterface scanner = null;
    try {
      if (indexDirs.length == 1) {
        searcher = new IndexSearcher((new File(indexDirs[0].
          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].
            toUri()).getAbsolutePath()));
        }
        searcher = new MultiSearcher(searchers);
      } else {
        throw new IOException("no index directory found");
      }

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

      HStoreKey key = new HStoreKey();
      TreeMap<Text, byte[]> results = new TreeMap<Text, byte[]>();

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

      int count = 0;
      while (scanner.next(key, results)) {
        String value = key.getRow().toString();
        LOG.debug("Scanned over " + key.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

  private void verify(Configuration c) throws IOException {
    Path localDir = new Path(this.testDir, "index_" +
      Integer.toString(new Random().nextInt()));
    this.fs.copyToLocalFile(new Path(INDEX_DIR), localDir);
    Path [] indexDirs = this.localFs.listPaths(new Path [] {localDir});
    Searcher searcher = null;
    HScannerInterface scanner = null;
    try {
      if (indexDirs.length == 1) {
        searcher = new IndexSearcher((new File(indexDirs[0].
          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].
            toUri()).getAbsolutePath()));
        }
        searcher = new MultiSearcher(searchers);
      } else {
        throw new IOException("no index directory found");
      }

      HTable table = new HTable(c, new Text(TABLE_NAME));
      Text[] columns = { TEXT_INPUT_COLUMN, TEXT_OUTPUT_COLUMN };
      scanner = table.obtainScanner(columns, HConstants.EMPTY_START_ROW);

      HStoreKey key = new HStoreKey();
      TreeMap<Text, byte[]> results = new TreeMap<Text, byte[]>();

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

      int count = 0;
      while (scanner.next(key, results)) {
        String value = key.getRow().toString();
        Term term = new Term(rowkeyName, value);
        int hitCount = searcher.search(new TermQuery(term)).length();
        assertEquals("check row " + value, 1, hitCount);
        count++;
      }
      int maxDoc = searcher.maxDoc();
      assertEquals("check number of rows", count, maxDoc);
    } finally {
      if (null != searcher)
        searcher.close();
      if (null != scanner)
        scanner.close();
    }
  }
View Full Code Here

            return aDocs;
        }

        private static String[] queryImpl(String aLanguageStr, String aIndexStr, String aQueryStr, boolean bCaptionOnly) throws Exception {
            IndexReader reader = IndexReader.open(aIndexStr);
            Searcher searcher = new IndexSearcher(reader);
            Analyzer analyzer = aLanguageStr.equals("ja") ? (Analyzer)new CJKAnalyzer() : (Analyzer)new StandardAnalyzer();

            String aField;
            if (bCaptionOnly) {
                aField = "caption";
            } else {
                aField = "content";
            }

            Query aQuery;
            if (aQueryStr.endsWith("*")) {
                aQuery = new WildcardQuery(new Term(aField, aQueryStr));
            } else {
                aQuery = new TermQuery(new Term(aField, aQueryStr));
            }

            // Perform search
            Hits aHits = searcher.search(aQuery);
            int nHitCount = aHits.length();

            String aDocs[] = new String[nHitCount];
            for (int iHit = 0; iHit < nHitCount; iHit++) {
                Document aDoc = aHits.doc(iHit);
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

    }
  }
   
  private float query(Object index, Query query) {
//    System.out.println("MB=" + (getMemorySize(index) / (1024.0f * 1024.0f)));
    Searcher searcher = null;
    try {
      if (index instanceof Directory)
        searcher = new IndexSearcher((Directory)index, true);
      else
        searcher = ((MemoryIndex) index).createSearcher();

      final float[] scores = new float[1]; // inits to 0.0f (no match)
      searcher.search(query, new Collector() {
        private Scorer scorer;

        @Override
        public void collect(int doc) throws IOException {
          scores[0] = scorer.score();
        }

        @Override
        public void setScorer(Scorer scorer) throws IOException {
          this.scorer = scorer;
        }

        @Override
        public boolean acceptsDocsOutOfOrder() {
          return true;
        }

        @Override
        public void setNextReader(IndexReader reader, int docBase) { }
      });
      float score = scores[0];
//      Hits hits = searcher.search(query);
//      float score = hits.length() > 0 ? hits.score(0) : 0.0f;
      return score;
    } catch (IOException e) { // should never happen (RAMDirectory)
      throw new RuntimeException(e);
    } finally {
      try {
        if (searcher != null) searcher.close();
      } catch (IOException e) { // should never happen (RAMDirectory)
        throw new RuntimeException(e);
      }
    }
  }
View Full Code Here

   
    File topicsFile = new File(args[0]);
    File qrelsFile = new File(args[1]);
    SubmissionReport submitLog = new SubmissionReport(new PrintWriter(args[2]), "lucene");
    FSDirectory dir = FSDirectory.open(new File(args[3]));
    Searcher searcher = new IndexSearcher(dir, true);

    int maxResults = 1000;
    String docNameField = "docname";

    PrintWriter logger = new PrintWriter(System.out, true);
View Full Code Here

    SearchResults searchResults = new SearchResults();
    searchResults.setQuery(queryString);

    if (queryString != null && queryString.length() > 0) {
      Searcher searcher = null;

      try {
        searcher = new IndexSearcher(blog.getSearchIndexDirectory());
        Query query = QueryParser.parse(queryString, "blogEntry", getAnalyzer());
        Hits hits = searcher.search(query);

        for (int i = 0; i < hits.length(); i++) {
          Document doc = hits.doc(i);
          SearchHit result = new SearchHit(
              blog,
              doc.get("id"),
              doc.get("permalink"),
              doc.get("title"),
              doc.get("truncatedBody"),
              DateField.stringToDate(doc.get("date")),
              hits.score(i));
          searchResults.add(result);
        }
      } catch (ParseException pe) {
        pe.printStackTrace();
        searchResults.setMessage("Sorry, but there was an error. Please try another search");
      } catch (Exception e) {
        e.printStackTrace();
        throw new SearchException(e.getMessage());
      } finally {
        if (searcher != null) {
          try {
            searcher.close();
          } catch (IOException e) {
            // can't do much now! ;-)
          }
        }
      }
View Full Code Here

        RAMDirectory dir = new RAMDirectory();
        IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
        writer.addDocument(makeDocumentWithFields());
        writer.close();

        Searcher searcher = new IndexSearcher(dir);

  // search for something that does exists
  Query query = new TermQuery(new Term("keyword", "test1"));

  // ensure that queries return expected results without DateFilter first
        Hits hits = searcher.search(query);
  assertEquals(1, hits.length());

        try
        {
            doAssert(hits.doc(0), true);
        }
        catch (Exception e)
        {
            e.printStackTrace(System.err);
            System.err.print("\n");
        }
        finally
        {
            searcher.close();
        }
    }
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.