Search in sources :

Example 1 with SuggestField

use of org.apache.lucene.search.suggest.document.SuggestField in project elasticsearch by elastic.

the class CompletionFieldMapper method parse.

/**
     * Parses and indexes inputs
     *
     * Parsing:
     *  Acceptable format:
     *   "STRING" - interpreted as field value (input)
     *   "ARRAY" - each element can be one of "OBJECT" (see below)
     *   "OBJECT" - { "input": STRING|ARRAY, "weight": STRING|INT, "contexts": ARRAY|OBJECT }
     *
     * Indexing:
     *  if context mappings are defined, delegates to {@link ContextMappings#addField(ParseContext.Document, String, String, int, Map)}
     *  else adds inputs as a {@link org.apache.lucene.search.suggest.document.SuggestField}
     */
@Override
public Mapper parse(ParseContext context) throws IOException {
    // parse
    XContentParser parser = context.parser();
    Token token = parser.currentToken();
    Map<String, CompletionInputMetaData> inputMap = new HashMap<>(1);
    if (token == Token.VALUE_NULL) {
        throw new MapperParsingException("completion field [" + fieldType().name() + "] does not support null values");
    } else if (token == Token.START_ARRAY) {
        while ((token = parser.nextToken()) != Token.END_ARRAY) {
            parse(context, token, parser, inputMap);
        }
    } else {
        parse(context, token, parser, inputMap);
    }
    // index
    for (Map.Entry<String, CompletionInputMetaData> completionInput : inputMap.entrySet()) {
        String input = completionInput.getKey();
        // truncate input
        if (input.length() > maxInputLength) {
            int len = Math.min(maxInputLength, input.length());
            if (Character.isHighSurrogate(input.charAt(len - 1))) {
                assert input.length() >= len + 1 && Character.isLowSurrogate(input.charAt(len));
                len += 1;
            }
            input = input.substring(0, len);
        }
        CompletionInputMetaData metaData = completionInput.getValue();
        if (fieldType().hasContextMappings()) {
            fieldType().getContextMappings().addField(context.doc(), fieldType().name(), input, metaData.weight, metaData.contexts);
        } else {
            context.doc().add(new SuggestField(fieldType().name(), input, metaData.weight));
        }
    }
    multiFields.parse(this, context);
    return null;
}
Also used : SuggestField(org.apache.lucene.search.suggest.document.SuggestField) HashMap(java.util.HashMap) Token(org.elasticsearch.common.xcontent.XContentParser.Token) HashMap(java.util.HashMap) Map(java.util.Map) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 2 with SuggestField

use of org.apache.lucene.search.suggest.document.SuggestField in project elasticsearch by elastic.

the class CompletionFieldMapperTests method assertSuggestFields.

private static void assertSuggestFields(IndexableField[] fields, int expected) {
    int actualFieldCount = 0;
    for (IndexableField field : fields) {
        if (field instanceof SuggestField) {
            actualFieldCount++;
        }
    }
    assertThat(actualFieldCount, equalTo(expected));
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) SuggestField(org.apache.lucene.search.suggest.document.SuggestField)

Aggregations

SuggestField (org.apache.lucene.search.suggest.document.SuggestField)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IndexableField (org.apache.lucene.index.IndexableField)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 Token (org.elasticsearch.common.xcontent.XContentParser.Token)1