alessandrobenedetti commented on a change in pull request #357: [SOLR-12238] 
Synonym Queries boost by payload 
URL: https://github.com/apache/lucene-solr/pull/357#discussion_r374032839
 
 

 ##########
 File path: solr/core/src/java/org/apache/solr/parser/SolrQueryParserBase.java
 ##########
 @@ -600,29 +617,133 @@ protected Query newRegexpQuery(Term regexp) {
     return query;
   }
 
+  private Query buildBooleanQuery(List<Query> sidePathsQueries) {
+    BooleanQuery.Builder builder = new BooleanQuery.Builder();
+    for (Query sidePath : sidePathsQueries) {
+      builder.add(sidePath, BooleanClause.Occur.SHOULD);
+    }
+    return builder.build();
+  }
+
+  @Override
+  protected Query newTermQuery(String field, AttributeSource attribute) {
+    Query termQuery = super.newTermQuery(field,attribute);
+    return getBoostedQueryByPayload(new AttributeSource[]{attribute}, 
termQuery);
+  }
+
+  @Override
+  protected Query newPhraseQuery(String field, AttributeSource[] attributes, 
int slop) {
+    Query phraseQuery = super.newPhraseQuery(field,attributes,slop);
+    return getBoostedQueryByPayload(attributes, phraseQuery);
+  }
+
+  @Override
+  protected SpanQuery newSpanQuery(String field, AttributeSource[] attributes) 
{
+    SpanQuery spanQuery = super.newSpanQuery(field,attributes);
+    return getBoostedQueryByPayload(attributes, spanQuery);
+  }
+
   @Override
-  protected Query newSynonymQuery(Term terms[]) {
+  protected Query newSynonymQuery(String field, AttributeSource[] attributes) {
     switch (synonymQueryStyle) {
-      case PICK_BEST:
-        List<Query> currPosnClauses = new ArrayList<Query>(terms.length);
-        for (Term term : terms) {
-          currPosnClauses.add(newTermQuery(term));
-        }
-        DisjunctionMaxQuery dm = new DisjunctionMaxQuery(currPosnClauses, 
0.0f);
-        return dm;
-      case AS_DISTINCT_TERMS:
-        BooleanQuery.Builder builder = new BooleanQuery.Builder();
-        for (Term term : terms) {
-          builder.add(newTermQuery(term), BooleanClause.Occur.SHOULD);
+      case PICK_BEST: {
+        List<Query> synonymQueries = getSynonymQueries(field, attributes);
+        return new DisjunctionMaxQuery(synonymQueries, 0.0f);
+      }
+      case AS_DISTINCT_TERMS: {
+        List<Query> synonymQueries = getSynonymQueries(field, attributes);
+        return buildBooleanQuery(synonymQueries);
+      }
+      case AS_SAME_TERM:{
+        SynonymQuery.Builder builder = new SynonymQuery.Builder(field);
+        for (int i = 0; i < attributes.length; i++) {
+          TermToBytesRefAttribute termAttribute = 
attributes[i].getAttribute(TermToBytesRefAttribute.class);
+          float payloadBoost = getDecodedPayload(attributes[i]);
+          if (isAcceptableBoost(payloadBoost)) {
+            builder.addTerm(new Term(field, termAttribute.getBytesRef()), 
payloadBoost);
+          } else {
+            builder.addTerm(new Term(field, termAttribute.getBytesRef()));
+          }
         }
         return builder.build();
-      case AS_SAME_TERM:
-        return super.newSynonymQuery(terms);
+      }
       default:
         throw new AssertionError("unrecognized synonymQueryStyle passed when 
creating newSynonymQuery");
     }
   }
 
+  private List<Query> getSynonymQueries(String field, AttributeSource[] 
attributes) {
+    List<Query> synonymQueries = new ArrayList<>(attributes.length);
+    for (int i = 0; i < attributes.length; i++) {
+      TermToBytesRefAttribute termAttribute = 
attributes[i].getAttribute(TermToBytesRefAttribute.class);
+      Query synonymQuery = new TermQuery(new Term(field, 
termAttribute.getBytesRef()));
+      synonymQueries.add(getBoostedQueryByPayload(new 
AttributeSource[]{attributes[i]}, synonymQuery));
+    }
+    return synonymQueries;
+  }
+  
+  private Query getBoostedQueryByPayload(AttributeSource[] attributes, Query 
query) {
+    float payloadBoost = 0f;
+    for (int i = 0; i < attributes.length; i++) {
+      payloadBoost = getDecodedPayload(attributes[i]);
+    }
+    if (isAcceptableBoost(payloadBoost)) {
+      return new BoostQuery(query, payloadBoost);
+    }
+    return query;
+  }
+  
+  private SpanQuery getBoostedQueryByPayload(AttributeSource[] attributes, 
SpanQuery query) {
+    float payloadBoost = 0f;
+    for (int i = 0; i < attributes.length; i++) {
+      payloadBoost = getDecodedPayload(attributes[i]);
+    }
+    if (isAcceptableBoost(payloadBoost)) {
+      return new SpanBoostQuery(query, payloadBoost);
+    }
+    return query;
+  }
+
+  private float getDecodedPayload(AttributeSource attribute) {
+    float payloadBoost = 0f;
+    PayloadAttributeImpl payloadAttribute = 
attribute.getAttributeImpl(PayloadAttributeImpl.class);
 
 Review comment:
   definitely make sense!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to