I was able to search by whole words, for example searching phrase "secret" text "This is a secret word" was found. But If I search for phrase "secre" I get an empty array. I need this for autocomplete function. I'm using Spring Boot release 1.3.1.RELEASE (it uses Elasticsearch version 1.5.2 and I can't upgrade spring). What am I doing wrong? I'l be thankful for a link to a working example also. I know Elasticsearch is too heavy for this purpose but I want to learn how to use it. Many thanks in advance!
SearchConfig.java
@Configuration
@EnableElasticsearchRepositories(basePackages = {"cz.project.search"})
public class SearchConfig {
@Autowired
private Client elasticsearchClient;
@Bean
public ConstructionWorkIndexInitializer constructionWorkIndexInitializer() {
return new ConstructionWorkIndexInitializer();
}
}
ConstructionWorkIndexRepository.java
public interface ConstructionWorkIndexRepository extends ElasticsearchRepository<ConstructionWorkIndex, String> {
Page<ConstructionWorkIndex> findByCodeOrDescription(String code, String description, Pageable pageable);
}
ConstructionWorkIndex.java
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "works", type = "work", shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1")
@Setting(settingPath = "classpath:construction-works-settings.json")
public class ConstructionWorkIndex {
@Id
@Field(indexAnalyzer = "standard", searchAnalyzer = "standard", type = FieldType.String, store = true)
private String code;
@Field(indexAnalyzer = "standard", searchAnalyzer = "standard", type = FieldType.String, store = true)
private String description;
public ConstructionWorkIndex(ConstructionWorkVO constructionWork) {
requireNonNull(constructionWork, "constructionWork must not be null");
this.code = constructionWork.getCode();
this.description = constructionWork.getDescription();
}
}
construction-works-settings.json
{
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
ConstructionWorkIndexInitializer.java
@Slf4j
public class ConstructionWorkIndexInitializer {
@Autowired
private ConstructionWorkRepository workRepository;
@Autowired
private ConstructionWorkIndexRepository workIndexRepository;
@PostConstruct
@Transactional(readOnly = true)
@Async
public void init() {
initWorks();
}
private void initWorks() {
List<ConstructionWorkVO> works = workRepository.findAll();
works.forEach(work -> {
workIndexRepository.save(new ConstructionWorkIndex(work));
log.debug("Added construction work code '{}'", work.getCode());
});
log.debug("Indexed {} construction works", works.size());
}
}