Search in sources :

Example 1 with GeoContextMapping

use of org.elasticsearch.search.suggest.completion.context.GeoContextMapping in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method createIndexAndMappingAndSettings.

private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
    XContentBuilder mapping = jsonBuilder().startObject().startObject(TYPE).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);
    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:
                    final String fieldName = ((CategoryContextMapping) contextMapping.getValue()).getFieldName();
                    if (fieldName != null) {
                        mapping = mapping.field("path", fieldName);
                    }
                    break;
                case GEO:
                    final String name = ((GeoContextMapping) contextMapping.getValue()).getFieldName();
                    mapping = mapping.field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
                    if (name != null) {
                        mapping.field("path", name);
                    }
                    break;
            }
            mapping = mapping.endObject();
        }
        mapping = mapping.endArray();
    }
    mapping = mapping.endObject().endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(TYPE, mapping).get());
}
Also used : GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 2 with GeoContextMapping

use of org.elasticsearch.search.suggest.completion.context.GeoContextMapping in project elasticsearch by elastic.

the class CompletionSuggestSearchIT method createIndexAndMappingAndSettings.

private void createIndexAndMappingAndSettings(Settings settings, CompletionMappingBuilder completionMappingBuilder) throws IOException {
    XContentBuilder mapping = jsonBuilder().startObject().startObject(TYPE).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().endObject();
    assertAcked(client().admin().indices().prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put(settings)).addMapping(TYPE, mapping).get());
}
Also used : GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 3 with GeoContextMapping

use of org.elasticsearch.search.suggest.completion.context.GeoContextMapping in project elasticsearch by elastic.

the class GeoContextMappingTests method testParsingQueryContextMixed.

public void testParsingQueryContextMixed() throws Exception {
    XContentBuilder builder = jsonBuilder().startArray().startObject().startObject("context").field("lat", 23.654242).field("lon", 90.047153).endObject().field("boost", 10).array("neighbours", 1, 2).endObject().startObject().field("lat", 22.337374).field("lon", 92.112583).endObject().endArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    GeoContextMapping mapping = ContextBuilder.geo("geo").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(1 + 1 + 8 + 1 + 8 + 1 + 8));
    Collection<String> firstLocations = new ArrayList<>();
    firstLocations.add("wh0n94");
    firstLocations.add("w");
    addNeighbors("w", 1, firstLocations);
    firstLocations.add("wh");
    addNeighbors("wh", 2, firstLocations);
    Collection<String> secondLocations = new ArrayList<>();
    secondLocations.add("w5cx04");
    addNeighbors("w5cx04", 6, secondLocations);
    for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
        if (firstLocations.contains(internalQueryContext.context)) {
            assertThat(internalQueryContext.boost, equalTo(10));
        } else if (secondLocations.contains(internalQueryContext.context)) {
            assertThat(internalQueryContext.boost, equalTo(1));
        } else {
            fail(internalQueryContext.context + " was not expected");
        }
        assertThat(internalQueryContext.isPrefix, equalTo(internalQueryContext.context.length() < GeoContextMapping.DEFAULT_PRECISION));
    }
}
Also used : GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ArrayList(java.util.ArrayList) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 4 with GeoContextMapping

use of org.elasticsearch.search.suggest.completion.context.GeoContextMapping in project elasticsearch by elastic.

the class GeoContextMappingTests method testParsingQueryContextBasic.

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

Example 5 with GeoContextMapping

use of org.elasticsearch.search.suggest.completion.context.GeoContextMapping in project elasticsearch by elastic.

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, builder.bytes());
    GeoContextMapping mapping = ContextBuilder.geo("geo").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(1 + 8));
    Collection<String> locations = new ArrayList<>();
    locations.add("wh0n94");
    addNeighbors("wh0n94", GeoContextMapping.DEFAULT_PRECISION, locations);
    for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
        assertThat(internalQueryContext.context, isIn(locations));
        assertThat(internalQueryContext.boost, equalTo(1));
        assertThat(internalQueryContext.isPrefix, equalTo(false));
    }
}
Also used : GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ArrayList(java.util.ArrayList) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

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