@Override
  public List<LookupResult> lookup(CharSequence key, boolean onlyMorePopular, int num) {
    List<TernaryTreeNode> list = autocomplete.prefixCompletion(root, key, 0);
    List<LookupResult> res = new ArrayList<LookupResult>();
    if (list == null || list.size() == 0) {
      return res;
    }
    int maxCnt = Math.min(num, list.size());
    if (onlyMorePopular) {
      LookupPriorityQueue queue = new LookupPriorityQueue(num);

      for (TernaryTreeNode ttn : list) {
        queue.insertWithOverflow(new LookupResult(ttn.token, ((Number) ttn.val).longValue()));
      }
      for (LookupResult lr : queue.getResults()) {
        res.add(lr);
      }
    } else {
      for (int i = 0; i < maxCnt; i++) {
        TernaryTreeNode ttn = list.get(i);
        res.add(new LookupResult(ttn.token, ((Number) ttn.val).longValue()));
      }
    }
    return res;
  }
Ejemplo n.º 2
0
 @Override
 public List<LookupResult> lookup(
     CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num) {
   if (contexts != null) {
     throw new IllegalArgumentException("this suggester doesn't support contexts");
   }
   List<LookupResult> res = new ArrayList<>();
   List<String> list;
   int count = onlyMorePopular ? num * 2 : num;
   if (usePrefix) {
     list = trie.matchPrefix(key, count);
   } else {
     list = trie.matchAlmost(key, count);
   }
   if (list == null || list.size() == 0) {
     return res;
   }
   int maxCnt = Math.min(num, list.size());
   if (onlyMorePopular) {
     LookupPriorityQueue queue = new LookupPriorityQueue(num);
     for (String s : list) {
       long freq = ((Number) trie.get(s)).longValue();
       queue.insertWithOverflow(new LookupResult(new CharsRef(s), freq));
     }
     for (LookupResult lr : queue.getResults()) {
       res.add(lr);
     }
   } else {
     for (int i = 0; i < maxCnt; i++) {
       String s = list.get(i);
       long freq = ((Number) trie.get(s)).longValue();
       res.add(new LookupResult(new CharsRef(s), freq));
     }
   }
   return res;
 }