Java Hibernate搜索:为自定义FieldBridge配置方面
在此示例中,动态字段被添加到索引文档中:Java Hibernate搜索:为自定义FieldBridge配置方面,java,lucene,hibernate-search,facet,Java,Lucene,Hibernate Search,Facet,在此示例中,动态字段被添加到索引文档中: public class DateSplitBridge implements FieldBridge { ... public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { ... luceneOptions.addFieldToDocument( name + ".year", String.valueOf(
public class DateSplitBridge implements FieldBridge {
...
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
...
luceneOptions.addFieldToDocument( name + ".year", String.valueOf( year ), document);
...
如何为此类特殊字段配置Facet?
这可以在FieldBridge本身中完成吗?在中找到了解决方案
这里是采用的版本:companyId是实体成员(Long),isFavorite也是成员(Boolean)。结果:在索引文档中,有值为“true”或“false”的customfieldfacet1234等字段。注意:没有@Facet注释
用法:
@Field(analyze = Analyze.NO, store = Store.YES, bridge = @FieldBridge(impl = CustomFieldBridge.class))
@Transient
public CustomField getFacetingCustomField() {
return new CustomField("customFieldFacet_" + companyId, isFavorite);
}
田桥:
import org.apache.lucene.document.Document;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
import java.io.IOException;
public class CustomFieldBridge implements FieldBridge {
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if (value != null) {
CustomField customField = (CustomField) value;
if (customField.getFieldValue() != null) {
String fieldName = customField.getFieldName();
String fieldValue = customField.getFieldValue();
CustomFacetsConfig config = new CustomFacetsConfig();
config.setIndexFieldName(fieldName, fieldName);
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField(fieldName, fieldValue));
try {
config.CustomBuild(doc, document);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
CustomFacetConfig:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.facet.FacetsConfig;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
import org.apache.lucene.facet.taxonomy.FacetLabel;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.util.BytesRef;
import java.io.IOException;
import java.util.*;
public class CustomFacetsConfig extends FacetsConfig {
public Document CustomBuild(Document doc, Document destDocument) throws IOException {
Map<String, List<SortedSetDocValuesFacetField>> dvByField = new HashMap<>();
Set<String> seenDims = new HashSet<>();
for (IndexableField field : doc.getFields()) {
if (field.fieldType() == SortedSetDocValuesFacetField.TYPE) {
SortedSetDocValuesFacetField facetField = (SortedSetDocValuesFacetField) field;
FacetsConfig.DimConfig dimConfig = getDimConfig(facetField.dim);
if (dimConfig.multiValued == false) {
checkSeen(seenDims, facetField.dim);
}
String indexFieldName = dimConfig.indexFieldName;
List<SortedSetDocValuesFacetField> fields = dvByField.get(indexFieldName);
if (fields == null) {
fields = new ArrayList<>();
dvByField.put(indexFieldName, fields);
}
fields.add(facetField);
}
}
processSSDVFacetFields(dvByField, destDocument);
return destDocument;
}
private void checkSeen(Set<String> seenDims, String dim) {
if (seenDims.contains(dim)) {
throw new IllegalArgumentException("dimension " + dim + " is not multiValued, but it appears more than once in this document");
}
seenDims.add(dim);
}
private void processSSDVFacetFields(Map<String, List<SortedSetDocValuesFacetField>> byField, Document doc) throws IOException {
for (Map.Entry<String, List<SortedSetDocValuesFacetField>> ent : byField.entrySet()) {
String indexFieldName = ent.getKey();
for (SortedSetDocValuesFacetField facetField : ent.getValue()) {
FacetLabel cp = new FacetLabel(facetField.dim, facetField.label);
String fullPath = pathToString(cp.components, cp.length);
doc.add(new SortedSetDocValuesField(indexFieldName, new BytesRef(fullPath)));
doc.add(new StringField(indexFieldName, fullPath, Field.Store.YES));
}
}
}
}
import org.apache.lucene.document.document;
导入org.apache.lucene.document.Field;
导入org.apache.lucene.document.SortedSetDocValuesField;
导入org.apache.lucene.document.StringField;
导入org.apache.lucene.facet.FacetsConfig;
导入org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
导入org.apache.lucene.facet.taxonomy.facetlab;
导入org.apache.lucene.index.IndexableField;
导入org.apache.lucene.util.BytesRef;
导入java.io.IOException;
导入java.util.*;
公共类CustomFacetsConfig扩展了FacetsConfig{
公共文档CustomBuild(文档文档、文档destDocument)引发IOException{
Map dvByField=new HashMap();
Set seenDims=new HashSet();
for(IndexableField字段:doc.getFields()){
if(field.fieldType()==SortedSetDocValuesFacetField.TYPE){
SortedSetDocValuesFacetField=(SortedSetDocValuesFacetField)字段;
FacetsConfig.DimConfig-DimConfig=getDimConfig(facetField.dim);
if(dimConfig.multiValued==false){
选中SEED(seenDims,facetField.dim);
}
字符串indexFieldName=dimConfig.indexFieldName;
列表字段=dvByField.get(indexFieldName);
如果(字段==null){
字段=新的ArrayList();
put(indexFieldName,fields);
}
字段。添加(facetField);
}
}
ProcessSsdFacetFields(dvByField、destDocument);
归还文件;
}
私有无效检查已查看(设置seenDims、字符串尺寸){
如果(见详细信息包含(dim)){
抛出新的IllegalArgumentException(“维度“+dim+”不是多值的,但它在本文档中多次出现”);
}
seenDims.add(dim);
}
私有void进程SsdvFaceFields(映射按字段、文档文档)引发IOException{
for(Map.Entry-ent:byField.entrySet()){
字符串indexFieldName=ent.getKey();
for(SortedSetDocValuesFacetField:ent.getValue()){
FacetLabel cp=新的FacetLabel(facetField.dim,facetField.label);
字符串完整路径=路径字符串(cp.components,cp.length);
添加文档(新的SortedSetDocValuesField(indexFieldName,新的ByteRef(完整路径));
doc.add(新的StringField(indexFieldName,fullPath,Field.Store.YES));
}
}
}
}
在中找到了解决方案
这里是采用的版本:companyId是实体成员(Long),isFavorite也是成员(Boolean)。结果:在索引文档中,有值为“true”或“false”的customfieldfacet1234等字段。注意:没有@Facet注释
用法:
@Field(analyze = Analyze.NO, store = Store.YES, bridge = @FieldBridge(impl = CustomFieldBridge.class))
@Transient
public CustomField getFacetingCustomField() {
return new CustomField("customFieldFacet_" + companyId, isFavorite);
}
田桥:
import org.apache.lucene.document.Document;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
import java.io.IOException;
public class CustomFieldBridge implements FieldBridge {
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if (value != null) {
CustomField customField = (CustomField) value;
if (customField.getFieldValue() != null) {
String fieldName = customField.getFieldName();
String fieldValue = customField.getFieldValue();
CustomFacetsConfig config = new CustomFacetsConfig();
config.setIndexFieldName(fieldName, fieldName);
Document doc = new Document();
doc.add(new SortedSetDocValuesFacetField(fieldName, fieldValue));
try {
config.CustomBuild(doc, document);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
CustomFacetConfig:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.facet.FacetsConfig;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
import org.apache.lucene.facet.taxonomy.FacetLabel;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.util.BytesRef;
import java.io.IOException;
import java.util.*;
public class CustomFacetsConfig extends FacetsConfig {
public Document CustomBuild(Document doc, Document destDocument) throws IOException {
Map<String, List<SortedSetDocValuesFacetField>> dvByField = new HashMap<>();
Set<String> seenDims = new HashSet<>();
for (IndexableField field : doc.getFields()) {
if (field.fieldType() == SortedSetDocValuesFacetField.TYPE) {
SortedSetDocValuesFacetField facetField = (SortedSetDocValuesFacetField) field;
FacetsConfig.DimConfig dimConfig = getDimConfig(facetField.dim);
if (dimConfig.multiValued == false) {
checkSeen(seenDims, facetField.dim);
}
String indexFieldName = dimConfig.indexFieldName;
List<SortedSetDocValuesFacetField> fields = dvByField.get(indexFieldName);
if (fields == null) {
fields = new ArrayList<>();
dvByField.put(indexFieldName, fields);
}
fields.add(facetField);
}
}
processSSDVFacetFields(dvByField, destDocument);
return destDocument;
}
private void checkSeen(Set<String> seenDims, String dim) {
if (seenDims.contains(dim)) {
throw new IllegalArgumentException("dimension " + dim + " is not multiValued, but it appears more than once in this document");
}
seenDims.add(dim);
}
private void processSSDVFacetFields(Map<String, List<SortedSetDocValuesFacetField>> byField, Document doc) throws IOException {
for (Map.Entry<String, List<SortedSetDocValuesFacetField>> ent : byField.entrySet()) {
String indexFieldName = ent.getKey();
for (SortedSetDocValuesFacetField facetField : ent.getValue()) {
FacetLabel cp = new FacetLabel(facetField.dim, facetField.label);
String fullPath = pathToString(cp.components, cp.length);
doc.add(new SortedSetDocValuesField(indexFieldName, new BytesRef(fullPath)));
doc.add(new StringField(indexFieldName, fullPath, Field.Store.YES));
}
}
}
}
import org.apache.lucene.document.document;
导入org.apache.lucene.document.Field;
导入org.apache.lucene.document.SortedSetDocValuesField;
导入org.apache.lucene.document.StringField;
导入org.apache.lucene.facet.FacetsConfig;
导入org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
导入org.apache.lucene.facet.taxonomy.facetlab;
导入org.apache.lucene.index.IndexableField;
导入org.apache.lucene.util.BytesRef;
导入java.io.IOException;
导入java.util.*;
公共类CustomFacetsConfig扩展了FacetsConfig{
公共文档CustomBuild(文档文档、文档destDocument)引发IOException{
Map dvByField=new HashMap();
Set seenDims=new HashSet();
for(IndexableField字段:doc.getFields()){
if(field.fieldType()==SortedSetDocValuesFacetField.TYPE){
SortedSetDocValuesFacetField=(SortedSetDocValuesFacetField)字段;
FacetsConfig.DimConfig-DimConfig=getDimConfig(facetField.dim);
if(dimConfig.multiValued==false){
选中SEED(seenDims,facetField.dim);
}
字符串indexFieldName=dimConfig.indexFieldName;
列表字段=dvByField.get(indexFieldName);
如果(字段==null){
字段=新的ArrayList();
put(indexFieldName,fields);
}
字段。添加(facetField);
}
}
ProcessSsdFacetFields(dvByField、destDocument);
归还文件;
}
私有无效检查已查看(设置seenDims、字符串尺寸){
如果(见详细信息包含(dim)){
抛出新的IllegalArgumentException(“维度“+dim+”不是多值的,但它在本文档中多次出现”);
}
seenDims.add(dim);
}
私有void进程SsdvFaceFields(映射按字段、文档文档)引发IOException{
for(Map.Entry-ent:byField.entrySet()){
字符串indexFieldName=ent.getKey();
for(SortedSetDocValuesFacetField:ent.getValue()){
FacetLabel cp=新的FacetLabel(facetField.dim,facetField.label);
字符串完整路径=路径字符串(cp.components,cp.length);
添加文档(新的SortedSetDocValuesField(indexFieldName,新的ByteRef(完整路径));
doc.add(新的StringField(indexFieldName,fullPath,Field.Store.YES));
}
}
}
}