Search in sources :

Example 46 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class SimilarityTests method testResolveSimilaritiesFromMapping_bm25.

public void testResolveSimilaritiesFromMapping_bm25() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "text").field("similarity", "my_similarity").endObject().endObject().endObject().endObject().string();
    Settings indexSettings = Settings.builder().put("index.similarity.my_similarity.type", "BM25").put("index.similarity.my_similarity.k1", 2.0f).put("index.similarity.my_similarity.b", 0.5f).put("index.similarity.my_similarity.discount_overlaps", false).build();
    IndexService indexService = createIndex("foo", indexSettings);
    DocumentMapper documentMapper = indexService.mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    assertThat(documentMapper.mappers().getMapper("field1").fieldType().similarity(), instanceOf(BM25SimilarityProvider.class));
    BM25Similarity similarity = (BM25Similarity) documentMapper.mappers().getMapper("field1").fieldType().similarity().get();
    assertThat(similarity.getK1(), equalTo(2.0f));
    assertThat(similarity.getB(), equalTo(0.5f));
    assertThat(similarity.getDiscountOverlaps(), equalTo(false));
}
Also used : IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) Settings(org.elasticsearch.common.settings.Settings)

Example 47 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class SimilarityTests method testResolveSimilaritiesFromMapping_DFR.

public void testResolveSimilaritiesFromMapping_DFR() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "text").field("similarity", "my_similarity").endObject().endObject().endObject().endObject().string();
    Settings indexSettings = Settings.builder().put("index.similarity.my_similarity.type", "DFR").put("index.similarity.my_similarity.basic_model", "g").put("index.similarity.my_similarity.after_effect", "l").put("index.similarity.my_similarity.normalization", "h2").put("index.similarity.my_similarity.normalization.h2.c", 3f).build();
    IndexService indexService = createIndex("foo", indexSettings);
    DocumentMapper documentMapper = indexService.mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    assertThat(documentMapper.mappers().getMapper("field1").fieldType().similarity(), instanceOf(DFRSimilarityProvider.class));
    DFRSimilarity similarity = (DFRSimilarity) documentMapper.mappers().getMapper("field1").fieldType().similarity().get();
    assertThat(similarity.getBasicModel(), instanceOf(BasicModelG.class));
    assertThat(similarity.getAfterEffect(), instanceOf(AfterEffectL.class));
    assertThat(similarity.getNormalization(), instanceOf(NormalizationH2.class));
    assertThat(((NormalizationH2) similarity.getNormalization()).getC(), equalTo(3f));
}
Also used : BasicModelG(org.apache.lucene.search.similarities.BasicModelG) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) NormalizationH2(org.apache.lucene.search.similarities.NormalizationH2) DFRSimilarity(org.apache.lucene.search.similarities.DFRSimilarity) Settings(org.elasticsearch.common.settings.Settings) AfterEffectL(org.apache.lucene.search.similarities.AfterEffectL)

Example 48 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class SimilarityTests method testSimilarityDefaultBackCompat.

public void testSimilarityDefaultBackCompat() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("similarity", "default").field("type", "text").endObject().endObject().endObject().endObject().string();
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.V_2_2_0)).build();
    DocumentMapperParser parser = createIndex("test_v2.x", settings).mapperService().documentMapperParser();
    DocumentMapper documentMapper = parser.parse("type", new CompressedXContent(mapping));
    assertThat(documentMapper.mappers().getMapper("field1").fieldType().similarity(), instanceOf(ClassicSimilarityProvider.class));
    assertThat(documentMapper.mappers().getMapper("field1").fieldType().similarity().name(), equalTo("classic"));
    parser = createIndex("test_v3.x").mapperService().documentMapperParser();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("Expected MappingParsingException");
    } catch (MapperParsingException e) {
        assertThat(e.getMessage(), equalTo("Unknown Similarity type [default] for field [field1]"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser) Settings(org.elasticsearch.common.settings.Settings)

Example 49 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class SimilarityTests method testResolveSimilaritiesFromMapping_LMJelinekMercer.

public void testResolveSimilaritiesFromMapping_LMJelinekMercer() throws IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "text").field("similarity", "my_similarity").endObject().endObject().endObject().endObject().string();
    Settings indexSettings = Settings.builder().put("index.similarity.my_similarity.type", "LMJelinekMercer").put("index.similarity.my_similarity.lambda", 0.7f).build();
    IndexService indexService = createIndex("foo", indexSettings);
    DocumentMapper documentMapper = indexService.mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    assertThat(documentMapper.mappers().getMapper("field1").fieldType().similarity(), instanceOf(LMJelinekMercerSimilarityProvider.class));
    LMJelinekMercerSimilarity similarity = (LMJelinekMercerSimilarity) documentMapper.mappers().getMapper("field1").fieldType().similarity().get();
    assertThat(similarity.getLambda(), equalTo(0.7f));
}
Also used : IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) LMJelinekMercerSimilarity(org.apache.lucene.search.similarities.LMJelinekMercerSimilarity) Settings(org.elasticsearch.common.settings.Settings)

Example 50 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper in project elasticsearch by elastic.

the class ParentToChildrenAggregatorTests method mapperServiceMock.

@Override
protected MapperService mapperServiceMock() {
    MapperService mapperService = mock(MapperService.class);
    DocumentMapper childDocMapper = mock(DocumentMapper.class);
    DocumentMapper parentDocMapper = mock(DocumentMapper.class);
    ParentFieldMapper parentFieldMapper = createParentFieldMapper();
    when(childDocMapper.parentFieldMapper()).thenReturn(parentFieldMapper);
    when(parentDocMapper.parentFieldMapper()).thenReturn(parentFieldMapper);
    when(mapperService.documentMapper(CHILD_TYPE)).thenReturn(childDocMapper);
    when(mapperService.documentMapper(PARENT_TYPE)).thenReturn(parentDocMapper);
    when(mapperService.docMappers(false)).thenReturn(Arrays.asList(new DocumentMapper[] { childDocMapper, parentDocMapper }));
    when(parentDocMapper.typeFilter()).thenReturn(new TypeFieldMapper.TypesQuery(new BytesRef(PARENT_TYPE)));
    when(childDocMapper.typeFilter()).thenReturn(new TypeFieldMapper.TypesQuery(new BytesRef(CHILD_TYPE)));
    return mapperService;
}
Also used : TypeFieldMapper(org.elasticsearch.index.mapper.TypeFieldMapper) ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) MapperService(org.elasticsearch.index.mapper.MapperService) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)83 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)49 IndexService (org.elasticsearch.index.IndexService)29 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)27 IndexableField (org.apache.lucene.index.IndexableField)19 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)15 Matchers.containsString (org.hamcrest.Matchers.containsString)15 Settings (org.elasticsearch.common.settings.Settings)13 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)12 MapperService (org.elasticsearch.index.mapper.MapperService)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)11 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)9 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 Document (org.elasticsearch.index.mapper.ParseContext.Document)8 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 BytesRef (org.apache.lucene.util.BytesRef)6 ParentFieldMapper (org.elasticsearch.index.mapper.ParentFieldMapper)6