Hi,
I am trying to create and run a "join" query using JoinUitl.createJoinQuery() 
in Lucene 10.2.0. However, the query returns 0 results. I am attaching my 
little test program. Can you please tell me what I am doing wrong?
Thanks a lot,Markos.

import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.Random;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.index.Term;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.search.join.JoinUtil;
import org.apache.lucene.search.join.ScoreMode;

import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.classic.ParseException;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import org.apache.lucene.util.BytesRef;

public class LuceneTest3 {

  static Directory theDirectory;

  static IndexWriter theWriter;

  static int batchSize = 10;

  static String[] texts = {
      "This is the text to be indexed.",
      "Please index this text as text and as binary.",
      "I love France",
      "It is getting too hot",
      "I need a vacation",

      "But that will not be enough to fix Germany structural problems. One is a lack of domestic energy sources",

      "Overall, Germany industrial sector is struggling to cope with not only the high price of energy but with the transition to a future that is more nimble and more digital.",

      "Germany failed to reach its goal, set in 2017, of requiring all public offices in Germany to offer digital services by the end of 2022",

      "I hate Germany. Germany is a really bad country. Don't got to Germany ever"
  };

  static Random rand = new Random(1);

  public static void main(String[] args) {

    Path indexDirPath = Paths.get("/home/markos/kvs/lucene-10.2.0/myStuff/TestIndex");
    batchSize = Integer.parseInt(args[0]);
    
    File indexDir = new File("/home/markos/kvs/lucene-10.2.0/myStuff/TestIndex");
    deleteDir(indexDir);

    try {
      theDirectory = FSDirectory.open(indexDirPath);

      IndexWriterConfig conf = new IndexWriterConfig();
      conf.setSimilarity(new BM25Similarity());

      theWriter = new IndexWriter(theDirectory, conf);

      int numDocs = 100;

      for (int i = 1; i <= numDocs; ++i) {
        ArrayList<Document> docs = createDocs(i);
        for (Document doc : docs) {
          theWriter.addDocument(doc);
        }
      }

      theWriter.commit();

      DirectoryReader reader = DirectoryReader.open(theWriter);

      System.out.println("\nreader2.numDocs() after commit1: " + reader.numDocs());

      IndexSearcher searcher = new IndexSearcher(reader);
      StoredFields storedFields = searcher.storedFields();

      QueryParser parser = new QueryParser("text1", theWriter.getAnalyzer());

      String textQuery = "+text2:Germany +text3:Germany";
      Query fromQuery;
      try {
        fromQuery = parser.parse(textQuery);
      } catch (ParseException e) {
        System.out.println(e);
        return;
      }

      //Query topQuery = fromQuery; 
      Query topQuery = JoinUtil.createJoinQuery("pk_c",    // fromField
                                                false,
                                                "pk_p",    // toField
                                                fromQuery, // fromQuery
                                                searcher,
                                                ScoreMode.Total);

      TopDocs results = searcher.search(topQuery, batchSize);

      int numResults = results.scoreDocs.length;
      System.out.println("Got " + numResults + " results");

      for (int i = 0; i < numResults; ++i) {
        ScoreDoc res = results.scoreDocs[i];
        Document doc = storedFields.document(res.doc);
        int pk = Integer.parseInt(doc.get("pk"));
        String elem = doc.get("elem");
        System.out.println("pk: " + pk + ":" + elem + " score: " + res.score);
      }
    } catch (IOException e) {
      System.out.println("Got IOException:\n" + e);
    } finally {
      try {
        theWriter.close();
      } catch (IOException ce) {
        System.out.println("Got IOException during close:\n" + ce);
      }
    }
  }

  private static ArrayList<Document> createDocs(int id) {

    String idStr = Integer.toString(id);
    BytesRef idBytes = new BytesRef(idStr);

    ArrayList<Document> docs = new ArrayList<Document>();

    Document pdoc = new Document();
    pdoc.add(new StringField("pk", idStr, Field.Store.YES));
    pdoc.add(new SortedDocValuesField("pk_p", idBytes));
    String text1 = texts[rand.nextInt(9)];
    pdoc.add(new TextField("text1", text1, Field.Store.NO));

    System.out.println("\nInserting: " + id + " : " + text1);

    for (int i = 0; i < 5; ++i) {
      Document cdoc = new Document();
      cdoc.add(new StringField("pk", idStr, Field.Store.YES));
      cdoc.add(new SortedDocValuesField("pk_c", idBytes));
      cdoc.add(new StringField("elem", Integer.toString(i), Field.Store.YES));
      String text2 = texts[rand.nextInt(9)];
      String text3 = texts[rand.nextInt(9)];
      cdoc.add(new TextField("text2", text2, Field.Store.NO));
      cdoc.add(new TextField("text3", text3, Field.Store.NO));
      docs.add(cdoc);
      System.out.println("Inserting: " + id + ":" + i + " :\n" + 
                         text2 + "\n" + text3);
    }

    docs.add(pdoc);
    return docs;
  }

  private static void deleteDir(File dir) {

    if (dir.isDirectory()) {
      File[] files = dir.listFiles();
      for (File f : files) {
        deleteDir(f);
      }
    }

    dir.delete();
  }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org
For additional commands, e-mail: java-user-h...@lucene.apache.org

Reply via email to