You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2020/02/03 10:04:27 UTC

[GitHub] [lucene-solr] romseygeek commented on a change in pull request #357: [SOLR-12238] Synonym Queries boost by payload

romseygeek 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_r374012472
 
 

 ##########
 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:
   You can just use `PayloadAttribute` here?  The implementation is, well, an implementation detail :)

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

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