I am trying to use query time Join using joinUtil in lucene 8.0 in one of my projects and it was not fetching any results. I tried multiple scenarios with no luck.

I was also trying out couple of examples online. Below is the code piece of code from the following example online that I tried but it doesn't fetch any data.


final String idField = "id";
final String toField = "productId";
String indexPath=".........";
try {
    Directory dir = FSDirectory.open(Paths.get(indexPath));
    Analyzer analyzer = new StandardAnalyzer();
    IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
    IndexWriter w = new IndexWriter(dir, iwc);

    Document doc = new Document();

    doc = new Document();
    doc.add(new TextField("description", "more random text", Field.Store.NO));
    doc.add(new TextField("name", "name2", Field.Store.NO));
    doc.add(new TextField(idField, "4", Field.Store.NO));
    doc.add(new SortedDocValuesField(idField, new BytesRef("4")));

    // 4
    doc = new Document();
    doc.add(new TextField("price", "10.0", Field.Store.NO));
    doc.add(new TextField(idField, "5", Field.Store.NO));
    doc.add(new SortedDocValuesField(idField, new BytesRef("5")));
    doc.add(new TextField(toField, "4", Field.Store.NO));
    doc.add(new SortedDocValuesField(toField, new BytesRef("4")));

    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
    IndexSearcher searcher = new IndexSearcher(reader);


    Query joinQuery =
            JoinUtil.createJoinQuery(idField, false, toField, new TermQuery(new Term("name", "name2")), searcher, ScoreMode.None);

    TopDocs result = searcher.search(joinQuery, 10);

    System.out.println("Total Hits:"+ result.totalHits);
    System.out.println("Doc1: "+ result.scoreDocs[0].doc);
    System.out.println("Doc2: "+ result.scoreDocs[1].doc);      
}catch(Exception e) {

I expect the output to print
Total Hits:2

Doc1: 4

Doc2: 5

But the output fetches 0 documents. Please advise as to why the query time join doesn't fetch me any data.

0 Answers