我正在尝试在elasticsearch中实现自动完成,我在spring boot中使用它,我已经尝试了很多并尝试了很多来自互联网的例子但是无法实现。下面是我的代码示例请帮助我。
主类: -
@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);
}
}
Bean类: -
@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中实现它。
答案 0 :(得分:1)
使用注释的简明方法:
@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);