我的配置:
@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 世界中的第一次冒险,我不确定这里出了什么问题。任何帮助表示赞赏。
编辑:
来自 Elasticsearch 的提供者映射
{
"provider_search": {
"mappings": {
"properties": {
"Address": {
"type": "text",
"analyzer": "autocomplete"
},
"City": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"FaxNumber": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"PhoneNumber": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ProviderName": {
"type": "text",
"analyzer": "autocomplete"
},
"State": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Status": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Zip": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"fac_dbk": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"search": {
"type": "text",
"analyzer": "autocomplete"
}
}
}
}
}
请注意,出于测试目的,我没有在 Java 端映射 Provider 的每个字段。如果这有问题,请告诉我。
第二次更新:
我已更改 Provider 文档以按原样映射字段名称。尽管如此,除了 id 之外的所有内容仍然为空。
@Document(indexName = "provider_search")
public class Provider {
@Id
private Long id;
private String search;
private String ProviderName;
private String Address;
private String City;
private String State;
...getters...
...setters...
}
更新:
原来 Elasticsearch 索引有一个错误,我在 Java 端映射的字段在 ES 索引上不可用。它已被修复,我看到所有值都正确填充。