4646import org .apache .lucene .facet .taxonomy .SearcherTaxonomyManager ;
4747import org .apache .lucene .facet .taxonomy .directory .DirectoryTaxonomyReader ;
4848import org .apache .lucene .index .*;
49+ import org .apache .lucene .queries .function .FunctionScoreQuery ;
4950import org .apache .lucene .queryparser .classic .ParseException ;
5051import org .apache .lucene .search .*;
5152import org .apache .lucene .util .Bits ;
@@ -98,7 +99,8 @@ public class LuceneIndexWorker implements OrderedValuesIndex, QNamedKeysIndex {
9899
99100 public static final org .apache .lucene .document .FieldType TYPE_NODE_ID = new org .apache .lucene .document .FieldType ();
100101 public static final org .apache .lucene .document .FieldType CONTENT_FIELD_TYPE = new org .apache .lucene .document .FieldType ();
101-
102+ public static final org .apache .lucene .document .FieldType NON_XML_STORED_FIELD_TYPE = new org .apache .lucene .document .FieldType ();
103+ public static final org .apache .lucene .document .FieldType NON_XML_FIELD_TYPE = new org .apache .lucene .document .FieldType ();
102104 static {
103105 TYPE_NODE_ID .setIndexOptions (IndexOptions .DOCS_AND_FREQS_AND_POSITIONS );
104106 TYPE_NODE_ID .setStored (false );
@@ -111,6 +113,20 @@ public class LuceneIndexWorker implements OrderedValuesIndex, QNamedKeysIndex {
111113 CONTENT_FIELD_TYPE .setTokenized (true );
112114 CONTENT_FIELD_TYPE .setStoreTermVectors (true );
113115 CONTENT_FIELD_TYPE .freeze ();
116+
117+ NON_XML_FIELD_TYPE .setIndexOptions (IndexOptions .DOCS_AND_FREQS_AND_POSITIONS );
118+ NON_XML_FIELD_TYPE .setStored (false );
119+ NON_XML_FIELD_TYPE .setTokenized (true );
120+ NON_XML_FIELD_TYPE .setStoreTermVectors (false );
121+ NON_XML_FIELD_TYPE .freeze ();
122+
123+ NON_XML_STORED_FIELD_TYPE .setIndexOptions (IndexOptions .DOCS_AND_FREQS_AND_POSITIONS );
124+ NON_XML_STORED_FIELD_TYPE .setStored (true );
125+ NON_XML_STORED_FIELD_TYPE .setTokenized (true );
126+ NON_XML_STORED_FIELD_TYPE .setStoreTermVectors (false );
127+ NON_XML_STORED_FIELD_TYPE .freeze ();
128+
129+
114130 }
115131
116132 static final Logger LOG = LogManager .getLogger (LuceneIndexWorker .class );
@@ -400,6 +416,7 @@ protected void removeNodes() {
400416 int nodeIdLen = nodeId .size ();
401417 byte [] data = new byte [nodeIdLen + 2 ];
402418 //TODO - Should be rewritten to IntPoint
419+ //
403420 ByteConversion .shortToByte ((short ) nodeId .units (), data , 0 );
404421 nodeId .serialize (data , 2 );
405422 Term it = new Term (LuceneUtil .FIELD_NODE_ID , new BytesRef (data ));
@@ -460,13 +477,44 @@ public NodeSet query(final int contextId, final DocumentSet docs, @Nullable fina
460477 if (facets .isPresent () && config != null ) {
461478 query = drilldown (facets .get (), query , config );
462479 }
480+ query = rewriteBoost (query , field );
481+
463482 searchAndProcess (contextId , qname , docs , contextSet , resultSet ,
464483 returnAncestor , searcher , query , config );
465484 }
466485 return resultSet ;
467486 });
468487 }
469488
489+
490+ public Query rewriteBoost (Query q , String field ) {
491+ if (q instanceof TermQuery ) {
492+ var query = (TermQuery ) q ;
493+ if (query .getTerm ().field ().equals (field )) {
494+ return new FunctionScoreQuery (query , DoubleValuesSource .fromFloatField (field + "_boost" ));
495+ }
496+ } else if (q instanceof WildcardQuery ) {
497+ var query = (WildcardQuery ) q ;
498+ if (query .getField ().equals (field )) {
499+ return new FunctionScoreQuery (query , DoubleValuesSource .fromFloatField (field + "_boost" ));
500+ }
501+ }else if (q instanceof PhraseQuery ) {
502+ var query = (PhraseQuery ) q ;
503+ if (query .getField ().equals (field )) {
504+ return new FunctionScoreQuery (query , DoubleValuesSource .fromFloatField (field + "_boost" ));
505+ }
506+ } else if (q instanceof BooleanQuery ) {
507+ var query = (BooleanQuery ) q ;
508+ BooleanQuery .Builder queryBuilder = new BooleanQuery .Builder ();
509+ for (BooleanClause c : query .clauses ()) {
510+ queryBuilder .add (rewriteBoost (c .getQuery (), field ), c .getOccur ());
511+ }
512+ return queryBuilder .build ();
513+ }
514+ return q ;
515+ }
516+
517+
470518 /**
471519 * Query the index. Returns a node set containing all matching nodes. Each node
472520 * in the node set has a {@link LuceneMatch}
@@ -676,25 +724,9 @@ public void indexNonXML(NodeValue descriptor) {
676724
677725 // Get name from SOLR field
678726 String contentFieldName = field .getName ();
679-
680- // Actual field content ; Store flag can be set in solrField
681- // Field contentField = new Field(contentFieldName, field.getData().toString(), store, Field.Index.ANALYZED, Field.TermVector.YES);
682-
683- //TODO - Refactor this code and create one Field type, maybe reuse what we have.
684- var gggg = new org .apache .lucene .document .FieldType ();
685- gggg .setIndexOptions (IndexOptions .DOCS_AND_FREQS_AND_POSITIONS );
686- gggg .setStored (store == Field .Store .YES );
687- gggg .setTokenized (true );
688- gggg .setStoreTermVectors (false );//TODO - It looks like we should not store term vector // Equivalent to TermVector.YES
689- // gggg.setStoreTermVectorPositions(true);
690- // gggg.setStoreTermVectorOffsets(true);
691- Field contentField = new Field (contentFieldName , field .getData (), gggg );
692-
693- // Extract (document) Boost factor
694- // if (field.getBoost() > 0) {
695- // contentField.setBoost(field.getBoost());
696- // }
697-
727+ Field contentField = new Field (contentFieldName , field .getData (),
728+ store == Field .Store .YES ? NON_XML_STORED_FIELD_TYPE : NON_XML_FIELD_TYPE
729+ );
698730 pendingDoc .add (contentField );
699731 }
700732 }
@@ -1442,7 +1474,6 @@ private void write() {
14421474 doc .add (fNodeId );
14431475
14441476 // add separate index for node id
1445- //TODO : Rewrite to the Point API.
14461477 BinaryTokenStream bts = new BinaryTokenStream (new BytesRef (data ));
14471478 Field fNodeIdIdx = new Field (LuceneUtil .FIELD_NODE_ID , bts , TYPE_NODE_ID );
14481479 doc .add (fNodeIdIdx );
@@ -1457,13 +1488,20 @@ private void write() {
14571488 else
14581489 contentField = LuceneUtil .encodeQName (pending .qname , index .getBrokerPool ().getSymbols ());
14591490
1460- //var fld = new Field(contentField, pending.text.toString(), CONTENT_FIELD_TYPE);
14611491 var fld = new ExistLuceneTextField (contentField , pending .text .toString (), CONTENT_FIELD_TYPE );
14621492 if (pending .idxConf .getAnalyzer () != null ) {
14631493 fld .setAnalyzer (pending .idxConf .getAnalyzer ());
1464- //fld.setTokenStream(pending.idxConf.getAnalyzer().tokenStream(fld.name(), fld.stringValue()));
14651494 }
14661495 doc .add (fld );
1496+
1497+ float boost = 1.0f ; //Default boost for all fields.
1498+ if (pending .boost > 0 ) {
1499+ boost = pending .boost ;
1500+ } else if (config .getBoost () > 0 ) {
1501+ boost = config .getBoost ();
1502+ }
1503+ final var boostField = new BoostField (contentField + "_boost" , boost );
1504+ doc .add (boostField );
14671505 }
14681506 writer .addDocument (config .facetsConfig .build (index .getTaxonomyWriter (), doc ));
14691507 }
0 commit comments