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

The following examples show how to use org.apache.solr.schema.FieldType#getQueryAnalyzer() . 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: LanguagePrefixedTokenStream.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Returns the {@link Analyzer} associated with the given language.
 * The proper {@link Analyzer} is retrieved from the first field type not null in the following list:
 *
 * <ul>
 *     <li>highlighted_text_ + locale (e.g. highlighted_text_en)</li>
 *     <li>text_ + locale (e.g. text_en)</li>
 *     <li>text___ (text general field)</li>
 * </ul>
 *
 * @param language the language code.
 * @return the {@link Analyzer} associated with the given language.
 */
Analyzer analyzer(String language) {
    FieldType localisedFieldType =
            ofNullable(indexSchema.getFieldTypeByName(highlightingFieldTypeName(language)))
                    .orElseGet(() -> indexSchema.getFieldTypeByName(localisedFieldTypeName(language)));

    FieldType targetFieldType =
            ofNullable(localisedFieldType)
                    .orElseGet(() ->  indexSchema.getFieldTypeByName(FALLBACK_TEXT_FIELD_TYPE_NAME));
    switch (mode)
    {
        case QUERY:
            return targetFieldType.getQueryAnalyzer();
        case INDEX:
        default:
            return targetFieldType.getIndexAnalyzer();
    }
}
 
Example 2
Source File: FreeTextLookupFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Lookup create(@SuppressWarnings({"rawtypes"})NamedList params, SolrCore core) {
  Object fieldTypeName = params.get(QUERY_ANALYZER);
  if (fieldTypeName == null) {
    throw new IllegalArgumentException("Error in configuration: " + QUERY_ANALYZER + " parameter is mandatory");
  }
  FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
  if (ft == null) {
    throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
  }
  
  Analyzer indexAnalyzer = ft.getIndexAnalyzer();
  Analyzer queryAnalyzer = ft.getQueryAnalyzer();
  
  int grams = (params.get(NGRAMS) != null) 
      ? Integer.parseInt(params.get(NGRAMS).toString()) 
      : FreeTextSuggester.DEFAULT_GRAMS;
  
  byte separator = (params.get(SEPARATOR) != null) 
      ? params.get(SEPARATOR).toString().getBytes(StandardCharsets.UTF_8)[0]
      : FreeTextSuggester.DEFAULT_SEPARATOR;
  
  return new FreeTextSuggester(indexAnalyzer, queryAnalyzer, grams, separator);
}
 
Example 3
Source File: DefaultLTRQParserPlugin.java    From ltr4l with Apache License 2.0 5 votes vote down vote up
@Override
public Query parse() throws SyntaxError {
  IndexReaderContext context = req.getSearcher().getTopReaderContext();
  for(FieldFeatureExtractorFactory factory: featuresSpec){
    String fieldName = factory.getFieldName();
    FieldType fieldType = req.getSchema().getFieldType(fieldName);
    Analyzer analyzer = fieldType.getQueryAnalyzer();
    factory.init(context, FieldFeatureExtractorFactory.terms(fieldName, qstr, analyzer));
  }

  return new DefaultLTRQuery(featuresSpec, ranker);
}
 
Example 4
Source File: MLAnalayser.java    From SearchServices with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Analyzer selectAnalyzer(FieldType fieldType) {
	 if(mode == Mode.INDEX)
	 {
		 return fieldType.getIndexAnalyzer();
	 }
	 else if(mode == Mode.QUERY)
	 {
		 return fieldType.getQueryAnalyzer();
	 }
	 else
	 {
		 return null;
	 }
}
 
Example 5
Source File: QueryElevationComponent.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void parseFieldType(SolrCore core) throws InitializationException {
  String a = initArgs.get(FIELD_TYPE);
  if (a != null) {
    FieldType ft = core.getLatestSchema().getFieldTypes().get(a);
    if (ft == null) {
      throw new InitializationException("Parameter " + FIELD_TYPE + " defines an unknown field type \"" + a + "\"", InitializationExceptionCause.UNKNOWN_FIELD_TYPE);
    }
    queryAnalyzer = ft.getQueryAnalyzer();
  }
}
 
