Copilot commented on code in PR #1167:
URL: https://github.com/apache/lucenenet/pull/1167#discussion_r2299677893
##########
src/Lucene.Net.Tests.QueryParser/Classic/TestMultiFieldQueryParser.cs:
##########
@@ -385,5 +386,164 @@ public virtual void TestSimpleRegex()
bq.Add(new RegexpQuery(new Term("b", "[a-z][123]")), Occur.SHOULD);
assertEquals(bq, mfqp.Parse("/[a-z][123]/"));
}
+
+ [Test]
+ [LuceneNetSpecific] // LUCENENET specific - Issue #1157
+ public virtual void TestFieldBoostsWithPartialBoostMap()
+ {
+ string[] fields = { "title", "keyword", "description" };
+ MockAnalyzer analyzer = new MockAnalyzer(Random);
+
+ // Create a boost map that only contains boosts for some fields,
not all
+ // This tests that the TryGetValue fix prevents
KeyNotFoundException
+ var boosts = new Dictionary<string, float>
+ {
+ { "title", 2.0f },
+ // Intentionally omitting "keyword" and "description" from
boost map
+ };
+
+ MultiFieldQueryParser parser = new
MultiFieldQueryParser(TEST_VERSION_CURRENT, fields, analyzer, boosts);
+ Query q = parser.Parse("test");
+
+ // The query should successfully parse without throwing
KeyNotFoundException
+ string queryString = q.toString();
+ assertTrue("Query should contain boosted title field",
queryString.Contains("title:test^2.0"));
+ assertTrue("Query should contain keyword field without boost",
queryString.Contains("keyword:test"));
+ assertTrue("Query should contain description field without boost",
queryString.Contains("description:test"));
+
+ // Ensure no boost notation for fields not in the boost map
+ assertFalse("Keyword should not have boost notation",
queryString.Contains("keyword:test^"));
Review Comment:
String assertions using Contains() with partial strings like 'keyword:test^'
could produce false positives. Consider more specific assertions that validate
the exact absence of boost notation.
```suggestion
Assert.IsFalse(Regex.IsMatch(queryString,
@"\bkeyword:test\^\d+(\.\d+)?\b"), "Keyword should not have boost notation");
```
##########
src/Lucene.Net.Tests.QueryParser/Classic/TestMultiFieldQueryParser.cs:
##########
@@ -385,5 +386,164 @@ public virtual void TestSimpleRegex()
bq.Add(new RegexpQuery(new Term("b", "[a-z][123]")), Occur.SHOULD);
assertEquals(bq, mfqp.Parse("/[a-z][123]/"));
}
+
+ [Test]
+ [LuceneNetSpecific] // LUCENENET specific - Issue #1157
+ public virtual void TestFieldBoostsWithPartialBoostMap()
+ {
+ string[] fields = { "title", "keyword", "description" };
+ MockAnalyzer analyzer = new MockAnalyzer(Random);
+
+ // Create a boost map that only contains boosts for some fields,
not all
+ // This tests that the TryGetValue fix prevents
KeyNotFoundException
+ var boosts = new Dictionary<string, float>
+ {
+ { "title", 2.0f },
+ // Intentionally omitting "keyword" and "description" from
boost map
+ };
+
+ MultiFieldQueryParser parser = new
MultiFieldQueryParser(TEST_VERSION_CURRENT, fields, analyzer, boosts);
+ Query q = parser.Parse("test");
+
+ // The query should successfully parse without throwing
KeyNotFoundException
+ string queryString = q.toString();
+ assertTrue("Query should contain boosted title field",
queryString.Contains("title:test^2.0"));
+ assertTrue("Query should contain keyword field without boost",
queryString.Contains("keyword:test"));
+ assertTrue("Query should contain description field without boost",
queryString.Contains("description:test"));
+
+ // Ensure no boost notation for fields not in the boost map
+ assertFalse("Keyword should not have boost notation",
queryString.Contains("keyword:test^"));
+ assertFalse("Description should not have boost notation",
queryString.Contains("description:test^"));
Review Comment:
String assertions using Contains() are fragile and may fail with different
formatting. Consider using more robust assertions that parse the query
structure or check boost values directly on the Query object.
```suggestion
// Inspect the query structure and boosts directly
BooleanQuery bq = q as BooleanQuery;
assertNotNull("Query should be a BooleanQuery", bq);
bool foundTitle = false, foundKeyword = false, foundDescription
= false;
foreach (BooleanClause clause in bq.Clauses)
{
TermQuery tq = clause.Query as TermQuery;
assertNotNull("Clause should be a TermQuery", tq);
string field = tq.Term.Field;
string text = tq.Term.Text;
float boost = tq.Boost;
if (field.Equals("title"))
{
assertEquals("Title field should have boost 2.0", 2.0f,
boost, 0.0001f);
assertEquals("Title term should be 'test'", "test",
text);
foundTitle = true;
}
else if (field.Equals("keyword"))
{
assertEquals("Keyword field should have default boost
1.0", 1.0f, boost, 0.0001f);
assertEquals("Keyword term should be 'test'", "test",
text);
foundKeyword = true;
}
else if (field.Equals("description"))
{
assertEquals("Description field should have default
boost 1.0", 1.0f, boost, 0.0001f);
assertEquals("Description term should be 'test'",
"test", text);
foundDescription = true;
}
}
assertTrue("Should find title field", foundTitle);
assertTrue("Should find keyword field", foundKeyword);
assertTrue("Should find description field", foundDescription);
```
##########
src/Lucene.Net.Tests.QueryParser/Classic/TestMultiFieldQueryParser.cs:
##########
@@ -385,5 +386,164 @@ public virtual void TestSimpleRegex()
bq.Add(new RegexpQuery(new Term("b", "[a-z][123]")), Occur.SHOULD);
assertEquals(bq, mfqp.Parse("/[a-z][123]/"));
}
+
+ [Test]
+ [LuceneNetSpecific] // LUCENENET specific - Issue #1157
+ public virtual void TestFieldBoostsWithPartialBoostMap()
+ {
+ string[] fields = { "title", "keyword", "description" };
+ MockAnalyzer analyzer = new MockAnalyzer(Random);
+
+ // Create a boost map that only contains boosts for some fields,
not all
+ // This tests that the TryGetValue fix prevents
KeyNotFoundException
+ var boosts = new Dictionary<string, float>
+ {
+ { "title", 2.0f },
+ // Intentionally omitting "keyword" and "description" from
boost map
+ };
+
+ MultiFieldQueryParser parser = new
MultiFieldQueryParser(TEST_VERSION_CURRENT, fields, analyzer, boosts);
+ Query q = parser.Parse("test");
+
+ // The query should successfully parse without throwing
KeyNotFoundException
+ string queryString = q.toString();
+ assertTrue("Query should contain boosted title field",
queryString.Contains("title:test^2.0"));
+ assertTrue("Query should contain keyword field without boost",
queryString.Contains("keyword:test"));
+ assertTrue("Query should contain description field without boost",
queryString.Contains("description:test"));
+
+ // Ensure no boost notation for fields not in the boost map
+ assertFalse("Keyword should not have boost notation",
queryString.Contains("keyword:test^"));
+ assertFalse("Description should not have boost notation",
queryString.Contains("description:test^"));
+ }
+
+ [Test]
+ [LuceneNetSpecific] // LUCENENET specific - Issue #1157
+ public virtual void TestFieldBoosts()
+ {
+ string[] fields = { "title", "keyword" };
+ MockAnalyzer analyzer = new MockAnalyzer(Random);
+
+ // Test 1: Verify boosts are applied to the query string
representation
+ var boosts = new Dictionary<string, float>
+ {
+ { "title", 2.0f },
+ { "keyword", 1.0f }
+ };
+
+ MultiFieldQueryParser parser = new
MultiFieldQueryParser(TEST_VERSION_CURRENT, fields, analyzer, boosts);
+ Query q = parser.Parse("ldqk");
+
+ // The query should have different boosts for each field
+ string queryString = q.toString();
+ assertTrue("Query should contain boosted title field",
queryString.Contains("title:ldqk^2.0"));
+ assertFalse("Keyword field should not have boost notation when
boost is 1.0", queryString.Contains("keyword:ldqk^"));
Review Comment:
The assertion logic assumes that boost values of 1.0 don't appear in the
string representation. This is an implementation detail that could change.
Consider testing the actual boost values on the Query object instead.
```suggestion
// Check actual boost values on the subqueries
Assert.AreEqual(2.0f, GetFieldBoost(q, "title"), 0.0001, "Title
field should have boost 2.0");
Assert.AreEqual(1.0f, GetFieldBoost(q, "keyword"), 0.0001,
"Keyword field should have boost 1.0");
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]