コード例 #1
0
    public void parse(NamedList<NamedList<Number>> terms) {
      // exit if there is no terms
      if (terms == null) {
        return;
      }

      TermsResponse termsResponse = new TermsResponse(terms);

      // loop though each field and add each term+freq to map
      for (String key : fieldmap.keySet()) {
        HashMap<String, TermsResponse.Term> termmap = fieldmap.get(key);
        List<TermsResponse.Term> termlist = termsResponse.getTerms(key);

        // skip this field if there are no terms
        if (termlist == null) {
          continue;
        }

        // loop though each term
        for (TermsResponse.Term tc : termlist) {
          String term = tc.getTerm();
          if (termmap.containsKey(term)) {
            TermsResponse.Term oldtc = termmap.get(term);
            oldtc.addFrequency(tc.getFrequency());
            termmap.put(term, oldtc);
          } else {
            termmap.put(term, tc);
          }
        }
      }
    }
コード例 #2
0
  private List<String> query(String q, int limit) throws MalformedURLException {

    List<String> suggestedStrings = new ArrayList<String>();
    List<TermsResponse.Term> items = null;

    // escape special characters
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    query.addTermsField("title_autocomplete");
    query.setTerms(true);
    query.setTermsLimit(limit);
    // query.setTermsLower(q);
    query.setHighlight(true);
    // query.setTermsPrefix(q);
    query.setTermsRegex(".*" + q + ".*");
    // query.setTermsRegexFlag("dotall");
    query.setQueryType("/terms");
    try {
      QueryResponse qr = server.query(query);
      TermsResponse resp = qr.getTermsResponse();
      items = resp.getTerms("title_autocomplete");
    } catch (SolrServerException e) {
      items = null;
    }

    if (items != null) {
      for (TermsResponse.Term item : items) {
        suggestedStrings.add(item.getTerm());
      }
    }
    return suggestedStrings;
  }
コード例 #3
0
    public NamedList buildResponse() {
      NamedList<Object> response = new SimpleOrderedMap<>();

      // determine if we are going index or count sort
      boolean sort =
          !TermsParams.TERMS_SORT_INDEX.equals(
              params.get(TermsParams.TERMS_SORT, TermsParams.TERMS_SORT_COUNT));

      // init minimum frequency
      long freqmin = 1;
      String s = params.get(TermsParams.TERMS_MINCOUNT);
      if (s != null) freqmin = Long.parseLong(s);

      // init maximum frequency, default to max int
      long freqmax = -1;
      s = params.get(TermsParams.TERMS_MAXCOUNT);
      if (s != null) freqmax = Long.parseLong(s);
      if (freqmax < 0) {
        freqmax = Long.MAX_VALUE;
      }

      // init limit, default to max int
      long limit = 10;
      s = params.get(TermsParams.TERMS_LIMIT);
      if (s != null) limit = Long.parseLong(s);
      if (limit < 0) {
        limit = Long.MAX_VALUE;
      }

      // loop though each field we want terms from
      for (String key : fieldmap.keySet()) {
        NamedList<Number> fieldterms = new SimpleOrderedMap<>();
        TermsResponse.Term[] data = null;
        if (sort) {
          data = getCountSorted(fieldmap.get(key));
        } else {
          data = getLexSorted(fieldmap.get(key));
        }

        // loop though each term until we hit limit
        int cnt = 0;
        for (TermsResponse.Term tc : data) {
          if (tc.getFrequency() >= freqmin && tc.getFrequency() <= freqmax) {
            fieldterms.add(tc.getTerm(), num(tc.getFrequency()));
            cnt++;
          }

          if (cnt >= limit) {
            break;
          }
        }

        response.add(key, fieldterms);
      }

      return response;
    }