1. <small id='ZjMLu'></small><noframes id='ZjMLu'>

    <i id='ZjMLu'><tr id='ZjMLu'><dt id='ZjMLu'><q id='ZjMLu'><span id='ZjMLu'><b id='ZjMLu'><form id='ZjMLu'><ins id='ZjMLu'></ins><ul id='ZjMLu'></ul><sub id='ZjMLu'></sub></form><legend id='ZjMLu'></legend><bdo id='ZjMLu'><pre id='ZjMLu'><center id='ZjMLu'></center></pre></bdo></b><th id='ZjMLu'></th></span></q></dt></tr></i><div id='ZjMLu'><tfoot id='ZjMLu'></tfoot><dl id='ZjMLu'><fieldset id='ZjMLu'></fieldset></dl></div>
      <bdo id='ZjMLu'></bdo><ul id='ZjMLu'></ul>

    <tfoot id='ZjMLu'></tfoot>

    1. <legend id='ZjMLu'><style id='ZjMLu'><dir id='ZjMLu'><q id='ZjMLu'></q></dir></style></legend>
    2. Hibernate Search:为自定义 FieldBridge 配置 Facet

      时间:2023-09-29

      <small id='mcc02'></small><noframes id='mcc02'>

      1. <tfoot id='mcc02'></tfoot>
          <i id='mcc02'><tr id='mcc02'><dt id='mcc02'><q id='mcc02'><span id='mcc02'><b id='mcc02'><form id='mcc02'><ins id='mcc02'></ins><ul id='mcc02'></ul><sub id='mcc02'></sub></form><legend id='mcc02'></legend><bdo id='mcc02'><pre id='mcc02'><center id='mcc02'></center></pre></bdo></b><th id='mcc02'></th></span></q></dt></tr></i><div id='mcc02'><tfoot id='mcc02'></tfoot><dl id='mcc02'><fieldset id='mcc02'></fieldset></dl></div>
              <bdo id='mcc02'></bdo><ul id='mcc02'></ul>
              <legend id='mcc02'><style id='mcc02'><dir id='mcc02'><q id='mcc02'></q></dir></style></legend>
                  <tbody id='mcc02'></tbody>

                本文介绍了Hibernate Search:为自定义 FieldBridge 配置 Facet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在这个例子中 DateSplitBridge.java 动态字段被添加到索引文档中:

                In this example DateSplitBridge.java dynamic fields are added to indexed document:

                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 本身中完成吗?

                How do I configure Facet for such ad hoc fields? Can it be done in FieldBridge itself?

                推荐答案

                在 https://hibernate.atlassian.net/browse/HSEARCH-1686?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&showAll=true

                这里采用的版本:companyId是实体成员(Long),isFavorite也是成员(布尔).结果:在索引文档中有类似的字段:customFieldFacet_1234,其值为:true"或false".注意:没有@Facet注解

                Here is adopted version: companyId is entity member (Long), isFavorite also member (Boolean). As result: in the indexed document there are fields like: customFieldFacet_1234 with values: "true" or "false". Note: there is no @Facet annotation

                用法:

                @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:

                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));
                            }
                        }
                    }
                
                }
                

                这篇关于Hibernate Search:为自定义 FieldBridge 配置 Facet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Lucene 与 EclipseLink 的集成 下一篇:为什么 Query、TermQuery 等不再在 Lucene 4.0 中实现

                相关文章

                最新文章

              • <tfoot id='yUuQZ'></tfoot>
              • <legend id='yUuQZ'><style id='yUuQZ'><dir id='yUuQZ'><q id='yUuQZ'></q></dir></style></legend>
                • <bdo id='yUuQZ'></bdo><ul id='yUuQZ'></ul>

                <i id='yUuQZ'><tr id='yUuQZ'><dt id='yUuQZ'><q id='yUuQZ'><span id='yUuQZ'><b id='yUuQZ'><form id='yUuQZ'><ins id='yUuQZ'></ins><ul id='yUuQZ'></ul><sub id='yUuQZ'></sub></form><legend id='yUuQZ'></legend><bdo id='yUuQZ'><pre id='yUuQZ'><center id='yUuQZ'></center></pre></bdo></b><th id='yUuQZ'></th></span></q></dt></tr></i><div id='yUuQZ'><tfoot id='yUuQZ'></tfoot><dl id='yUuQZ'><fieldset id='yUuQZ'></fieldset></dl></div>

                <small id='yUuQZ'></small><noframes id='yUuQZ'>