msokolov commented on issue #14886:
URL: https://github.com/apache/lucene/issues/14886#issuecomment-3045670761

   sure -- this is what we are using; you can see it devolves to hashing 
`toString()` in cases that aren't covered explicitly, so in theory it is fully 
general, but `toString()` isn't really reliable for this purpose, I think, so 
this is just sort of waving at these other query types.
   
   ```
    // Query.hashCode is not stable across processes (partly because of 
including the Class hash).
       static long queryHashCodeStable(Query q) {
           long hashCode = 0;
           long prime = 31L;
           if (q instanceof TermQuery tq) {
               // Term hashCode() relies on BytesRef.hashCode() which is not 
stable
               Term t = tq.getTerm();
               hashCode = hashCode * prime + t.field().hashCode();
               hashCode = hashCode * prime + t.text().hashCode();
           } else if (q instanceof BooleanQuery bq) {
               List<BooleanClause> clauses = new ArrayList<>(bq.clauses());
               clauses.sort((a, b) -> a.toString().compareTo(b.toString()));
               for (BooleanClause clause : clauses) {
                   hashCode = prime * hashCode + clause.getOccur().ordinal();
                   hashCode = prime * hashCode + 
queryHashCodeStable(clause.getQuery());
               }
           } else if (q instanceof MatchAllDocsQuery) {
               hashCode = 0x1234567890abcdefL;
           } else if (q instanceof ExactSortedNumericDocValuesQuery esndvq) {
               hashCode = Long.hashCode(esndvq.getValue());
           } else {
               // eg numeric range queries
               hashCode = q.toString().hashCode();
           }
           // System.out.println("hashCode(" + q + ") = " + hashCode);
           return hashCode;
       }
   ```
   


-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to