Java Code Examples for org.apache.solr.schema.FieldType

The following examples show how to use org.apache.solr.schema.FieldType. These examples are extracted from open source projects. 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
/**
 * 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
@Test
public void analyzerInCaseTheHighlightingFieldTypeExists()
{
    String language = "fr";
    String fieldTypeName = classUnderTest.highlightingFieldTypeName(language);

    FieldType highlightedFrenchFieldType = mock(FieldType.class);
    Analyzer queryTimeAnalyzer = mock(Analyzer.class);
    Analyzer indexTimeAnalyzer = mock(Analyzer.class);

    when(highlightedFrenchFieldType.getIndexAnalyzer()).thenReturn(indexTimeAnalyzer);
    when(highlightedFrenchFieldType.getQueryAnalyzer()).thenReturn(queryTimeAnalyzer);
    when(schema.getFieldTypeByName(fieldTypeName)).thenReturn(highlightedFrenchFieldType);

    assertSame(indexTimeAnalyzer, classUnderTest.analyzer(language));

    classUnderTest = new LanguagePrefixedTokenStream(schema, "aFieldName", AlfrescoAnalyzerWrapper.Mode.QUERY);

    assertSame(queryTimeAnalyzer, classUnderTest.analyzer(language));
}
 
Example 3
@Test
public void analyzerInCaseTheHighlightingFieldTypeDoesntExists_shouldFallbackToLocalisedFieldType()
{
    String language = "fr";
    String highlightingFieldTypeName = classUnderTest.highlightingFieldTypeName(language);
    String localisedFieldTypeName = classUnderTest.localisedFieldTypeName(language);

    FieldType localisedFieldType = mock(FieldType.class);
    Analyzer queryTimeAnalyzer = mock(Analyzer.class);
    Analyzer indexTimeAnalyzer = mock(Analyzer.class);

    when(localisedFieldType.getIndexAnalyzer()).thenReturn(indexTimeAnalyzer);
    when(localisedFieldType.getQueryAnalyzer()).thenReturn(queryTimeAnalyzer);
    when(schema.getFieldTypeByName(highlightingFieldTypeName)).thenReturn(null);
    when(schema.getFieldTypeByName(localisedFieldTypeName)).thenReturn(localisedFieldType);

    assertSame(indexTimeAnalyzer, classUnderTest.analyzer(language));

    classUnderTest = new LanguagePrefixedTokenStream(schema, "aFieldName", AlfrescoAnalyzerWrapper.Mode.QUERY);

    assertSame(queryTimeAnalyzer, classUnderTest.analyzer(language));
}
 
Example 4
@Test
public void analyzerInCaseHighlightAndLocalisedFieldsDontExist_shouldFallbackToTextGeneral()
{
    String language = "fr";
    String highlightingFieldTypeName = classUnderTest.highlightingFieldTypeName(language);
    String localisedFieldTypeName = classUnderTest.localisedFieldTypeName(language);

    FieldType textGeneralFieldType = mock(FieldType.class);
    Analyzer queryTimeAnalyzer = mock(Analyzer.class);
    Analyzer indexTimeAnalyzer = mock(Analyzer.class);

    when(textGeneralFieldType.getIndexAnalyzer()).thenReturn(indexTimeAnalyzer);
    when(textGeneralFieldType.getQueryAnalyzer()).thenReturn(queryTimeAnalyzer);
    when(schema.getFieldTypeByName(highlightingFieldTypeName)).thenReturn(null);
    when(schema.getFieldTypeByName(localisedFieldTypeName)).thenReturn(null);
    when(schema.getFieldTypeByName(FALLBACK_TEXT_FIELD_TYPE_NAME)).thenReturn(textGeneralFieldType);

    assertSame(indexTimeAnalyzer, classUnderTest.analyzer(language));

    classUnderTest = new LanguagePrefixedTokenStream(schema, "aFieldName", AlfrescoAnalyzerWrapper.Mode.QUERY);

    assertSame(queryTimeAnalyzer, classUnderTest.analyzer(language));
}
 
Example 5
Source Project: lucene-solr   Source File: SolrQueryParserBase.java    License: Apache License 2.0 6 votes vote down vote up
protected ReversedWildcardFilterFactory getReversedWildcardFilterFactory(FieldType fieldType) {
  if (leadingWildcards == null) leadingWildcards = new HashMap<>();
  ReversedWildcardFilterFactory fac = leadingWildcards.get(fieldType);
  if (fac != null || leadingWildcards.containsKey(fieldType)) {
    return fac;
  }

  Analyzer a = fieldType.getIndexAnalyzer();
  if (a instanceof TokenizerChain) {
    // examine the indexing analysis chain if it supports leading wildcards
    TokenizerChain tc = (TokenizerChain)a;
    TokenFilterFactory[] factories = tc.getTokenFilterFactories();
    for (TokenFilterFactory factory : factories) {
      if (factory instanceof ReversedWildcardFilterFactory) {
        fac = (ReversedWildcardFilterFactory)factory;
        break;
      }
    }
  }

  leadingWildcards.put(fieldType, fac);
  return fac;
}
 
Example 6
Source Project: lucene-solr   Source File: SolrQueryParserBase.java    License: 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 7
Source Project: lucene-solr   Source File: SimpleFacets.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Computes the term-&gt;count counts for the specified term values relative to the
 *
 * @param field the name of the field to compute term counts against
 * @param parsed contains the docset to compute term counts relative to
 * @param terms a list of term values (in the specified field) to compute the counts for
 */
