Fix NullPointerException in Monitor.getQuery when query is not present (#12736)

This commit is contained in:
Davis Cook 2023-10-31 11:05:31 -04:00 committed by GitHub
parent 361f0a4524
commit bbf3221049
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 8 deletions

View File

@ -273,6 +273,8 @@ Bug Fixes
* GITHUB#12727: Ensure negative scores are not returned by vector similarity functions (Ben Trent)
* GITHUB#12736: Fix NullPointerException when Monitor.getQuery cannot find the requested queryId (Davis Cook)
Build
---------------------

View File

@ -68,7 +68,7 @@ abstract class QueryIndex implements Closeable {
search(
new TermQuery(new Term(FIELDS.query_id, queryId)),
(id, query, dataValues) -> bytesHolder[0] = dataValues.mq.binaryValue());
return serializer.deserialize(bytesHolder[0]);
return bytesHolder[0] != null ? serializer.deserialize(bytesHolder[0]) : null;
}
public void scan(QueryCollector matcher) throws IOException {

View File

@ -28,16 +28,21 @@ public class TestMonitorPersistence extends MonitorTestBase {
private Path indexDirectory = createTempDir();
public void testCacheIsRepopulated() throws IOException {
Document doc = new Document();
doc.add(newTextField(FIELD, "test", Field.Store.NO));
protected Monitor newMonitorWithPersistence() throws IOException {
MonitorConfiguration config =
new MonitorConfiguration()
.setIndexPath(
indexDirectory, MonitorQuerySerializer.fromParser(MonitorTestBase::parse));
try (Monitor monitor = new Monitor(ANALYZER, config)) {
return new Monitor(ANALYZER, config);
}
public void testCacheIsRepopulated() throws IOException {
Document doc = new Document();
doc.add(newTextField(FIELD, "test", Field.Store.NO));
try (Monitor monitor = newMonitorWithPersistence()) {
monitor.register(
mq("1", "test"),
mq("2", "test"),
@ -58,7 +63,7 @@ public class TestMonitorPersistence extends MonitorTestBase {
e.getMessage());
}
try (Monitor monitor2 = new Monitor(ANALYZER, config)) {
try (Monitor monitor2 = newMonitorWithPersistence()) {
assertEquals(4, monitor2.getQueryCount());
assertEquals(4, monitor2.match(doc, QueryMatch.SIMPLE_MATCHER).getMatchCount());
@ -67,6 +72,21 @@ public class TestMonitorPersistence extends MonitorTestBase {
}
}
public void testGetQueryPresent() throws IOException {
try (Monitor monitor = newMonitorWithPersistence()) {
MonitorQuery monitorQuery = mq("1", "test");
monitor.register(monitorQuery);
assertEquals(monitorQuery, monitor.getQuery("1"));
}
}
public void testGetQueryNotPresent() throws IOException {
try (Monitor monitor = newMonitorWithPersistence()) {
assertNull(monitor.getQuery("1"));
}
}
public void testEphemeralMonitorDoesNotStoreQueries() throws IOException {
try (Monitor monitor2 = newMonitor(ANALYZER)) {