gpt4 book ai didi

elasticsearch - springboot ElasticSearch中的n-gram实现

转载 作者:行者123 更新时间:2023-12-03 01:43:31 27 4
gpt4 key购买 nike

我正在尝试在elasticsearch中实现自动完成功能,我在 Spring 靴中使用了它,我已经尝试了很多,并尝试了许多来自Internet的示例,但都无法做到这一点。下面是我的代码示例,请帮助我。

主分类:-

@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中实现它。

最佳答案

制图

使用注释的简便方法:

@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);

引用
  • Blog posts about completion
  • Official document about completion
  • 关于elasticsearch - springboot ElasticSearch中的n-gram实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45851917/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com