protected NamedList<Integer> getListedTermCounts(String field, final ParsedParams parsed, List<String> terms)
    throws IOException {
  final String sort = parsed.params.getFieldParam(field, FacetParams.FACET_SORT, "empty");
  final SchemaField sf = searcher.getSchema().getField(field);
  final FieldType ft = sf.getType();
  final DocSet baseDocset = parsed.docs;
  final NamedList<Integer> res = new NamedList<>();
  Stream<String> inputStream = terms.stream();
  if (sort.equals(FacetParams.FACET_SORT_INDEX)) { // it might always make sense
    inputStream = inputStream.sorted();
  }
  Stream<SimpleImmutableEntry<String,Integer>> termCountEntries = inputStream
      .map((term) -> new SimpleImmutableEntry<>(term, numDocs(term, sf, ft, baseDocset)));
  if (sort.equals(FacetParams.FACET_SORT_COUNT)) {
    termCountEntries = termCountEntries.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
  }
  termCountEntries.forEach(e -> res.add(e.getKey(), e.getValue()));
  return res;
}
 
Example 8
Source Project: lucene-solr   Source File: FreeTextLookupFactory.java    License: 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 9
Source Project: kite   Source File: TokenizeTextBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public TokenizeText(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {
  super(builder, config, parent, child, context);
  this.inputFieldName = getConfigs().getString(config, "inputField");
  this.outputFieldName = getConfigs().getString(config, "outputField");      
  String solrFieldType = getConfigs().getString(config, "solrFieldType");      
  Config solrLocatorConfig = getConfigs().getConfig(config, "solrLocator");
  SolrLocator locator = new SolrLocator(solrLocatorConfig, context);
  LOG.debug("solrLocator: {}", locator);
  IndexSchema schema = locator.getIndexSchema();
  FieldType fieldType = schema.getFieldTypeByName(solrFieldType);
  if (fieldType == null) {
    throw new MorphlineCompilationException("Missing Solr field type in schema.xml for name: " + solrFieldType, config);
  }
  this.analyzer = fieldType.getIndexAnalyzer();
  Preconditions.checkNotNull(analyzer);
  // register CharTermAttribute for later (implicit) reuse
  this.token = analyzer.tokenStream("content", reader).addAttribute(CharTermAttribute.class);
  Preconditions.checkNotNull(token);
  validateArguments();
}
 
Example 10
Source Project: mtas   Source File: MtasSolrComponentFacet.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Gets the field type.
 *
 * @param schema the schema
 * @param field the field
 * @return the field type
 * @throws IOException Signals that an I/O exception has occurred.
 */
private String getFieldType(IndexSchema schema, String field)
    throws IOException {
  SchemaField sf = schema.getField(field);
  FieldType ft = sf.getType();
  if (ft != null) {
    if (ft.isPointField() && !sf.hasDocValues()) {
      return ComponentFacet.TYPE_POINTFIELD_WITHOUT_DOCVALUES;
    }
    NumberType nt = ft.getNumberType();
    if (nt != null) {
      return nt.name();
    } else {
      return ComponentFacet.TYPE_STRING;
    }
  } else {
    // best guess
    return ComponentFacet.TYPE_STRING;
  }
}
 
Example 11
Source Project: lucene-solr   Source File: MultiFieldWriter.java    License: Apache License 2.0 6 votes vote down vote up
static LongFunction<Object> bitsToValue(FieldType fieldType) {
  switch (fieldType.getNumberType()) {
    case LONG:
      return (bits)-> bits;
    case DATE:
      return (bits)-> new Date(bits);
    case INTEGER:
      return (bits)-> (int)bits;
    case FLOAT:
      return (bits)-> NumericUtils.sortableIntToFloat((int)bits);
    case DOUBLE:
      return (bits)-> NumericUtils.sortableLongToDouble(bits);
    default:
      throw new AssertionError("Unsupported NumberType: " + fieldType.getNumberType());
  }
}
 
Example 12
Source Project: BioSolr   Source File: XJoinQParserPlugin.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Query parse() throws SyntaxError {
  Method method = Method.valueOf(localParams.get(METHOD, Method.termsFilter.name()));
  JoinSpec<T> js = JoinSpec.parse(localParams.get(QueryParsing.V));
  Iterator<T> it = js.iterator(this);
  if (joinField == null) {
    throw new Exception("No XJoin component referenced by query");
  }
  FieldType ft = req.getSchema().getFieldTypeNoEx(joinField);
  Iterator<BytesRef> bytesRefs = new TransformIterator(it, transformer(ft));
  if (! bytesRefs.hasNext()) {
    return new BooleanQuery.Builder().build(); // matches nothing
  }
  Query query = method.makeQuery(joinField, bytesRefs);
  return new SolrConstantScoreQuery(new QueryWrapperFilter(query));
}
 
Example 13
Source Project: lucene-solr   Source File: GeoDistValueSourceParser.java    License: Apache License 2.0 6 votes vote down vote up
private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
  String sfield = fp.getParam(SpatialParams.FIELD);
  if (sfield == null) return null;
  SchemaField sf = fp.getReq().getSchema().getField(sfield);
  FieldType type = sf.getType();
  if (type instanceof AbstractSpatialFieldType) {
    @SuppressWarnings({"rawtypes"})
    AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
    return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
  }
  ValueSource vs = type.getValueSource(sf, fp);
  if (vs instanceof MultiValueSource) {
    return (MultiValueSource)vs;
  }
  throw new SyntaxError("Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}
 
Example 14
Source Project: lucene-solr   Source File: CommandHandler.java    License: Apache License 2.0 6 votes vote down vote up
private DocSet computeGroupedDocSet(Query query, ProcessedFilter filter, List<Collector> collectors) throws IOException {
  @SuppressWarnings({"rawtypes"})
  Command firstCommand = commands.get(0);
  String field = firstCommand.getKey();
  SchemaField sf = searcher.getSchema().getField(field);
  FieldType fieldType = sf.getType();
  
  @SuppressWarnings({"rawtypes"})
  final AllGroupHeadsCollector allGroupHeadsCollector;
  if (fieldType.getNumberType() != null) {
    ValueSource vs = fieldType.getValueSource(sf, null);
    allGroupHeadsCollector = AllGroupHeadsCollector.newCollector(new ValueSourceGroupSelector(vs, new HashMap<>()),
        firstCommand.getWithinGroupSort());
  } else {
    allGroupHeadsCollector
        = AllGroupHeadsCollector.newCollector(new TermGroupSelector(firstCommand.getKey()), firstCommand.getWithinGroupSort());
  }
  if (collectors.isEmpty()) {
    searchWithTimeLimiter(query, filter, allGroupHeadsCollector);
  } else {
    collectors.add(allGroupHeadsCollector);
    searchWithTimeLimiter(query, filter, MultiCollector.wrap(collectors.toArray(new Collector[collectors.size()])));
  }

  return new BitDocSet(allGroupHeadsCollector.retrieveGroupHeads(searcher.maxDoc()));
}
 
Example 15
Source Project: lucene-solr   Source File: TopGroupsFieldCommand.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public List<Collector> create() throws IOException {
  if (firstPhaseGroups.isEmpty()) {
    return Collections.emptyList();
  }

  final List<Collector> collectors = new ArrayList<>(1);
  final FieldType fieldType = field.getType();
  if (fieldType.getNumberType() != null) {
    ValueSource vs = fieldType.getValueSource(field, null);
    Collection<SearchGroup<MutableValue>> v = GroupConverter.toMutable(field, firstPhaseGroups);
    secondPassCollector = new TopGroupsCollector<>(new ValueSourceGroupSelector(vs, new HashMap<>()),
        v, groupSort, withinGroupSort, maxDocPerGroup, needMaxScore
    );
  } else {
    secondPassCollector = new TopGroupsCollector<>(new TermGroupSelector(field.getName()),
        firstPhaseGroups, groupSort, withinGroupSort, maxDocPerGroup, needMaxScore
    );
  }
  collectors.add(secondPassCollector);
  return collectors;
}
 
Example 16
Source Project: lucene-solr   Source File: TopGroupsFieldCommand.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void postCollect(IndexSearcher searcher) throws IOException {
  if (firstPhaseGroups.isEmpty()) {
    topGroups = new TopGroups<>(groupSort.getSort(), withinGroupSort.getSort(), 0, 0, new GroupDocs[0], Float.NaN);
    return;
  }

  FieldType fieldType = field.getType();
  if (fieldType.getNumberType() != null) {
    topGroups = GroupConverter.fromMutable(field, secondPassCollector.getTopGroups(0));
  } else {
    topGroups = secondPassCollector.getTopGroups(0);
  }
  if (needScores) {
    for (GroupDocs<?> group : topGroups.groups) {
      TopFieldCollector.populateScores(group.scoreDocs, searcher, query);
    }
  }
}
 
Example 17
Source Project: lucene-solr   Source File: GroupConverter.java    License: Apache License 2.0 6 votes vote down vote up
static Collection<SearchGroup<BytesRef>> fromMutable(SchemaField field, Collection<SearchGroup<MutableValue>> values) {
  if (values == null) {
    return null;
  }
  FieldType fieldType = field.getType();
  List<SearchGroup<BytesRef>> result = new ArrayList<>(values.size());
  for (SearchGroup<MutableValue> original : values) {
    SearchGroup<BytesRef> converted = new SearchGroup<>();
    converted.sortValues = original.sortValues;
    if (original.groupValue.exists) {
      BytesRefBuilder binary = new BytesRefBuilder();
      fieldType.readableToIndexed(Utils.OBJECT_TO_STRING.apply(original.groupValue.toObject()), binary);
      converted.groupValue = binary.get();
    } else {
      converted.groupValue = null;
    }
    result.add(converted);
  }
  return result;
}
 
Example 18
Source Project: BioSolr   Source File: XJoinQParserPlugin.java    License: Apache License 2.0 6 votes vote down vote up
static private Transformer transformer(final FieldType ft) {
  return new Transformer() {
    
    BytesRefBuilder term = new BytesRefBuilder();
    
    @Override
    public BytesRef transform(Object joinId) {
      String joinStr = joinId.toString();
      // logic same as TermQParserPlugin
      if (ft != null) {
        ft.readableToIndexed(joinStr, term);
      } else {
        term.copyChars(joinStr);
      }
      return term.toBytesRef();
    }
    
  };
}
 
Example 19
Source Project: lucene-solr   Source File: HashQParserPlugin.java    License: Apache License 2.0 6 votes vote down vote up
public DelegatingCollector getFilterCollector(IndexSearcher indexSearcher) {
  HashKey[] hashKeys = new HashKey[keys.length];
  SolrIndexSearcher searcher = (SolrIndexSearcher)indexSearcher;
  IndexSchema schema = searcher.getSchema();
  for(int i=0; i<keys.length; i++) {
    String key = keys[i];
    FieldType ft = schema.getField(key).getType();
    HashKey h = null;
    if(ft instanceof StrField) {
      h = new BytesHash(key, ft);
    } else {
      h = new NumericHash(key);
    }
    hashKeys[i] = h;
  }
  HashKey k = (hashKeys.length > 1) ? new CompositeHash(hashKeys) : hashKeys[0];
  return new HashCollector(k, workers, worker);
}
 
Example 20
Source Project: lucene-solr   Source File: TestRetrieveFieldsOptimizer.java    License: Apache License 2.0 6 votes vote down vote up
String getValAsString(Object val) {

    FieldType fieldType = schemaField.getType();

    //Why do mutliValued date fields get here as Strings whereas single-valued fields are Dates?
    // Why do BoolFields sometimes get here as "F" or "T"?
    if (val instanceof String) {
      if (fieldType instanceof TrieDateField || fieldType instanceof DatePointField) {
        long lVal = Long.parseLong((String) val);
        return (new Date(lVal).toInstant().toString());
      }
      if (fieldType instanceof BoolField) {
        if (val.equals("F")) return "false";
        if (val.equals("T")) return "true";
      }
      return (String) val;
    }
    if (fieldType instanceof TrieDateField || fieldType instanceof DatePointField) {
      return ((Date) val).toInstant().toString();
    }

    return val.toString();
  }
 
Example 21
Source Project: BioSolr   Source File: XJoinQParserPlugin.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Query parse() throws SyntaxError {
  Method method = Method.valueOf(localParams.get(METHOD, Method.termsFilter.name()));
  JoinSpec<T> js = JoinSpec.parse(localParams.get(QueryParsing.V));
  Iterator<T> it = js.iterator(this);
  if (joinField == null) {
    throw new Exception("No XJoin component referenced by query");
  }
  FieldType ft = req.getSchema().getFieldTypeNoEx(joinField);
  Iterator<BytesRef> bytesRefs = new TransformIterator(it, transformer(ft));
  if (! bytesRefs.hasNext()) {
    return new BooleanQuery(); // matches nothing
  }
  return new SolrConstantScoreQuery(method.makeFilter(joinField, bytesRefs));
}
 
Example 22
Source Project: lucene-solr   Source File: ExpandComponent.java    License: Apache License 2.0 6 votes vote down vote up
private String numericToString(FieldType fieldType, long val) {
  if (fieldType.getNumberType() != null) {
    switch (fieldType.getNumberType()) {
      case INTEGER:
      case LONG:
        return Long.toString(val);
      case FLOAT:
        return Float.toString(Float.intBitsToFloat((int)val));
      case DOUBLE:
        return Double.toString(Double.longBitsToDouble(val));
      case DATE:
        break;
    }
  }
  throw new IllegalArgumentException("FieldType must be INT,LONG,FLOAT,DOUBLE found " + fieldType);
}
 
Example 23
Source Project: jesterj   Source File: PreAnalyzeFields.java    License: Apache License 2.0 6 votes vote down vote up
public PreAnalyzeFields build() {
  final SolrSchemaUtil util = new SolrSchemaUtil();

  try {
    int endIndex = schemaFile.lastIndexOf("/");
    String subpath;
    if (endIndex > 0) {
      subpath = schemaFile.substring(0, endIndex + 1);
      schemaFile = schemaFile.substring(endIndex + 1);
    } else {
      subpath = "";
    }
    obj.loader = new ClassSubPathResourceLoader(obj.classLoaderProvider.get(), subpath);
    org.w3c.dom.Document doc = util.getSchemaDocument(schemaFile, obj.loader);
    FieldType ft = util.getFieldType(doc, typeName, luceneMatch, schemaVersion, obj.loader);
    obj.analyzerFactory = ft::getIndexAnalyzer;
  } catch (IllegalAccessException | InstantiationException | ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
    throw new RuntimeException(e);
  }

  PreAnalyzeFields built = getObj();
  setObj(new PreAnalyzeFields());
  return built;
}
 
Example 24
Source Project: customized-symspell   Source File: CustomSpellCheckListner.java    License: MIT License 5 votes vote down vote up
/**
 * Relod method of spellcheck listner
 * @param newSearcher
 * @param checker
 * @throws IOException
 * @throws SpellCheckException
 */
public void reload(SolrIndexSearcher newSearcher, SpellChecker checker)
    throws IOException, SpellCheckException {

  DirectoryReader productsIndexReader = newSearcher.getIndexReader();
  Fields fields = MultiFields.getFields(productsIndexReader);
  IndexSchema schema = newSearcher.getCore().getLatestSchema();
  long time = System.currentTimeMillis();
  for (String field : fields) {
    if (!fieldArr.contains(field)) {
      continue;
    }
    FieldType type = schema.getField(field).getType();
    int insertionsCount = 0;
    for (TermsEnum iterator = fields.terms(field).iterator(); iterator.next() != null; ) {
      BytesRef term = iterator.term();
      CharsRefBuilder charsRefBuilder = new CharsRefBuilder();
      type.indexedToReadable(term, charsRefBuilder);
      insertionsCount++;
      checker.getDataHolder().addItem(
          new DictionaryItem(charsRefBuilder.toString().trim(), (double) iterator.totalTermFreq(),
              0.0));
    }
    log.info("Spellcheck Dictionary populated for Field Name {}, Count {}", field,
        insertionsCount);
  }
  log.info("Data for SpellChecker  was populated. Time={} ms",
      (System.currentTimeMillis() - time));
}
 
Example 25
Source Project: ltr4l   Source File: DefaultLTRQParserPlugin.java    License: 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 26
private Analyzer selectAnalyzer(FieldType fieldType) {
	 if(mode == Mode.INDEX)
	 {
		 return fieldType.getIndexAnalyzer();
	 }
	 else if(mode == Mode.QUERY)
	 {
		 return fieldType.getQueryAnalyzer();
	 }
	 else
	 {
		 return null;
	 }
}
 
Example 27
Source Project: lucene-solr   Source File: FacetRangeGenerator.java    License: Apache License 2.0 5 votes vote down vote up
public static FacetRangeGenerator<? extends Comparable<?>> create(RangeFacet rangeFacet){
  final SchemaField sf = rangeFacet.getField();
  final FieldType ft = sf.getType();
  final FacetRangeGenerator<?> calc;
  if (ft instanceof NumericFieldType) {
    switch (ft.getNumberType()) {
      case FLOAT:
        calc = new FloatFacetRangeGenerator(rangeFacet);
        break;
      case DOUBLE:
        calc = new DoubleFacetRangeGenerator(rangeFacet);
        break;
      case INTEGER:
        calc = new IntegerFacetRangeGenerator(rangeFacet);
        break;
      case LONG:
        calc = new LongFacetRangeGenerator(rangeFacet);
        break;
      case DATE:
        calc = new DateFacetRangeGenerator(rangeFacet, null);
        break;
      default:
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on numeric field of unexpected type: " + sf.getName());
    }
  } else {
    throw new SolrException (SolrException.ErrorCode.BAD_REQUEST, "Unable to range facet on non-numeric field: " + sf);
  }
  return calc;
}
 
Example 28
Source Project: lucene-solr   Source File: SolrQueryParserBase.java    License: Apache License 2.0 5 votes vote down vote up
protected String analyzeIfMultitermTermText(String field, String part, FieldType fieldType) {

    if (part == null || ! (fieldType instanceof TextField) || ((TextField)fieldType).getMultiTermAnalyzer() == null) return part;

    SchemaField sf = schema.getFieldOrNull((field));
    if (sf == null || ! (fieldType instanceof TextField)) return part;
    BytesRef out = TextField.analyzeMultiTerm(field, part, ((TextField)fieldType).getMultiTermAnalyzer());
    return out == null ? part : out.utf8ToString();
  }
 
Example 29
Source Project: lucene-solr   Source File: TestRetrieveFieldsOptimizer.java    License: Apache License 2.0 5 votes vote down vote up
IndexSchema addFieldTypes(IndexSchema schema) {
  List<FieldType> typesToAdd = new ArrayList<>();
  for (RetrieveFieldType testType : testTypes.values()) {
    typesToAdd.add(testType.getFieldType());
  }
  return schema.addFieldTypes(typesToAdd, false);
}
 
Example 30
Source Project: lucene-solr   Source File: SimpleFacets.java    License: Apache License 2.0 5 votes vote down vote up
private int numDocs(String term, final SchemaField sf, final FieldType ft, final DocSet baseDocset) {
  try {
    return searcher.numDocs(ft.getFieldQuery(null, sf, term), baseDocset);
  } catch (IOException e1) {
    throw new RuntimeException(e1);
  }
}