Search in sources :

Example 41 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testParsingFull.

public void testParsingFull() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject().startArray("completion").startObject().field("input", "suggestion1").field("weight", 3).endObject().startObject().field("input", "suggestion2").field("weight", 4).endObject().startObject().field("input", "suggestion3").field("weight", 5).endObject().endArray().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 42 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testCompletionAnalyzerSettings.

public void testCompletionAnalyzerSettings() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").field("analyzer", "simple").field("search_analyzer", "standard").field("preserve_separators", false).field("preserve_position_increments", true).endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    assertThat(fieldMapper, instanceOf(CompletionFieldMapper.class));
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    NamedAnalyzer indexAnalyzer = completionFieldType.indexAnalyzer();
    assertThat(indexAnalyzer.name(), equalTo("simple"));
    assertThat(indexAnalyzer.analyzer(), instanceOf(CompletionAnalyzer.class));
    CompletionAnalyzer analyzer = (CompletionAnalyzer) indexAnalyzer.analyzer();
    assertThat(analyzer.preservePositionIncrements(), equalTo(true));
    assertThat(analyzer.preserveSep(), equalTo(false));
    NamedAnalyzer searchAnalyzer = completionFieldType.searchAnalyzer();
    assertThat(searchAnalyzer.name(), equalTo("standard"));
    assertThat(searchAnalyzer.analyzer(), instanceOf(CompletionAnalyzer.class));
    analyzer = (CompletionAnalyzer) searchAnalyzer.analyzer();
    assertThat(analyzer.preservePositionIncrements(), equalTo(true));
    assertThat(analyzer.preserveSep(), equalTo(false));
}
Also used : NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) CompletionAnalyzer(org.apache.lucene.search.suggest.document.CompletionAnalyzer) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 43 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testEmptyName.

public void testEmptyName() throws IOException {
    IndexService indexService = createIndex("test");
    DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("").field("type", "completion").endObject().endObject().endObject().endObject().string();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
}
Also used : IndexService(org.elasticsearch.index.IndexService) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 44 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testParsingWithWeight.

public void testParsingWithWeight() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", XContentFactory.jsonBuilder().startObject().startObject("completion").field("input", "suggestion").field("weight", 2).endObject().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertSuggestFields(fields, 1);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 45 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CompletionFieldMapperTests method testPrefixQueryType.

public void testPrefixQueryType() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    CompletionFieldMapper completionFieldMapper = (CompletionFieldMapper) fieldMapper;
    Query prefixQuery = completionFieldMapper.fieldType().prefixQuery(new BytesRef("co"));
    assertThat(prefixQuery, instanceOf(PrefixCompletionQuery.class));
}
Also used : Query(org.apache.lucene.search.Query) FuzzyCompletionQuery(org.apache.lucene.search.suggest.document.FuzzyCompletionQuery) RegexCompletionQuery(org.apache.lucene.search.suggest.document.RegexCompletionQuery) PrefixCompletionQuery(org.apache.lucene.search.suggest.document.PrefixCompletionQuery) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) PrefixCompletionQuery(org.apache.lucene.search.suggest.document.PrefixCompletionQuery) Matchers.containsString(org.hamcrest.Matchers.containsString) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)366 Matchers.containsString (org.hamcrest.Matchers.containsString)223 IndexableField (org.apache.lucene.index.IndexableField)75 BytesReference (org.elasticsearch.common.bytes.BytesReference)62 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)51 IndexService (org.elasticsearch.index.IndexService)49 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)46 Settings (org.elasticsearch.common.settings.Settings)25 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)19 BytesRef (org.apache.lucene.util.BytesRef)18 BytesArray (org.elasticsearch.common.bytes.BytesArray)16 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)16 IOException (java.io.IOException)15 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)15 Map (java.util.Map)13 Document (org.elasticsearch.index.mapper.ParseContext.Document)13 MapperService (org.elasticsearch.index.mapper.MapperService)12 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)9 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)9