Некоторые из исключений, которые я получаю, - это
Код: Выделить всё
org.hibernate.search.util.common.SearchException: HSEARCH000614: Cannot project on entity type 'MetadataSearch': this type cannot be loaded from an external datasource, and the documents from the index cannot be projected to its Java class 'MetadataSearch'. To enable loading of entity instances from an external source, provide a SelectionLoadingStrategy when registering the entity type to the mapping builder. To enable projections turning taking index data into entity instances, annotate a constructor of the entity type with @ProjectionConstructor.See the reference documentation for more information.Код: Выделить всё
"java.lang.IllegalArgumentException: HSEARCH900004: 'projections' must not be null or empty."
Код: Выделить всё
org.hibernate.search
hibernate-search-mapper-pojo-standalone
org.hibernate.search
hibernate-search-backend-lucene
Код: Выделить всё
import lombok.*;
import lombok.experimental.Accessors;
import org.hibernate.search.engine.backend.types.Projectable;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Indexed
public class MetadataSearch {
@DocumentId private Integer id;
@GenericField(projectable = Projectable.YES)
private Integer oId;
@GenericField(projectable = Projectable.YES)
private Integer cId;
@GenericField(projectable = Projectable.YES)
private Integer oParentId;
@GenericField(projectable = Projectable.YES)
private Integer oType;
private Integer oStartVersion;
private Integer oEndVersion;
@FullTextField(projectable = Projectable.YES, analyzer = "english")
private String oName;
@FullTextField(analyzer = "english", projectable = Projectable.YES)
private String oPhysicalName;
@FullTextField(analyzer = "english", projectable = Projectable.YES)
private String oDefinition;
@FullTextField(analyzer = "english", projectable = Projectable.YES)
private String oComment;
}
Код: Выделить всё
import org.hibernate.search.engine.environment.bean.BeanReference;
import org.hibernate.search.engine.environment.bean.BeanRetrieval;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.AnnotatedTypeSource;
import org.hibernate.search.mapper.pojo.standalone.mapping.CloseableSearchMapping;
import org.hibernate.search.mapper.pojo.standalone.mapping.SearchMapping;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class SearchConfig {
@Value("${lucene.index-dir}")
private String indexDir;
@Bean
public CloseableSearchMapping searchMapping() {
Map properties = new HashMap();
properties.put("hibernate.search.backend.directory.root", indexDir);
properties.put(
"hibernate.search.backend.analysis.configurer",
BeanReference.of(LuceneAnalysisConfig.class, BeanRetrieval.CLASS));
properties.put(
"hibernate.search.mapping.configurer",
BeanReference.of(SearchMappingConfig.class, BeanRetrieval.CLASS));
return SearchMapping.builder(AnnotatedTypeSource.fromClasses(MetadataSearch.class))
.properties(properties)
.build();
}
}
Код: Выделить всё
import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.ProgrammaticMappingConfigurationContext;
import org.hibernate.search.mapper.pojo.mapping.definition.programmatic.TypeMappingStep;
import org.hibernate.search.mapper.pojo.standalone.mapping.StandalonePojoMappingConfigurationContext;
import org.hibernate.search.mapper.pojo.standalone.mapping.StandalonePojoMappingConfigurer;
public class SearchMappingConfig implements StandalonePojoMappingConfigurer {
@Override
public void configure(StandalonePojoMappingConfigurationContext context) {
context.annotationMapping()
.discoverAnnotationsFromReferencedTypes(false)
.discoverAnnotatedTypesFromRootMappingAnnotations(false);
ProgrammaticMappingConfigurationContext mappingContext = context.programmaticMapping();
TypeMappingStep mappingStep = mappingContext.type(MetadataSearch.class);
mappingStep.searchEntity();
mappingStep.indexed();
mappingStep.property("id").documentId();
mappingStep.property("oName");
}
}
import org.hibernate.search.backend.lucene.analysis.LuceneAnalysisConfigurationContext;
import org.hibernate.search.backend.lucene.analysis.LuceneAnalysisConfigurer;
public class LuceneAnalysisConfig implements LuceneAnalysisConfigurer {
@Override
public void configure(LuceneAnalysisConfigurationContext context) {
context.analyzer("english")
.custom()
.tokenizer("standard")
.tokenFilter("lowercase")
.tokenFilter("snowballPorter")
.param("language", "English")
.tokenFilter("asciiFolding");
context.analyzer("name")
.custom()
.tokenizer("standard")
.tokenFilter("lowercase")
.tokenFilter("asciiFolding");
}
}
Код: Выделить всё
import lombok.RequiredArgsConstructor;
import org.hibernate.search.engine.search.query.SearchResult;
import org.hibernate.search.mapper.pojo.standalone.mapping.CloseableSearchMapping;
import org.hibernate.search.mapper.pojo.standalone.session.SearchSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SearchService {
private final CloseableSearchMapping searchMapping;
public boolean index() {
try (SearchSession session = searchMapping.createSession()) {
MetadataSearch metadata =
new MetadataSearch()
.setId(6553610)
.setCId(65536)
.setOId(1)
.setOParentId(0)
.setOName("Hello")
.setOPhysicalName("hello")
.setODefinition("hello def)
.setOComment("hello comment");
session.indexingPlan().addOrUpdate(metadata);
return true;
}
}
public Optional search() {
try (SearchSession session = searchMapping.createSession()) {
SearchResult metadataResults =
session.search(MetadataSearch.class)
.where(f -> f.wildcard().fields("oName").matching("hello"))
.fetch(5);
long totalHitCount = metadataResults.total().hitCount();
List metadataList = metadataResults.hits();
MetadataDetails metadataDetails =
MetadataDetails.builder()
.metadata(metadataList)
.totalHitCount(totalHitCount)
.build();
return Optional.of(metadataDetails);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... ile-system
Мобильная версия