Search in sources :

Example 1 with CategoryContextMapping

use of org.opensearch.search.suggest.completion.context.CategoryContextMapping in project OpenSearch by opensearch-project.

the class CompletionSuggestSearchIT method createIndexAndMappingAndSettings.

private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
    XContentBuilder mapping = jsonBuilder().startObject().startObject("properties").startObject("test_field").field("type", "keyword").endObject().startObject("title").field("type", "keyword").endObject().startObject(FIELD).field("type", "completion").field("analyzer", completionMappingBuilder.indexAnalyzer).field("search_analyzer", completionMappingBuilder.searchAnalyzer).field("preserve_separators", completionMappingBuilder.preserveSeparators).field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements);
    if (completionMappingBuilder.contextMappings != null) {
        mapping = mapping.startArray("contexts");
        for (Map.Entry<String, ContextMapping<?>> contextMapping : completionMappingBuilder.contextMappings.entrySet()) {
            mapping = mapping.startObject().field("name", contextMapping.getValue().name()).field("type", contextMapping.getValue().type().name());
            switch(contextMapping.getValue().type()) {
                case CATEGORY:
                    mapping = mapping.field("path", ((CategoryContextMapping) contextMapping.getValue()).getFieldName());
                    break;
                case GEO:
                    mapping = mapping.field("path", ((GeoContextMapping) contextMapping.getValue()).getFieldName()).field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
                    break;
            }
            mapping = mapping.endObject();
        }
        mapping = mapping.endArray();
    }
    mapping = mapping.endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(MapperService.SINGLE_MAPPING_NAME, mapping).get());
}
Also used : ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping)

Example 2 with CategoryContextMapping

use of org.opensearch.search.suggest.completion.context.CategoryContextMapping in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testSingleContextBoosting.

public void testSingleContextBoosting() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build())));
    assertSuggestions("foo", prefix, "suggestion8", "suggestion6", "suggestion4", "suggestion9", "suggestion2");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with CategoryContextMapping

use of org.opensearch.search.suggest.completion.context.CategoryContextMapping in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method createIndexAndMappingAndSettings.

private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
    XContentBuilder mapping = jsonBuilder().startObject().startObject("properties").startObject(FIELD).field("type", "completion").field("analyzer", completionMappingBuilder.indexAnalyzer).field("search_analyzer", completionMappingBuilder.searchAnalyzer).field("preserve_separators", completionMappingBuilder.preserveSeparators).field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements);
    List<String> categoryContextFields = new ArrayList<>();
    if (completionMappingBuilder.contextMappings != null) {
        mapping.startArray("contexts");
        for (Map.Entry<String, ContextMapping<?>> contextMapping : completionMappingBuilder.contextMappings.entrySet()) {
            mapping.startObject().field("name", contextMapping.getValue().name()).field("type", contextMapping.getValue().type().name());
            switch(contextMapping.getValue().type()) {
                case CATEGORY:
                    final String fieldName = ((CategoryContextMapping) contextMapping.getValue()).getFieldName();
                    if (fieldName != null) {
                        mapping.field("path", fieldName);
                        categoryContextFields.add(fieldName);
                    }
                    break;
                case GEO:
                    final String name = ((GeoContextMapping) contextMapping.getValue()).getFieldName();
                    mapping.field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
                    if (name != null) {
                        mapping.field("path", name);
                    }
                    break;
            }
            mapping.endObject();
        }
        mapping.endArray();
    }
    mapping.endObject();
    for (String fieldName : categoryContextFields) {
        mapping.startObject(fieldName).field("type", randomBoolean() ? "keyword" : "text").endObject();
    }
    mapping.endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(MapperService.SINGLE_MAPPING_NAME, mapping).get());
}
Also used : ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping)

Example 4 with CategoryContextMapping

use of org.opensearch.search.suggest.completion.context.CategoryContextMapping in project OpenSearch by opensearch-project.

the class ContextCompletionSuggestSearchIT method testSingleContextFiltering.

public void testSingleContextFiltering() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Collections.singletonList(CategoryQueryContext.builder().setCategory("cat0").build())));
    assertSuggestions("foo", prefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with CategoryContextMapping

use of org.opensearch.search.suggest.completion.context.CategoryContextMapping in project OpenSearch by opensearch-project.

the class CategoryContextMappingTests method testParsingContextFromDocument.

public void testParsingContextFromDocument() throws Exception {
    CategoryContextMapping mapping = ContextBuilder.category("cat").field("category").build();
    ParseContext.Document document = new ParseContext.Document();
    KeywordFieldMapper.KeywordFieldType keyword = new KeywordFieldMapper.KeywordFieldType("category");
    document.add(new KeywordFieldMapper.KeywordField(keyword.name(), new BytesRef("category1"), new FieldType()));
    // Ignore doc values
    document.add(new SortedSetDocValuesField(keyword.name(), new BytesRef("category1")));
    Set<String> context = mapping.parseContext(document);
    assertThat(context.size(), equalTo(1));
    assertTrue(context.contains("category1"));
    document = new ParseContext.Document();
    TextFieldMapper.TextFieldType text = new TextFieldMapper.TextFieldType("category");
    document.add(new Field(text.name(), "category1", TextFieldMapper.Defaults.FIELD_TYPE));
    // Ignore stored field
    document.add(new StoredField(text.name(), "category1", TextFieldMapper.Defaults.FIELD_TYPE));
    context = mapping.parseContext(document);
    assertThat(context.size(), equalTo(1));
    assertTrue(context.contains("category1"));
    document = new ParseContext.Document();
    document.add(new SortedSetDocValuesField("category", new BytesRef("category")));
    context = mapping.parseContext(document);
    assertThat(context.size(), equalTo(0));
    document = new ParseContext.Document();
    document.add(new SortedDocValuesField("category", new BytesRef("category")));
    context = mapping.parseContext(document);
    assertThat(context.size(), equalTo(0));
    final ParseContext.Document doc = new ParseContext.Document();
    doc.add(new IntPoint("category", 36));
    IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> mapping.parseContext(doc));
    assertThat(exc.getMessage(), containsString("Failed to parse context field [category]"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) FieldType(org.apache.lucene.document.FieldType) CompletionFieldType(org.opensearch.index.mapper.CompletionFieldMapper.CompletionFieldType) IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) ContextSuggestField(org.apache.lucene.search.suggest.document.ContextSuggestField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) IntPoint(org.apache.lucene.document.IntPoint) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) StoredField(org.apache.lucene.document.StoredField) ParseContext(org.opensearch.index.mapper.ParseContext) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper)

Aggregations

CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)22 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)18 XContentParser (org.opensearch.common.xcontent.XContentParser)16 LinkedHashMap (java.util.LinkedHashMap)5 XContentParseException (org.opensearch.common.xcontent.XContentParseException)5 ContextMapping (org.opensearch.search.suggest.completion.context.ContextMapping)5 GeoContextMapping (org.opensearch.search.suggest.completion.context.GeoContextMapping)5 ArrayList (java.util.ArrayList)3 CompletionMappingBuilder (org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)3 CompletionSuggestionBuilder (org.opensearch.search.suggest.completion.CompletionSuggestionBuilder)3 Map (java.util.Map)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 HashMap (java.util.HashMap)1 Field (org.apache.lucene.document.Field)1 FieldType (org.apache.lucene.document.FieldType)1 IntPoint (org.apache.lucene.document.IntPoint)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)1