Search in sources :

Example 1 with GeoQueryContext

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

the class GeoQueryContextTests method testNullGeoPointIsIllegal.

public void testNullGeoPointIsIllegal() {
    final GeoQueryContext geoQueryContext = randomGeoQueryContext();
    final GeoQueryContext.Builder builder = GeoQueryContext.builder().setNeighbours(geoQueryContext.getNeighbours()).setPrecision(geoQueryContext.getPrecision()).setBoost(geoQueryContext.getBoost());
    try {
        builder.build();
        fail("null geo point is illegal");
    } catch (NullPointerException e) {
        assertThat(e.getMessage(), equalTo("geoPoint must not be null"));
    }
}
Also used : GeoQueryContext(org.opensearch.search.suggest.completion.context.GeoQueryContext)

Example 2 with GeoQueryContext

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

the class ContextCompletionSuggestSearchIT method testGeoBoosting.

public void testGeoBoosting() throws Exception {
    LinkedHashMap<String, ContextMapping<?>> map = new LinkedHashMap<>();
    map.put("geo", ContextBuilder.geo("geo").build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    GeoPoint[] geoPoints = new GeoPoint[] { new GeoPoint("ezs42e44yx96"), new GeoPoint("u4pruydqqvj8") };
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).startObject("contexts").field("geo", (i % 2 == 0) ? geoPoints[0].getGeohash() : geoPoints[1].getGeohash()).endObject().endObject().endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX).setId("" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    GeoQueryContext context1 = GeoQueryContext.builder().setGeoPoint(geoPoints[0]).setBoost(11).build();
    GeoQueryContext context2 = GeoQueryContext.builder().setGeoPoint(geoPoints[1]).build();
    CompletionSuggestionBuilder geoBoostingPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Arrays.asList(context1, context2)));
    assertSuggestions("foo", geoBoostingPrefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
Also used : ArrayList(java.util.ArrayList) GeoPoint(org.opensearch.common.geo.GeoPoint) GeoQueryContext(org.opensearch.search.suggest.completion.context.GeoQueryContext) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.opensearch.search.suggest.completion.CompletionSuggestionBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) ContextMapping(org.opensearch.search.suggest.completion.context.ContextMapping) GeoContextMapping(org.opensearch.search.suggest.completion.context.GeoContextMapping) CategoryContextMapping(org.opensearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 3 with GeoQueryContext

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

the class GeoQueryContextTests method randomGeoQueryContext.

public static GeoQueryContext randomGeoQueryContext() {
    final GeoQueryContext.Builder builder = GeoQueryContext.builder();
    builder.setGeoPoint(new GeoPoint(randomDouble(), randomDouble()));
    maybeSet(builder::setBoost, randomIntBetween(1, 10));
    maybeSet(builder::setPrecision, randomIntBetween(1, 12));
    List<Integer> neighbours = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(1, 12); i++) {
        neighbours.add(randomIntBetween(1, 12));
    }
    maybeSet(builder::setNeighbours, neighbours);
    return builder.build();
}
Also used : GeoPoint(org.opensearch.common.geo.GeoPoint) ArrayList(java.util.ArrayList) GeoQueryContext(org.opensearch.search.suggest.completion.context.GeoQueryContext) GeoPoint(org.opensearch.common.geo.GeoPoint)

Example 4 with GeoQueryContext

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

the class GeoQueryContextTests method testStringPrecision.

public void testStringPrecision() throws IOException {
    XContentBuilder builder = jsonBuilder().startObject();
    {
        builder.startObject("context").field("lat", 23.654242).field("lon", 90.047153).endObject();
        builder.field("boost", 10);
        builder.field("precision", 12);
        builder.array("neighbours", 1, 2);
    }
    builder.endObject();
    XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
    parser.nextToken();
    GeoQueryContext queryContext = fromXContent(parser);
    assertEquals(10, queryContext.getBoost());
    assertEquals(12, queryContext.getPrecision());
    assertEquals(Arrays.asList(1, 2), queryContext.getNeighbours());
    builder = jsonBuilder().startObject();
    {
        builder.startObject("context").field("lat", 23.654242).field("lon", 90.047153).endObject();
        builder.field("boost", 10);
        builder.field("precision", "12m");
        builder.array("neighbours", "4km", "10km");
    }
    builder.endObject();
    parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder));
    parser.nextToken();
    queryContext = fromXContent(parser);
    assertEquals(10, queryContext.getBoost());
    assertEquals(9, queryContext.getPrecision());
    assertEquals(Arrays.asList(6, 5), queryContext.getNeighbours());
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) GeoQueryContext(org.opensearch.search.suggest.completion.context.GeoQueryContext)

Aggregations

GeoQueryContext (org.opensearch.search.suggest.completion.context.GeoQueryContext)4 ArrayList (java.util.ArrayList)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)2 LinkedHashMap (java.util.LinkedHashMap)1 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)1 XContentParser (org.opensearch.common.xcontent.XContentParser)1 CompletionMappingBuilder (org.opensearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)1 CompletionSuggestionBuilder (org.opensearch.search.suggest.completion.CompletionSuggestionBuilder)1 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)1 ContextMapping (org.opensearch.search.suggest.completion.context.ContextMapping)1 GeoContextMapping (org.opensearch.search.suggest.completion.context.GeoContextMapping)1