org.apache.solr.schema.FieldType Java Examples

The following examples show how to use org.apache.solr.schema.FieldType. 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: MultiFieldWriter.java    From lucene-solr with 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 #2
Source File: LanguagePrefixedTokenStreamTest.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #3
Source File: LanguagePrefixedTokenStreamTest.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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
Source File: LanguagePrefixedTokenStreamTest.java    From SearchServices with GNU Lesser General Public License v3.0 6 votes vote down vote up
@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 #5
Source File: XJoinQParserPlugin.java    From BioSolr with 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 #6
Source File: ExpandComponent.java    From lucene-solr with 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 #7
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 #8
Source File: SolrQueryParserBase.java    From lucene-solr with 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 #9
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 #10
Source File: TestRetrieveFieldsOptimizer.java    From lucene-solr with 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 #11
Source File: HashQParserPlugin.java    From lucene-solr with 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 #12
Source File: SimpleFacets.java    From lucene-solr with 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 #13
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 #14
Source File: PreAnalyzeFields.java    From jesterj with 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 #15
Source File: XJoinQParserPlugin.java    From BioSolr with 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 #16
Source File: GeoDistValueSourceParser.java    From lucene-solr with 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 #17
Source File: MtasSolrComponentFacet.java    From mtas with 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 #18
Source File: TokenizeTextBuilder.java    From kite with 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 #19
Source File: CommandHandler.java    From lucene-solr with 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 #20
Source File: TopGroupsFieldCommand.java    From lucene-solr with 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 #21
Source File: TopGroupsFieldCommand.java    From lucene-solr with 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 #22
Source File: GroupConverter.java    From lucene-solr with 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 #23
Source File: XJoinQParserPlugin.java    From BioSolr with 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 #24
Source File: TaggerRequestHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean fieldHasIndexedStopFilter(String field, SolrQueryRequest req) {
  FieldType fieldType = req.getSchema().getFieldType(field);
  Analyzer analyzer = fieldType.getIndexAnalyzer();//index analyzer
  if (analyzer instanceof TokenizerChain) {
    TokenizerChain tokenizerChain = (TokenizerChain) analyzer;
    TokenFilterFactory[] tokenFilterFactories = tokenizerChain.getTokenFilterFactories();
    for (TokenFilterFactory tokenFilterFactory : tokenFilterFactories) {
      if (tokenFilterFactory instanceof StopFilterFactory)
        return true;
    }
  }
  return false;
}
 
Example #25
Source File: CursorMark.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a Base64 encoded serialized representation of the sort values 
 * encapsulated by this object, for use in cursor requests.
 *
 * @see #parseSerializedTotem
 */
public String getSerializedTotem() {
  if (null == this.values) {
    return CURSOR_MARK_START;
  }

  final List<SchemaField> schemaFields = sortSpec.getSchemaFields();
  final ArrayList<Object> marshalledValues = new ArrayList<>(values.size()+1);
  for (int i = 0; i < schemaFields.size(); i++) {
    SchemaField fld = schemaFields.get(i);
    Object safeValue = values.get(i);
    if (null != fld) {
      FieldType type = fld.getType();
      safeValue = type.marshalSortValue(safeValue);
    }
    marshalledValues.add(safeValue);
  }

  // TODO: we could also encode info about the SortSpec for error checking:
  // the type/name/dir from the SortFields (or a hashCode to act as a checksum) 
  // could help provide more validation beyond just the number of clauses.

  try (JavaBinCodec jbc = new JavaBinCodec(); ByteArrayOutputStream out = new ByteArrayOutputStream(256)) {
    jbc.marshal(marshalledValues, out);
    byte[] rawData = out.toByteArray();
    return Base64.byteArrayToBase64(rawData, 0, rawData.length);
  } catch (Exception ex) {
    throw new SolrException(ErrorCode.SERVER_ERROR,
                            "Unable to format search after totem", ex);
  }
}
 
Example #26
Source File: SchemaSimilarityFactory.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Similarity get(String name) {
  FieldType fieldType = core.getLatestSchema().getFieldTypeNoEx(name);
  if (fieldType == null) {
    return defaultSimilarity;
  } else {
    Similarity similarity = fieldType.getSimilarity();
    return similarity == null ? defaultSimilarity : similarity;
  }
}
 
Example #27
Source File: LukeRequestHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static void populateFieldInfo(IndexSchema schema,
                                      Map<String, List<String>> typeusemap, Map<String, Object> fields,
                                      SchemaField uniqueField, SchemaField f) {
  FieldType ft = f.getType();
  SimpleOrderedMap<Object> field = new SimpleOrderedMap<>();
  field.add( "type", ft.getTypeName() );
  field.add( "flags", getFieldFlags(f) );
  if( f.isRequired() ) {
    field.add( "required", f.isRequired() );
  }
  if( f.getDefaultValue() != null ) {
    field.add( "default", f.getDefaultValue() );
  }
  if (f == uniqueField){
    field.add("uniqueKey", true);
  }
  if (ft.getIndexAnalyzer().getPositionIncrementGap(f.getName()) != 0) {
    field.add("positionIncrementGap", ft.getIndexAnalyzer().getPositionIncrementGap(f.getName()));
  }
  field.add("copyDests", toListOfStringDests(schema.getCopyFieldsList(f.getName())));
  field.add("copySources", schema.getCopySources(f.getName()));


  fields.put( f.getName(), field );

  List<String> v = typeusemap.get( ft.getTypeName() );
  if( v == null ) {
    v = new ArrayList<>();
  }
  v.add( f.getName() );
  typeusemap.put( ft.getTypeName(), v );
}
 
Example #28
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 #29
Source File: FieldMutatingUpdateProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean shouldMutateBasedOnSchema(final String fieldName, IndexSchema schema) {
  // order of checks is based on what should be quicker
  // (ie: set lookups faster the looping over instanceOf / matches tests
  
  if ( ! (params.fieldName.isEmpty() || params.fieldName.contains(fieldName)) ) {
    return false;
  }
  
  // do not consider it an error if the fieldName has no type
  // there might be another processor dealing with it later
  FieldType t =  schema.getFieldTypeNoEx(fieldName);
  final boolean fieldExists = (null != t);

  if ( (null != params.fieldNameMatchesSchemaField) &&
       (fieldExists != params.fieldNameMatchesSchemaField) ) {
    return false;
  }

  if (fieldExists) { 

    if (! (params.typeName.isEmpty() || params.typeName.contains(t.getTypeName())) ) {
      return false;
    }
    
    if (! (classes.isEmpty() || instanceOfAny(t, classes)) ) {
      return false;
    }
  } 
  
  if (! (params.fieldRegex.isEmpty() || matchesAny(fieldName, params.fieldRegex)) ) {
    return false;
  }
  
  return true;
}
 
Example #30
Source File: CustomSpellCheckListner.java    From customized-symspell with 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));
}