spring-boot ElasticSearch 中的 n-gram 实现

n-gram implementaion in spring-boot ElasticSearch

我正在尝试在 elasticsearch 中实现自动完成,我在 spring 引导中使用它,我已经尝试了很多,并尝试了很多来自互联网的示例,但无法实现。下面是我的代码示例,请帮助我。

主要Class:-

@SpringBootApplication
@EnableNatsAnnotations
@EnableAutoConfiguration
@EnableConfigurationProperties(ElasticsearchProperties.class)
@EntityScan(basePackages = {
        "com.text.model"
})
@ComponentScan(
        {
                "com.text.elastic",
                "com.text.elastic.controller",
                "com.text.elastic.service",
                "com.text.elastic.service.impl",
                "com.text.nats.utils"
        }
)
public class ElasticServicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(ElasticServicesApplication.class, args);
    }
}

豆子Class:-

@Setting(settingPath = "elasticsearch-settings.json")
@Document(indexName = "content", type = "content", shards = 1, replicas = 0, createIndex = true,   refreshInterval = "-1")
public class Content {

    @Id
    private String id;

    private Locale locale;

   // @Field(type = text, index = true, store = true, analyzer = "standard")
    @Field(
            type = FieldType.String,
            index = FieldIndex.analyzed,
            searchAnalyzer = "standard",
            //indexAnalyzer = "type_ahead",
            analyzer = "standard"

            /*,
            store = true*/
    )
    private String contentTitle;

这里我想在contentTitle中实现

映射

注解的简明用法:

@CompletionField()
private Completion suggest;

或者更强大但乏味的方式:

{
    "content" : {
        "properties" : {
            "contentTitle" : { "type" : "string" },
            "suggest" : { "type" : "completion",
                "analyzer" : "simple",
                "search_analyzer" : "simple"
            }
        }
    }
}


//Then refer to the mapping by `@Mapping`:
@Setting(settingPath = "elasticsearch-settings.json")
@Document(indexName = "content", type = "content", shards = 1, replicas = 0, createIndex = true,   refreshInterval = "-1")
@Mapping(mappingPath = "/mappings/content-mapping.json")
public class Content {...}

索引

我们可以索引为我们共同的实体:

esTemplate.save(new File(...));

查询

ElasticsearchTemplate有查询方法提示:

public SuggestResponse suggest(SuggestBuilder.SuggestionBuilder<?> suggestion, String... indices);

参考