Search in sources :

Example 1 with GeoContextMapping

use of org.opensearch.search.suggest.completion.context.GeoContextMapping 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 GeoContextMapping

use of org.opensearch.search.suggest.completion.context.GeoContextMapping 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 3 with GeoContextMapping

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

the class GeoContextMappingTests method testParsingQueryContextObject.

public void testParsingQueryContextObject() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().startObject("context").field("lat", 23.654242).field("lon", 90.047153).endObject().field("boost", 10).array("neighbours", 1, 2, 3).endObject();
    XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
    GeoContextMapping mapping = ContextBuilder.geo("geo").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
    assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8));
    Collection<String> locations = new ArrayList<>();
    locations.add("wh0n94");
    locations.add("w");
    addNeighborsAtLevel("w", 1, locations);
    locations.add("wh");
    addNeighborsAtLevel("wh", 2, locations);
    locations.add("wh0");
    addNeighborsAtLevel("wh0", 3, locations);
    for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
        assertThat(internalQueryContext.context, is(in(locations)));
        assertThat(internalQueryContext.boost, equalTo(10));
        assertThat(internalQueryContext.isPrefix, equalTo(internalQueryContext.context.length() < GeoContextMapping.DEFAULT_PRECISION));
    }
}
Also used : ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) ArrayList(java.util.ArrayList) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with GeoContextMapping

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

the class GeoContextMappingTests method testParsingQueryContextGeoPoint.

public void testParsingQueryContextGeoPoint() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject().field("lat", 23.654242).field("lon", 90.047153).endObject();
    XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
    GeoContextMapping mapping = ContextBuilder.geo("geo").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
    assertThat(internalQueryContexts.size(), equalTo(1 + 8));
    Collection<String> locations = new ArrayList<>();
    locations.add("wh0n94");
    addNeighborsAtLevel("wh0n94", GeoContextMapping.DEFAULT_PRECISION, locations);
    for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
        assertThat(internalQueryContext.context, is(in(locations)));
        assertThat(internalQueryContext.boost, equalTo(1));
        assertThat(internalQueryContext.isPrefix, equalTo(false));
    }
}
Also used : ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) ArrayList(java.util.ArrayList) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with GeoContextMapping

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

the class GeoContextMappingTests method testParsingQueryContextBasic.

public void testParsingQueryContextBasic() throws Exception {
    XContentBuilder builder = jsonBuilder().value("ezs42e44yx96");
    XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
    GeoContextMapping mapping = ContextBuilder.geo("geo").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(parser);
    assertThat(internalQueryContexts.size(), equalTo(1 + 8));
    Collection<String> locations = new ArrayList<>();
    locations.add("ezs42e");
    addNeighborsAtLevel("ezs42e", GeoContextMapping.DEFAULT_PRECISION, locations);
    for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
        assertThat(internalQueryContext.context, is(in(locations)));
        assertThat(internalQueryContext.boost, equalTo(1));
        assertThat(internalQueryContext.isPrefix, equalTo(false));
    }
}
Also used : ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) ArrayList(java.util.ArrayList) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)7 ContextMapping (org.opensearch.search.suggest.completion.context.ContextMapping)7 GeoContextMapping (org.opensearch.search.suggest.completion.context.GeoContextMapping)7 ArrayList (java.util.ArrayList)6 XContentParser (org.opensearch.common.xcontent.XContentParser)5 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)2 HashMap (java.util.HashMap)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1