Example 6
Source File: PayloadScoreQParserPlugin.java    From lucene-solr with Apache License 2.0 4 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() throws SyntaxError {
      String field = localParams.get(QueryParsing.F);
      String value = localParams.get(QueryParsing.V);
      String func = localParams.get("func");
      String operator = localParams.get("operator", DEFAULT_OPERATOR);
      if (!(operator.equalsIgnoreCase(DEFAULT_OPERATOR) || operator.equalsIgnoreCase("or"))) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Supported operators are : or , phrase");
      }
      boolean includeSpanScore = localParams.getBool("includeSpanScore", false);

      if (field == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'f' not specified");
      }

      if (value == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "query string missing");
      }

      FieldType ft = req.getCore().getLatestSchema().getFieldType(field);
      Analyzer analyzer = ft.getQueryAnalyzer();
      SpanQuery query;
      try {
        query = PayloadUtils.createSpanQuery(field, value, analyzer, operator);
      } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,e);
      }

      if (query == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SpanQuery is null");
      }

      // note: this query(/parser) does not support func=first; 'first' is a payload() value source feature only
      PayloadFunction payloadFunction = PayloadUtils.getPayloadFunction(func);
      if (payloadFunction == null) throw new SyntaxError("Unknown payload function: " + func);

      PayloadDecoder payloadDecoder = req.getCore().getLatestSchema().getPayloadDecoder(field);
      return new PayloadScoreQuery(query, payloadFunction, payloadDecoder, includeSpanScore);
    }
  };
}
 
Example 7
Source File: SpellCheckComponent.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void inform(SolrCore core) {
  if (initParams != null) {
    log.info("Initializing spell checkers");
    boolean hasDefault = false;
    for (int i = 0; i < initParams.size(); i++) {
      if (initParams.getName(i).equals("spellchecker")) {
        Object cfg = initParams.getVal(i);
        if (cfg instanceof NamedList) {
          addSpellChecker(core, hasDefault, (NamedList) cfg);
        } else if (cfg instanceof Map) {
          addSpellChecker(core, hasDefault, new NamedList((Map) cfg));
        } else if (cfg instanceof List) {
          for (Object o : (List) cfg) {
            if (o instanceof Map) {
              addSpellChecker(core, hasDefault, new NamedList((Map) o));
            }
          }
        }
      }
    }

    Map<String, QueryConverter> queryConverters = new HashMap<>();
    core.initPlugins(queryConverters,QueryConverter.class);

    //ensure that there is at least one query converter defined
    if (queryConverters.size() == 0) {
      log.trace("No queryConverter defined, using default converter");
      queryConverters.put("queryConverter", new SpellingQueryConverter());
    }

    //there should only be one
    if (queryConverters.size() == 1) {
      queryConverter = queryConverters.values().iterator().next();
      IndexSchema schema = core.getLatestSchema();
      String fieldTypeName = (String) initParams.get("queryAnalyzerFieldType");
      FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
      Analyzer analyzer = fieldType == null ? new WhitespaceAnalyzer()
              : fieldType.getQueryAnalyzer();
      //TODO: There's got to be a better way!  Where's Spring when you need it?
      queryConverter.setAnalyzer(analyzer);
    }
  }
}
 
