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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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);
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);
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?
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"));
}
}
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"));
}
}