Ejemplo n.º 1
0
  private void runTest(Class<? extends Lookup> lookupClass, boolean supportsExactWeights)
      throws Exception {

    // Add all input keys.
    Lookup lookup = lookupClass.newInstance();
    TermFreq[] keys = new TermFreq[this.keys.length];
    for (int i = 0; i < keys.length; i++) keys[i] = new TermFreq(this.keys[i], i);
    lookup.build(new TermFreqArrayIterator(keys));

    // Store the suggester.
    File storeDir = TEMP_DIR;
    lookup.store(new FileOutputStream(new File(storeDir, "lookup.dat")));

    // Re-read it from disk.
    lookup = lookupClass.newInstance();
    lookup.load(new FileInputStream(new File(storeDir, "lookup.dat")));

    // Assert validity.
    Random random = random();
    long previous = Long.MIN_VALUE;
    for (TermFreq k : keys) {
      List<LookupResult> list =
          lookup.lookup(_TestUtil.bytesToCharSequence(k.term, random), false, 1);
      assertEquals(1, list.size());
      LookupResult lookupResult = list.get(0);
      assertNotNull(k.term.utf8ToString(), lookupResult.key);

      if (supportsExactWeights) {
        assertEquals(k.term.utf8ToString(), k.v, lookupResult.value);
      } else {
        assertTrue(lookupResult.value + ">=" + previous, lookupResult.value >= previous);
        previous = lookupResult.value;
      }
    }
  }
Ejemplo n.º 2
0
  @Override
  public void build(SolrCore core, SolrIndexSearcher searcher) throws IOException {
    LOG.info("build()");
    if (sourceLocation == null) {
      reader = searcher.getIndexReader();
      dictionary = new HighFrequencyDictionary(reader, field, threshold);
    } else {
      try {

        final String fileDelim = ",";
        if (sourceLocation.contains(fileDelim)) {
          String[] files = sourceLocation.split(fileDelim);
          Reader[] readers = new Reader[files.length];
          for (int i = 0; i < files.length; i++) {
            Reader reader =
                new InputStreamReader(
                    core.getResourceLoader().openResource(files[i]), IOUtils.CHARSET_UTF_8);
            readers[i] = reader;
          }
          dictionary = new MultipleFileDictionary(readers);
        } else {
          dictionary =
              new FileDictionary(
                  new InputStreamReader(
                      core.getResourceLoader().openResource(sourceLocation),
                      IOUtils.CHARSET_UTF_8));
        }
      } catch (UnsupportedEncodingException e) {
        // should not happen
        LOG.error("should not happen", e);
      }
    }

    lookup.build(dictionary);
    if (storeDir != null) {
      File target = new File(storeDir, factory.storeFileName());
      if (!lookup.store(new FileOutputStream(target))) {
        if (sourceLocation == null) {
          assert reader != null && field != null;
          LOG.error(
              "Store Lookup build from index on field: "
                  + field
                  + " failed reader has: "
                  + reader.maxDoc()
                  + " docs");
        } else {
          LOG.error("Store Lookup build from sourceloaction: " + sourceLocation + " failed");
        }
      } else {
        LOG.info("Stored suggest data to: " + target.getAbsolutePath());
      }
    }
  }
  public List<String> autocomplete(String query) {
    List<String> result = new ArrayList<String>();

    String prefix = "";

    query = query.trim();

    if (query.contains(" ")) {
      prefix = query.substring(0, query.lastIndexOf(' ')) + ' ';
      query = query.substring(query.lastIndexOf(' ') + 1);
    }

    try {
      Lookup lookup = new TSTLookup();
      Directory indexPathDir = FSDirectory.open(new File(SearcherConfiguration.getIndexPath()));
      IndexReader ir = DirectoryReader.open(indexPathDir);

      Dictionary dictionary = new LuceneDictionary(ir, WebPage.TITLE);

      lookup.build(dictionary);

      List<LookupResult> resultsList = lookup.lookup(query, false, 30);

      for (LookupResult lr : resultsList) {
        String v = lr.key.toString();
        if (!v.equals(query)) {
          result.add(prefix + v);
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    }

    return result;
  }
Ejemplo n.º 4
0
 /**
  * Build lookup from a dictionary. Some implementations may require sorted or unsorted keys from
  * the dictionary's iterator - use {@link SortedInputIterator} or {@link UnsortedInputIterator} in
  * such case.
  */
 public void build(Dictionary dict) throws IOException {
   build(dict.getEntryIterator());
 }