Search in sources :

Example 1 with ContextMapping

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

the class ContextCompletionSuggestSearchIT method testMissingContextValue.

public void testMissingContextValue() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    map.put("type", ContextBuilder.category("type").field("type").build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject();
        if (randomBoolean()) {
            source.field("cat", "cat" + i % 2);
        }
        if (randomBoolean()) {
            source.field("type", "type" + i % 4);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ContextMapping

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

the class ContextCompletionSuggestSearchIT method testSeveralContexts.

public void testSeveralContexts() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    final int numContexts = randomIntBetween(2, 5);
    for (int i = 0; i < numContexts; i++) {
        map.put("type" + i, ContextBuilder.category("type" + i).field("type" + i).build());
    }
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = randomIntBetween(10, 200);
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", numDocs - i).endObject();
        for (int c = 0; c < numContexts; c++) {
            source.field("type" + c, "type" + c + i % 4);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion0", "suggestion1", "suggestion2", "suggestion3", "suggestion4");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with ContextMapping

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

the class ContextCompletionSuggestSearchIT method testContextPrefix.

public void testContextPrefix() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    boolean addAnotherContext = randomBoolean();
    if (addAnotherContext) {
        map.put("type", ContextBuilder.category("type").field("type").build());
    }
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2);
        if (addAnotherContext) {
            source.field("type", "type" + i % 3);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with ContextMapping

use of org.elasticsearch.search.suggest.completion.context.ContextMapping 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 5 with ContextMapping

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

the class ContextCompletionSuggestSearchIT method testGeoFiltering.

public void testGeoFiltering() 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, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
    CompletionSuggestionBuilder geoFilteringPrefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("geo", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(geoPoints[0])).build())));
    assertSuggestions("foo", geoFilteringPrefix, "suggestion8", "suggestion6", "suggestion4", "suggestion2", "suggestion0");
}
Also used : ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) LinkedHashMap(java.util.LinkedHashMap) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)20 LinkedHashMap (java.util.LinkedHashMap)18 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)18 GeoContextMapping (org.elasticsearch.search.suggest.completion.context.GeoContextMapping)18 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)16 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)16 ArrayList (java.util.ArrayList)15 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 GeoPoint (org.elasticsearch.common.geo.GeoPoint)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)15 HashMap (java.util.HashMap)5 List (java.util.List)3 Map (java.util.Map)3 ToXContent (org.elasticsearch.common.xcontent.ToXContent)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 ContextMappings (org.elasticsearch.search.suggest.completion.context.ContextMappings)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1