apache / apache/lucene

Register nested queries (ToParentBlockJoinQuery) to Lucene Monitor

Open
#12,406 4 comments 1 reaction 0 assignees View on GitHub
module:monitor type:enhancement
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

### Description

I use Lucene Monitor with regular `Document` objects and it works just fine. The thing is that I'd like to match with Solr queries that I use in a nested collection, but I've couldn't get this work. I'm openning this ticket as a request because I'm not sure if there is any official support for nested queries in `lucene-monitor`.

I've tries to match documents with nested queries that needs to match both fields of the parent doc and both fields from the child doc. To acheive this I've tried to use `Monitor#match` and give it `Document[]`, but from what I understand Lucene Monitor doesn't enable queries with "context" of other documents.

I'd like to know if it can work in any way right now, and if not I'd like to know what's needed to be done to contribute such feature.

The code I've tried it with is here

```java
@Override
public void run(ApplicationArguments args) throws Exception {
MonitorConfiguration monitorConfig = new MonitorConfiguration();
Monitor monitor = new Monitor(new StandardAnalyzer(), new TermFilteredPresearcher(), monitorConfig);

registerQueries(monitor);

// Creating a parent document with child documents
Document parentDoc = new Document();
parentDoc.add(new StringField("id", "g1", Field.Store.YES));
parentDoc.add(new StringField("color", "green", Field.Store.YES));
parentDoc.add(new StringField("title", "Grass", Field.Store.YES));
parentDoc.add(new StoredField("isParent", "true"));

// Creating child document
Document childDoc1 = new Document();
childDoc1.add(new TextField("childField1", "childValue1", Field.Store.YES));
childDoc1.add(new StringField("isParent", "false", Field.Store.YES));

Document childDoc2 = new Document();
childDoc2.add(new TextField("childField2", "childValue2", Field.Store.YES));
childDoc2.add(new StringField("isParent", "false", Field.Store.YES));

Document[] documents = {parentDoc,childDoc1};
MultiMatchingQueries hm = monitor.match(documents, HighlightsMatch.MATCHER);
log.info("Got " + hm.getMatchCount(0) + " matches");
hm.getMatches(0).forEach(m -> {
log.info("Match: " + m.getQueryId() + " with " + m.getHitCount());
m.getHits("childField1").forEach(h -> {
log.info(" hit: " + h.toString() + " - " + childDoc1.get("childField1").substring(h.startOffset, h.endOffset));
});
});
}

private void registerQueries(Monitor monitor) throws IOException, ParseException {
MonitorQuery monitorQuery = newMonitorQuery("ChildQuery1", "childField1:childValue1", Collections.singletonMap("customer", "123"));
monitor.register(monitorQuery);
monitor.register(newMonitorQuery("ChildQuery2", "childField2:childValue2", Collections.singletonMap("customer", "124")));
}

private MonitorQuery newMonitorQuery(String id, String queryString, Map metadata) throws ParseException {
QueryParser qp = new QueryParser("childField1", new StandardAnalyzer());
Query childQuery = qp.parse(queryString);

// Construct ToParentBlockJoinQuery from child query
BitSetProducer parentFilter = new QueryBitSetProducer(new TermQuery(new Term("isParent", "true")));
ToParentBlockJoinQuery parentJoinQuery = new ToParentBlockJoinQuery(childQuery, parentFilter, ScoreMode.None);

log.info("Registered monitor query " + id);
return new MonitorQuery(id, parentJoinQuery, queryString, metadata);
}
```

[Here is the code's repo](https://github.com/almogtavor/nested-lucene-monitor/blob/main/src/main/java/io/github/almogtavor/service/NestedLuceneMonitorService.java).

Contributor guide

Open the contributing guide

Research direction

Start with NestedLuceneMonitorService.java in the linked example repository and the Monitor#match call, then trace how the ToParentBlockJoinQuery is registered and evaluated. Compare the current behavior with the parent-and-child matching scenario described in the issue. Done means establishing whether nested queries are supported and, if not, defining and implementing the required behavior with corresponding tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.