Search in sources :

Example 6 with CategoryContextMapping

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

the class CategoryContextMappingTests method testQueryContextParsingMixed.

public void testQueryContextParsingMixed() throws Exception {
    XContentBuilder builder = jsonBuilder().startArray().startObject().field("context", "context1").field("boost", 2).field("prefix", true).endObject().value("context2").endArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    CategoryContextMapping mapping = ContextBuilder.category("cat").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(2));
    assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
    assertThat(internalQueryContexts.get(0).boost, equalTo(2));
    assertThat(internalQueryContexts.get(0).isPrefix, equalTo(true));
    assertThat(internalQueryContexts.get(1).context, equalTo("context2"));
    assertThat(internalQueryContexts.get(1).boost, equalTo(1));
    assertThat(internalQueryContexts.get(1).isPrefix, equalTo(false));
}
Also used : XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 7 with CategoryContextMapping

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

the class CategoryContextMappingTests method testParsingContextFromDocument.

public void testParsingContextFromDocument() throws Exception {
    CategoryContextMapping mapping = ContextBuilder.category("cat").field("category").build();
    ParseContext.Document document = new ParseContext.Document();
    document.add(new StringField("category", "category1", Field.Store.NO));
    Set<CharSequence> context = mapping.parseContext(document);
    assertThat(context.size(), equalTo(1));
    assertTrue(context.contains("category1"));
}
Also used : StringField(org.apache.lucene.document.StringField) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)

Example 8 with CategoryContextMapping

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

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

the class ContextCompletionSuggestSearchIT method testSingleContextMultipleContexts.

public void testSingleContextMultipleContexts() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<String, ContextMapping>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<String> contexts = Arrays.asList("type1", "type2", "type3", "type4");
    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", contexts).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 : ArrayList(java.util.ArrayList) GeoPoint(org.elasticsearch.common.geo.GeoPoint) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap) 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) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 10 with CategoryContextMapping

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

the class ContextCompletionSuggestSearchIT method testSingleContextBoosting.

public void testSingleContextBoosting() throws Exception {
    CategoryContextMapping contextMapping = ContextBuilder.category("cat").field("cat").build();
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<String, ContextMapping>(Collections.singletonMap("cat", contextMapping));
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2).endObject()));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg").contexts(Collections.singletonMap("cat", Arrays.asList(CategoryQueryContext.builder().setCategory("cat0").setBoost(3).build(), CategoryQueryContext.builder().setCategory("cat1").build())));
    assertSuggestions("foo", prefix, "suggestion8", "suggestion6", "suggestion4", "suggestion9", "suggestion2");
}
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) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)12 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 LinkedHashMap (java.util.LinkedHashMap)6 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)6 GeoContextMapping (org.elasticsearch.search.suggest.completion.context.GeoContextMapping)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)5 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)4 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)4 ArrayList (java.util.ArrayList)3 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)3 GeoPoint (org.elasticsearch.common.geo.GeoPoint)3 Map (java.util.Map)2 HashMap (java.util.HashMap)1 StringField (org.apache.lucene.document.StringField)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 ParseContext (org.elasticsearch.index.mapper.ParseContext)1 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)1 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1