@Override
    public int getOffsetGap(Fieldable field) {
      String fieldName = field.name();
      int dotIndex = fieldName.indexOf('.');
      if (dotIndex != -1) {
        String possibleType = fieldName.substring(0, dotIndex);
        DocumentMapper possibleDocMapper = mappers.get(possibleType);
        if (possibleDocMapper != null) {
          return possibleDocMapper.mappers().searchAnalyzer().getOffsetGap(field);
        }
      }
      FieldMappers mappers = fullNameFieldMappers.get(fieldName);
      if (mappers != null
          && mappers.mapper() != null
          && mappers.mapper().searchAnalyzer() != null) {
        return mappers.mapper().searchAnalyzer().getOffsetGap(field);
      }

      mappers = indexNameFieldMappers.get(fieldName);
      if (mappers != null
          && mappers.mapper() != null
          && mappers.mapper().searchAnalyzer() != null) {
        return mappers.mapper().searchAnalyzer().getOffsetGap(field);
      }
      return defaultAnalyzer.getOffsetGap(field);
    }
Esempio n. 2
0
  /**
   * Convenience method; Tokenizes the given field text and adds the resulting terms to the index;
   * Equivalent to adding an indexed non-keyword Lucene {@link org.apache.lucene.document.Field}
   * that is tokenized, not stored, termVectorStored with positions (or termVectorStored with
   * positions and offsets),
   *
   * @param fieldName a name to be associated with the text
   * @param text the text to tokenize and index.
   * @param analyzer the analyzer to use for tokenization
   */
  public void addField(String fieldName, String text, Analyzer analyzer) {
    if (fieldName == null) throw new IllegalArgumentException("fieldName must not be null");
    if (text == null) throw new IllegalArgumentException("text must not be null");
    if (analyzer == null) throw new IllegalArgumentException("analyzer must not be null");

    TokenStream stream;
    try {
      stream = analyzer.tokenStream(fieldName, text);
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }

    addField(
        fieldName,
        stream,
        1.0f,
        analyzer.getPositionIncrementGap(fieldName),
        analyzer.getOffsetGap(fieldName));
  }