Example 8
Source File: FuzzyLookupFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Lookup create(@SuppressWarnings({"rawtypes"})NamedList params, SolrCore core) {
  
  // mandatory parameter
  Object fieldTypeName = params.get(AnalyzingLookupFactory.QUERY_ANALYZER);
  if (fieldTypeName == null) {
    throw new IllegalArgumentException("Error in configuration: " + AnalyzingLookupFactory.QUERY_ANALYZER + " parameter is mandatory");
  }
  // retrieve index and query analyzers for the field
  FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
  if (ft == null) {
    throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
  }
  Analyzer indexAnalyzer = ft.getIndexAnalyzer();
  Analyzer queryAnalyzer = ft.getQueryAnalyzer();
  
  // optional parameters
  boolean exactMatchFirst = (params.get(AnalyzingLookupFactory.EXACT_MATCH_FIRST) != null)
  ? Boolean.valueOf(params.get(AnalyzingLookupFactory.EXACT_MATCH_FIRST).toString())
  : true;
      
  boolean preserveSep = (params.get(AnalyzingLookupFactory.PRESERVE_SEP) != null)
  ? Boolean.valueOf(params.get(AnalyzingLookupFactory.PRESERVE_SEP).toString())
  : true;
      
  int options = 0;
  if (exactMatchFirst) {
    options |= FuzzySuggester.EXACT_FIRST;
  }
  if (preserveSep) {
    options |= FuzzySuggester.PRESERVE_SEP;
  }
  
  int maxSurfaceFormsPerAnalyzedForm = (params.get(AnalyzingLookupFactory.MAX_SURFACE_FORMS) != null)
  ? Integer.parseInt(params.get(AnalyzingLookupFactory.MAX_SURFACE_FORMS).toString())
  : 256;
      
  int maxGraphExpansions = (params.get(AnalyzingLookupFactory.MAX_EXPANSIONS) != null)
  ? Integer.parseInt(params.get(AnalyzingLookupFactory.MAX_EXPANSIONS).toString())
  : -1;

  boolean preservePositionIncrements = params.get(AnalyzingLookupFactory.PRESERVE_POSITION_INCREMENTS) != null
  ? Boolean.valueOf(params.get(AnalyzingLookupFactory.PRESERVE_POSITION_INCREMENTS).toString())
  : false;
  
  int maxEdits = (params.get(MAX_EDITS) != null)
  ? Integer.parseInt(params.get(MAX_EDITS).toString())
  : FuzzySuggester.DEFAULT_MAX_EDITS;
  
  boolean transpositions = (params.get(TRANSPOSITIONS) != null)
  ? Boolean.parseBoolean(params.get(TRANSPOSITIONS).toString())
  : FuzzySuggester.DEFAULT_TRANSPOSITIONS;
      
  int nonFuzzyPrefix = (params.get(NON_FUZZY_PREFIX) != null)
  ? Integer.parseInt(params.get(NON_FUZZY_PREFIX).toString())
  :FuzzySuggester.DEFAULT_NON_FUZZY_PREFIX;
  
  
  int minFuzzyLength = (params.get(MIN_FUZZY_LENGTH) != null)
  ? Integer.parseInt(params.get(MIN_FUZZY_LENGTH).toString())
  :FuzzySuggester.DEFAULT_MIN_FUZZY_LENGTH;
  
  boolean unicodeAware = (params.get(UNICODE_AWARE) != null)
  ? Boolean.valueOf(params.get(UNICODE_AWARE).toString())
  : FuzzySuggester.DEFAULT_UNICODE_AWARE;
  
  return new FuzzySuggester(getTempDir(), "suggester", indexAnalyzer, queryAnalyzer, options, maxSurfaceFormsPerAnalyzedForm,
      maxGraphExpansions, preservePositionIncrements, maxEdits, transpositions, nonFuzzyPrefix,
      minFuzzyLength, unicodeAware);
}
 
Example 9
Source File: AnalyzingLookupFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Lookup create(@SuppressWarnings({"rawtypes"})NamedList params, SolrCore core) {
  // mandatory parameter
  Object fieldTypeName = params.get(QUERY_ANALYZER);
  if (fieldTypeName == null) {
    throw new IllegalArgumentException("Error in configuration: " + QUERY_ANALYZER + " parameter is mandatory");
  }
  FieldType ft = core.getLatestSchema().getFieldTypeByName(fieldTypeName.toString());
  if (ft == null) {
    throw new IllegalArgumentException("Error in configuration: " + fieldTypeName.toString() + " is not defined in the schema");
  }
  
  Analyzer indexAnalyzer = ft.getIndexAnalyzer();
  Analyzer queryAnalyzer = ft.getQueryAnalyzer();
  
  // optional parameters
  
  boolean exactMatchFirst = params.get(EXACT_MATCH_FIRST) != null
  ? Boolean.valueOf(params.get(EXACT_MATCH_FIRST).toString())
  : true;
  
  boolean preserveSep = params.get(PRESERVE_SEP) != null
  ? Boolean.valueOf(params.get(PRESERVE_SEP).toString())
  : true;
  
  int flags = 0;
  if (exactMatchFirst) {
    flags |= AnalyzingSuggester.EXACT_FIRST;
  }
  if (preserveSep) {
    flags |= AnalyzingSuggester.PRESERVE_SEP;
  }
  
  int maxSurfaceFormsPerAnalyzedForm = params.get(MAX_SURFACE_FORMS) != null
  ? Integer.parseInt(params.get(MAX_SURFACE_FORMS).toString())
  : 256;
  
  int maxGraphExpansions = params.get(MAX_EXPANSIONS) != null
  ? Integer.parseInt(params.get(MAX_EXPANSIONS).toString())
  : -1;
  
  boolean preservePositionIncrements = params.get(PRESERVE_POSITION_INCREMENTS) != null
  ? Boolean.valueOf(params.get(PRESERVE_POSITION_INCREMENTS).toString())
  : false;

  return new AnalyzingSuggester(getTempDir(), "suggester", indexAnalyzer, queryAnalyzer, flags, maxSurfaceFormsPerAnalyzedForm,
      maxGraphExpansions, preservePositionIncrements);
}