Search in sources :

Example 6 with GeoContextMapping

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

the class GeoContextMappingTests method testParsingQueryContextObjectArray.

public void testParsingQueryContextObjectArray() 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, 3).endObject().startObject().startObject("context").field("lat", 22.337374).field("lon", 92.112583).endObject().field("boost", 2).array("neighbours", 5).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 + 1 + 1 + 8));
    Collection<String> firstLocations = new ArrayList<>();
    firstLocations.add("wh0n94");
    firstLocations.add("w");
    addNeighbors("w", 1, firstLocations);
    firstLocations.add("wh");
    addNeighbors("wh", 2, firstLocations);
    firstLocations.add("wh0");
    addNeighbors("wh0", 3, firstLocations);
    Collection<String> secondLocations = new ArrayList<>();
    secondLocations.add("w5cx04");
    secondLocations.add("w5cx0");
    addNeighbors("w5cx0", 5, 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(2));
        } 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 7 with GeoContextMapping

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

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, 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> locations = new ArrayList<>();
    locations.add("wh0n94");
    locations.add("w");
    addNeighbors("w", 1, locations);
    locations.add("wh");
    addNeighbors("wh", 2, locations);
    locations.add("wh0");
    addNeighbors("wh0", 3, locations);
    for (ContextMapping.InternalQueryContext internalQueryContext : internalQueryContexts) {
        assertThat(internalQueryContext.context, isIn(locations));
        assertThat(internalQueryContext.boost, equalTo(10));
        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)

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