我的配置:
@Configuration
@EnableElasticsearchRepositories(
basePackages = { "com.aj.new.repositories" })
public class ElasticDataSourceConfig {
@Bean
public RestHighLevelClient client() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
@Bean
public ElasticsearchRestTemplate elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
}
文件:
@Document(indexName = "provider_search")
public class Provider {
@Id @Field
private Long id;
@Field(type = FieldType.Keyword)
private String search;
@Field(name = "ProviderName", type = FieldType.Keyword)
private String providerName;
@Field(name = "Address")
private String address;
@Field(name = "City")
private String city;
@Field(name = "State")
private String state;
...getters...
...setters...
}
用法:
@Service
public class MySearchService {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
public List<Provider> searchProvidersUsingElastic(final String
providerName, final AddressSearchCriteriaBean addressCriteria) {
final NativeSearchQueryBuilder searchQueryBuilder = new
NativeSearchQueryBuilder();
if (providerName != null) {
final String regex = ".*" + providerName + ".*";
searchQueryBuilder.withQuery(regexpQuery("providerName", regex));
}
if (addressCriteria.getState() != null) {
searchQueryBuilder.withFilter(matchQuery("state",
addressCriteria.getState())
.fuzziness(Fuzziness.ONE));
}
SearchHits<Provider> articles =
elasticsearchRestTemplate.search(searchQueryBuilder.build(),
Provider.class, IndexCoordinates.of("provider_search"));
final List<Provider> providers = articles
.stream()
.map(SearchHit::getContent)
.collect(Collectors.toList());
return providers;
}
}
在使用或不使用过滤器进行调试时,我得到的提供程序只填充了它们的id字段。“搜索”、“状态”等所有其他字段都为空。
这是我在elasticsearch世界的第一次尝试,我不确定这里出了什么问题。感谢您的帮助。
暂无答案!
目前还没有任何答案,快来回答吧!