-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SOLR-12238] Synonym Queries boost #357
Merged
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
74bbc02
[SOLR-12238] Synonym Queries boost by payload + tests
alessandrobenedetti cacf48a
[SOLR-12238] query builder style revert
alessandrobenedetti ed3aa61
[SOLR-12238] query edismax boost test
alessandrobenedetti 5c1e50c
[SOLR-12238] comments correction
alessandrobenedetti 3d7ad62
[SOLR-12238] use PayloadHelper to decode the payload
alessandrobenedetti 3d77474
[SOLR-12238] tests to verify managed synonym handle weighted synonyms…
alessandrobenedetti e02ed0d
[SOLR-12238] minor style
alessandrobenedetti 2e64120
[SOLR-12238] precommit fixes
alessandrobenedetti 9abd7ab
Merge remote-tracking branch 'upstream/master' into SOLR-12238
alessandrobenedetti 3258a1c
Merge branch 'upstreamMaster' into SOLR-12238
alessandrobenedetti 42f9a99
[SOLR-12238] merge conflicts fixed
alessandrobenedetti 6113658
[SOLR-12238] temp commit
alessandrobenedetti b44be68
[SOLR-12238] re-design
alessandrobenedetti f5567a6
[SOLR-12238] re-design
alessandrobenedetti 89e55d1
[SOLR-12238] re-design and refinement
alessandrobenedetti 5321a93
[SOLR-12238] re-design and refinement
alessandrobenedetti 60551b4
[SOLR-12238] minor fix for an pre-commit
alessandrobenedetti e0f23b1
[SOLR-12238] schema simplification
alessandrobenedetti e715fb1
[SOLR-12238] Refactor based on PR comments
alessandrobenedetti 6d2d291
[SOLR-12238] Extract the functionality to Solr classes keeping Lucene…
alessandrobenedetti 6d9f1ee
[SOLR-12238] documentation improvement
alessandrobenedetti 7f70c00
LUCENE-9171: Add BoostAttribute handling to QueryBuilder
romseygeek 96e1ed3
imports
romseygeek 8f349f0
javadocs
romseygeek edcdb8a
Merge remote-tracking branch 'AlanCommitter/queryparser/boosts' into …
alessandrobenedetti 1d8cd9a
Merge remote-tracking branch 'upstream/master' into SOLR-12238
alessandrobenedetti 9928a4a
[SOLR-12238] first implementation of the boostAttribute approach
alessandrobenedetti 71b5a43
[SOLR-12238] refinement of the boostAttribute approach
alessandrobenedetti a75d618
[SOLR-12238] test for boost token filter
alessandrobenedetti 61995cd
[SOLR-12238] package info fix
alessandrobenedetti 52d848a
[SOLR-12238] adjustments of the github PR feedback
alessandrobenedetti 6da4b82
[SOLR-12238] adjustments of the github PR feedback
alessandrobenedetti b6cbace
[SOLR-12238] adjustments of the github PR feedback
alessandrobenedetti 54225c8
[SOLR-12238] adjustments of the github PR feedback
alessandrobenedetti bfdaf9d
[SOLR-12238] docs
alessandrobenedetti 9d9f1c6
[SOLR-12238] docs
alessandrobenedetti 1881390
[SOLR-12238] minor spi name fix
alessandrobenedetti 81a4217
[SOLR-12238] adjustments of the github PR feedback
alessandrobenedetti 8b8cb99
[SOLR-12238] adjustments of the github PR feedback
alessandrobenedetti 899553b
Merge remote-tracking branch 'upstream/master' into SOLR-12238
alessandrobenedetti e4da3fb
[SOLR-12238] minor comment fix
alessandrobenedetti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
.../analysis/common/src/java/org/apache/lucene/analysis/boost/DelimitedBoostTokenFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.analysis.boost; | ||
|
||
import org.apache.lucene.analysis.TokenFilter; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.apache.lucene.search.BoostAttribute; | ||
|
||
import java.io.IOException; | ||
|
||
|
||
/** | ||
* Characters before the delimiter are the "token", those after are the boost. | ||
* <p> | ||
* For example, if the delimiter is '|', then for the string "foo|0.7", foo is the token | ||
* and 0.7 is the boost. | ||
* <p> | ||
* Note make sure your Tokenizer doesn't split on the delimiter, or this won't work | ||
*/ | ||
public final class DelimitedBoostTokenFilter extends TokenFilter { | ||
private final char delimiter; | ||
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class); | ||
private final BoostAttribute boostAtt = addAttribute(BoostAttribute.class); | ||
|
||
public DelimitedBoostTokenFilter(TokenStream input, char delimiter) { | ||
super(input); | ||
this.delimiter = delimiter; | ||
} | ||
|
||
@Override | ||
public boolean incrementToken() throws IOException { | ||
if (input.incrementToken()) { | ||
final char[] buffer = termAtt.buffer(); | ||
final int length = termAtt.length(); | ||
for (int i = 0; i < length; i++) { | ||
if (buffer[i] == delimiter) { | ||
float boost = Float.parseFloat(new String(buffer, i + 1, (length - (i + 1)))); | ||
boostAtt.setBoost(boost); | ||
termAtt.setLength(i); | ||
return true; | ||
} | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...is/common/src/java/org/apache/lucene/analysis/boost/DelimitedBoostTokenFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.analysis.boost; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.util.TokenFilterFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Factory for {@link DelimitedBoostTokenFilter}. | ||
* <pre class="prettyprint"> | ||
* <fieldType name="text_dlmtd" class="solr.TextField" positionIncrementGap="100"> | ||
* <analyzer> | ||
* <tokenizer class="solr.WhitespaceTokenizerFactory"/> | ||
* <filter class="solr.DelimitedBoostTokenFilterFactory" delimiter="|"/> | ||
* </analyzer> | ||
* </fieldType></pre> | ||
* | ||
* @lucene.spi {@value #NAME} | ||
*/ | ||
public class DelimitedBoostTokenFilterFactory extends TokenFilterFactory { | ||
|
||
/** | ||
* SPI name | ||
*/ | ||
public static final String NAME = "delimitedBoost"; | ||
public static final String DELIMITER_ATTR = "delimiter"; | ||
public static final char DEFAULT_DELIMITER = '|'; | ||
|
||
private final char delimiter; | ||
|
||
/** | ||
* Creates a new DelimitedPayloadTokenFilterFactory | ||
*/ | ||
public DelimitedBoostTokenFilterFactory(Map<String, String> args) { | ||
super(args); | ||
delimiter = getChar(args, DELIMITER_ATTR, DEFAULT_DELIMITER); | ||
if (!args.isEmpty()) { | ||
throw new IllegalArgumentException("Unknown parameters: " + args); | ||
} | ||
} | ||
|
||
@Override | ||
public DelimitedBoostTokenFilter create(TokenStream input) { | ||
return new DelimitedBoostTokenFilter(input, delimiter); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
lucene/analysis/common/src/java/org/apache/lucene/analysis/boost/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Provides various convenience classes for creating boosts on Tokens. | ||
*/ | ||
package org.apache.lucene.analysis.boost; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...lysis/common/src/test/org/apache/lucene/analysis/boost/DelimitedBoostTokenFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.analysis.boost; | ||
|
||
import org.apache.lucene.analysis.BaseTokenStreamTestCase; | ||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; | ||
import org.apache.lucene.search.BoostAttribute; | ||
|
||
public class DelimitedBoostTokenFilterTest extends BaseTokenStreamTestCase { | ||
|
||
public void testBoosts() throws Exception { | ||
String test = "The quick|0.4 red|0.5 fox|0.2 jumped|0.1 over the lazy|0.8 brown|0.9 dogs|0.9"; | ||
DelimitedBoostTokenFilter filter = new DelimitedBoostTokenFilter | ||
(whitespaceMockTokenizer(test), | ||
DelimitedBoostTokenFilterFactory.DEFAULT_DELIMITER); | ||
CharTermAttribute termAtt = filter.getAttribute(CharTermAttribute.class); | ||
BoostAttribute boostAtt = filter.addAttribute(BoostAttribute.class); | ||
filter.reset(); | ||
assertTermEquals("The", filter, termAtt, boostAtt, 1.0f); | ||
assertTermEquals("quick", filter, termAtt, boostAtt, 0.4f); | ||
assertTermEquals("red", filter, termAtt, boostAtt, 0.5f); | ||
assertTermEquals("fox", filter, termAtt, boostAtt, 0.2f); | ||
assertTermEquals("jumped", filter, termAtt, boostAtt, 0.1f); | ||
assertTermEquals("over", filter, termAtt, boostAtt, 1.0f); | ||
assertTermEquals("the", filter, termAtt, boostAtt, 1.0f); | ||
assertTermEquals("lazy", filter, termAtt, boostAtt, 0.8f); | ||
assertTermEquals("brown", filter, termAtt, boostAtt, 0.9f); | ||
assertTermEquals("dogs", filter, termAtt, boostAtt, 0.9f); | ||
assertFalse(filter.incrementToken()); | ||
filter.end(); | ||
filter.close(); | ||
} | ||
|
||
public void testNext() throws Exception { | ||
String test = "The quick|0.1 red|0.2 fox|0.3 jumped|0.4 over the lazy|0.5 brown|0.6 dogs|0.6"; | ||
DelimitedBoostTokenFilter filter = new DelimitedBoostTokenFilter | ||
(whitespaceMockTokenizer(test), | ||
DelimitedBoostTokenFilterFactory.DEFAULT_DELIMITER); | ||
filter.reset(); | ||
assertTermEquals("The", filter, 1.0f); | ||
assertTermEquals("quick", filter, 0.1f); | ||
assertTermEquals("red", filter, 0.2f); | ||
assertTermEquals("fox", filter, 0.3f); | ||
assertTermEquals("jumped", filter, 0.4f); | ||
assertTermEquals("over", filter, 1.0f); | ||
assertTermEquals("the", filter, 1.0f); | ||
assertTermEquals("lazy", filter, 0.5f); | ||
assertTermEquals("brown", filter, 0.6f); | ||
assertTermEquals("dogs", filter, 0.6f); | ||
assertFalse(filter.incrementToken()); | ||
filter.end(); | ||
filter.close(); | ||
} | ||
|
||
void assertTermEquals(String expected, TokenStream stream, float expectedBoost) throws Exception { | ||
CharTermAttribute termAtt = stream.getAttribute(CharTermAttribute.class); | ||
BoostAttribute boostAtt = stream.addAttribute(BoostAttribute.class); | ||
assertTrue(stream.incrementToken()); | ||
assertEquals(expected, termAtt.toString()); | ||
float actualBoost = boostAtt.getBoost(); | ||
assertTrue(actualBoost + " does not equal: " + expectedBoost, actualBoost == expectedBoost); | ||
} | ||
|
||
void assertTermEquals(String expected, TokenStream stream, CharTermAttribute termAtt, BoostAttribute boostAtt, float expectedBoost) throws Exception { | ||
assertTrue(stream.incrementToken()); | ||
assertEquals(expected, termAtt.toString()); | ||
float actualBoost = boostAtt.getBoost(); | ||
assertTrue(actualBoost + " does not equal: " + expectedBoost, actualBoost == expectedBoost); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I can see why you chose a new "boost" sub-package because the payload based filter from which you drew inspiration was in a "payload" sub-package, I lean towards the "miscellaneous" package. Note that DelimitedTermFrequencyTokenFilter is in "miscellaneous" too. WDYT @romseygeek ? Or maybe we need a new "delimited" sub-package for all these to go; I dunno.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the
boost
package - I'm already thinking about aTypeToBoostTokenFilter
that would automatically boost tokens marked with aSYNONYM
type for example, and there are probably other boosting filters we can come up with, so a package to collect them all makes sense to me. I prefer to group packages by functionality rather than implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So let's keep boost package then? no strong opinion here my side