Menu

Document Score...

Help
Peter
2005-08-03
2013-04-17
  • Peter

    Peter - 2005-08-03

    How do you get the document score - I believe this is how well it matches a query? - when getting the search results as a Java list?

    Je l'essayerai en franais...
    Comment obtenez-vous les points de document - est-ce que je crois que c'est  quel point lui des allumettes une question?  - quand obtenir la recherche rsulte-t-il comme Java List?

     
    • Rida Benjelloun

      Rida Benjelloun - 2005-08-16

      Hi,
      Actually, you can't get the score of the hit in a java list. I will try to fix the problem.
      You can use this methode :
      public static void search(File indexDir, String queryStr)  throws Exception{
        Directory fsDir = FSDirectory.getDirectory(indexDir, false);
        IndexSearcher is = new IndexSearcher(fsDir);

        Query query = QueryParser.parse(queryStr, "contents", new StandardAnalyzer());
        Hits hits = is.search(query);
        System.out.println("Found " + hits.length() +
                          " document(s) that matched query '" + q + "':");
        for (int i = 0; i < hits.length(); i++) {
          Document doc = hits.doc(i);

          System.out.println("score : "+ hits.score(i));
          System.out.println(doc.get("filename"));
        }
      }

       
    • Peter

      Peter - 2005-08-16

      Thank you for your response. 

      I've been looking through the lucene documentation and I've had a lot of trouble finding documentation to explain the document score.  All I've found is that the score is a number of type float between 1 and 0, which is somehow normalized based on the length of the content that is searched.  Do you know anywhere that has a thorough explanation of this?

      In case anyone else is interested...
      I modified the code that you provided to do a MultiFieldQueryParse of the query and also to print the file path instead of name - the latter only returned null for me.

          /**
           * Search to return the document scores for a query
           * @param indexDir - the directory containing the index
           * @param queryStr - the query
           * @throws Exception
           */
          public void search(File indexDir, String queryStr)  throws Exception{
              Directory fsDir = FSDirectory.getDirectory(indexDir, false);
              IndexSearcher is = new IndexSearcher(fsDir);
             
              String[] fields = {"title",
                                 "subject",
                                 "creator",
                                 "description",
                                 "publisher",
                                 "contributor",
                                 "fullText"};
             
              Query query = MultiFieldQueryParser.parse(queryStr,
                                               fields,       
                                               new StandardAnalyzer());
              Hits hits = is.search(query);
              System.out.println("Found " + hits.length() +
                      " document(s) that matched query '" + query + "':");
              for (int i = 0; i < hits.length(); i++) {
                  Document doc = hits.doc(i);
                 
                  System.out.println("score : "+ hits.score(i));
                  System.out.println(doc.get("path"));
              }
          }

       

Log in to post a comment.