Java Code Examples for org.apache.solr.schema.FieldType#getFieldQuery()

The following examples show how to use org.apache.solr.schema.FieldType#getFieldQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolrQueryParserBase.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
Query rawToNormal(Query q) {
  Query normal = q;
  if (q instanceof RawQuery) {
    RawQuery rawq = (RawQuery)q;
    if (rawq.sfield.getType().isTokenized()) {
      normal = rawq.sfield.getType().getFieldQuery(parser, rawq.sfield, rawq.getJoinedExternalVal());
    } else {
      FieldType ft = rawq.sfield.getType();
      if (rawq.getTermCount() == 1) {
        normal = ft.getFieldQuery(this.parser, rawq.sfield, rawq.getExternalVals().get(0));
      } else {
        BooleanQuery.Builder booleanBuilder = newBooleanQuery();
        for (String externalVal : rawq.getExternalVals()) {
          Query subq = ft.getFieldQuery(this.parser, rawq.sfield, externalVal);
          booleanBuilder.add(subq, BooleanClause.Occur.SHOULD);
        }
        normal = QueryUtils.build(booleanBuilder, parser);
      }
    }
  }
  return normal;
}
 
Example 2
Source File: FieldQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
  return new QParser(qstr, localParams, params, req) {
    @Override
    public Query parse() {
      String field = localParams.get(QueryParsing.F);
      String queryText = localParams.get(QueryParsing.V);
      SchemaField sf = req.getSchema().getField(field);
      FieldType ft = sf.getType();
      return ft.getFieldQuery(this, sf, queryText);
    }
  };
}
 
Example 3
Source File: FacetFieldProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private SimpleOrderedMap<Object> refineBucket(Object bucketVal, boolean skip, Map<String,Object> facetInfo) throws IOException {
  SimpleOrderedMap<Object> bucket = new SimpleOrderedMap<>();
  FieldType ft = sf.getType();
  bucketVal = ft.toNativeType(bucketVal);  // refinement info passed in as JSON will cause int->long and float->double
  bucket.add("val", bucketVal);

  // fieldQuery currently relies on a string input of the value...
  String bucketStr = bucketVal instanceof Date ? ((Date)bucketVal).toInstant().toString() : bucketVal.toString();
  Query domainQ = ft.getFieldQuery(null, sf, bucketStr);

  fillBucket(bucket, domainQ, null, skip, facetInfo);

  return bucket;
}
 
Example 4
Source File: SolrQueryParserBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected Query getFieldQuery(String field, String queryText, boolean quoted, boolean raw) throws SyntaxError {
  checkNullField(field);

  SchemaField sf;
  if (field.equals(lastFieldName)) {
    // only look up the SchemaField on a field change... this helps with memory allocation of dynamic fields
    // and large queries like foo_i:(1 2 3 4 5 6 7 8 9 10) when we are passed "foo_i" each time.
    sf = lastField;
  } else {
    // intercept magic field name of "_" to use as a hook for our
    // own functions.
    if (allowSubQueryParsing && field.charAt(0) == '_' && parser != null) {
      MagicFieldName magic = MagicFieldName.get(field);
      if (null != magic) {
        subQParser = parser.subQuery(queryText, magic.subParser);
        return subQParser.getQuery();
      }
    }

    lastFieldName = field;
    sf = lastField = schema.getFieldOrNull(field);
  }

  if (sf != null) {
    FieldType ft = sf.getType();
    // delegate to type for everything except tokenized fields
    if (ft.isTokenized() && sf.indexed()) {
      boolean fieldAutoGenPhraseQueries = ft instanceof TextField && ((TextField)ft).getAutoGeneratePhraseQueries();
      boolean fieldEnableGraphQueries = ft instanceof TextField && ((TextField)ft).getEnableGraphQueries();
      SynonymQueryStyle synonymQueryStyle = AS_SAME_TERM;
      if (ft instanceof TextField) {
        synonymQueryStyle = ((TextField)(ft)).getSynonymQueryStyle();
      }
      return newFieldQuery(getAnalyzer(), field, queryText, quoted, fieldAutoGenPhraseQueries, fieldEnableGraphQueries, synonymQueryStyle);
    } else {
      if (raw) {
        return new RawQuery(sf, queryText);
      } else {
        return ft.getFieldQuery(parser, sf, queryText);
      }
    }
  }

  // default to a normal field query
  return newFieldQuery(getAnalyzer(), field, queryText, quoted, false, true, AS_SAME_TERM);
}
 
Example 5
Source File: TermsComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void fetchTerms(SolrIndexSearcher indexSearcher, String[] fields, String termList,
    boolean includeTotalTermFreq, NamedList<Object> result) throws IOException {
  List<String> splitTermList = StrUtils.splitSmart(termList, ",", true);
  // Sort the terms once
  String[] splitTerms = splitTermList.toArray(new String[splitTermList.size()]);
  // Not a great idea to trim here, but it was in the original implementation
  for (int i = 0; i < splitTerms.length; i++) {
    splitTerms[i] = splitTerms[i].trim();
  }
  Arrays.sort(splitTerms);

  IndexReaderContext topReaderContext = indexSearcher.getTopReaderContext();
  for (String field : fields) {
    SchemaField sf = indexSearcher.getSchema().getField(field);
    FieldType fieldType = sf.getType();
    NamedList<Object> termsMap = new NamedList<>();

    if (fieldType.isPointField()) {
      for (String term : splitTerms) {
        Query q = fieldType.getFieldQuery(null, sf, term);
        int count = indexSearcher.getDocSet(q).size();
        termsMap.add(term, count);
      }
    } else {

      // Since splitTerms is already sorted, this array will also be sorted. NOTE: this may not be true, it depends on readableToIndexed.
      Term[] terms = new Term[splitTerms.length];
      for (int i = 0; i < splitTerms.length; i++) {
        terms[i] = new Term(field, fieldType.readableToIndexed(splitTerms[i]));
      }

      TermStates[] termStates = new TermStates[terms.length];
      collectTermStates(topReaderContext, termStates, terms);

      for (int i = 0; i < terms.length; i++) {
        if (termStates[i] != null) {
          String outTerm = fieldType.indexedToReadable(terms[i].bytes().utf8ToString());
          int docFreq = termStates[i].docFreq();
          addTermToNamedList(termsMap, outTerm, docFreq, termStates[i].totalTermFreq(), includeTotalTermFreq);
        }
      }
    }

    result.add(field, termsMap);
  }
}