Java Code Examples for org.apache.lucene.search.Hits#score()

The following examples show how to use org.apache.lucene.search.Hits#score() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NGramTestSetup.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
protected int thresholdHits(Hits hits) throws IOException {
    /** We could consider doing thresholding as a relative thing...
     * instead of checking against an absolute value, we grab top score
     * then filter based on difference from that...
     */
    int counter = 0;
    for (int i=0; i < hits.length(); i++) {
        if (hits.score(i) >= score_threshold) {
            counter++;
        }
        else {
            break;
        }
    }
    return counter;
}
 
Example 2
Source File: NGramTestSetup.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
protected int thresholdHits(Hits hits) throws IOException {
    /** We could consider doing thresholding as a relative thing...
     * instead of checking against an absolute value, we grab top score
     * then filter based on difference from that...
     */
    int counter = 0;
    for (int i=0; i < hits.length(); i++) {
        if (hits.score(i) >= score_threshold) {
            counter++;
        }
        else {
            break;
        }
    }
    return counter;
}
 
Example 3
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
        Query q, Set<Term> queryTerms)
    throws IOException {
    log.debug("Parsed Query is " + q.toString());
    log.debug("Looking at index:  " + indexName);
    for (int i = 0; i < hits.length(); i++) {
        if ((i < 10)) {
            Document doc = hits.doc(i);
            Float score = hits.score(i);
            Explanation ex = searcher.explain(q, hits.id(i));
            log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
                    ">: " + doc);
            log.debug("Explanation: " + ex);
            MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
            String fieldName = match.getFieldName();
            String fieldValue = match.getFieldValue();
            log.debug("Guessing that matched fieldName is " + fieldName + " = " +
                    fieldValue);
        }
    }
}
 
Example 4
Source File: IndexManager.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
        Query q, Set<Term> queryTerms)
    throws IOException {
    log.debug("Parsed Query is " + q.toString());
    log.debug("Looking at index:  " + indexName);
    for (int i = 0; i < hits.length(); i++) {
        if ((i < 10)) {
            Document doc = hits.doc(i);
            Float score = hits.score(i);
            Explanation ex = searcher.explain(q, hits.id(i));
            log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
                    ">: " + doc);
            log.debug("Explanation: " + ex);
            MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
            String fieldName = match.getFieldName();
            String fieldValue = match.getFieldValue();
            log.debug("Guessing that matched fieldName is " + fieldName + " = " +
                    fieldValue);
        }
    }
}
 
Example 5
Source File: IndexManager.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
private List<Result> processHits(String indexName, Hits hits, Set<Term> queryTerms,
        String query, String lang)
    throws IOException {
    List<Result> retval = new ArrayList<Result>();
    for (int x = 0; x < hits.length(); x++) {
        Document doc = hits.doc(x);
        Result pr = null;
        if (!isScoreAcceptable(indexName, hits, x, query)) {
            break;
        }
        if (indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) {
            pr = new DocResult(x, hits.score(x), doc);
            String summary = lookupDocSummary(doc, query, lang);
            if (summary != null) {
                ((DocResult)pr).setSummary(summary);
            }
        }
        else if (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0) {
            pr = new HardwareDeviceResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE)  == 0) {
            pr = new SnapshotTagResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) {
            pr = new ServerCustomInfoResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.XCCDF_IDENT_TYPE) == 0) {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("identifier").stringValue(),
                    hits.score(x));
        }
        else {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("name").stringValue(),
                    hits.score(x));
        }
        if (log.isDebugEnabled()) {
            log.debug("Hit[" + x + "] Score = " + hits.score(x) + ", Result = " + pr);
        }
        /**
         * matchingField will help the webUI to understand what field was responsible
         * for this match.  Later implementation should use "Explanation" to determine
         * field, for now we will simply grab one term and return it's field.
         */
        try {
            MatchingField match = new MatchingField(query, doc, queryTerms);
            pr.setMatchingField(match.getFieldName());
            pr.setMatchingFieldValue(match.getFieldValue());
            log.info("hit[" + x + "] matchingField is being set to: <" +
                pr.getMatchingField() + "> based on passed in query field.  " +
                "matchingFieldValue = " + pr.getMatchingFieldValue());
        }
        catch (Exception e) {
            log.error("Caught exception: ", e);
        }
        if (pr != null) {
            retval.add(pr);
        }
        if (maxHits > 0 && x == maxHits) {
            break;
        }
    }
    return retval;
}
 
Example 6
Source File: IndexManager.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param indexName
 * @param hits
 * @param x
 * @param query
 * @return  true - score is acceptable
 *          false - score is NOT acceptable
 * @throws IOException
 */
private boolean isScoreAcceptable(String indexName, Hits hits, int x, String queryIn)
    throws IOException {
    String guessMainQueryTerm = MatchingField.getFirstFieldName(queryIn);

    if ((indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) &&
            (!filterDocResults)) {
        return true;
    }
    /**
     * Dropping matches which are a poor fit.
     * system searches are filtered based on "system_score_threshold"
     * other searches will return 10 best matches, then filter anything below
     * "score_threshold"
     */
    if ((indexName.compareTo(BuilderFactory.SERVER_TYPE) == 0) ||
            (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) ||
            (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE)  == 0) ||
            (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0)) {
        if (hits.score(x) < system_score_threshold) {
            if (log.isDebugEnabled()) {
                log.debug("hits.score(" + x + ") is " + hits.score(x));
                log.debug("Filtering out search results from " + x + " to " +
                        hits.length() + ", due to their score being below " +
                        "system_score_threshold = " + system_score_threshold);
            }
            return false;
        }
    }
    else if (indexName.compareTo(BuilderFactory.ERRATA_TYPE) == 0) {
        if (guessMainQueryTerm.compareTo("name") == 0) {
            if (hits.score(x) < errata_advisory_score_threshold) {
                if (log.isDebugEnabled()) {
                    log.debug("hits.score(" + x + ") is " + hits.score(x));
                    log.debug("Filtering out search results from " + x + " to " +
                        hits.length() + ", due to their score being below " +
                        "errata_advisory_score_threshold = " +
                        errata_advisory_score_threshold);
                }
                return false;
            }
        }
        else {
            if (hits.score(x) < errata_score_threshold) {
                if (log.isDebugEnabled()) {
                    log.debug("hits.score(" + x + ") is " + hits.score(x));
                    log.debug("Filtering out search results from " + x + " to " +
                        hits.length() + ", due to their score being below " +
                        "errata_score_threshold = " +
                        errata_score_threshold);
                }
                return false;
            }
        }
    }
    else if (((hits.score(x) < score_threshold) && (x > 10)) ||
            (hits.score(x) < 0.001)) {
        /**
         * Dropping matches which are a poor fit.
         * First term is configurable, it allows matches like spelling errors or
         * suggestions to be possible.
         * Second term is intended to get rid of pure and utter crap hits
         */
        if (log.isDebugEnabled()) {
            log.debug("hits.score(" + x + ") is " + hits.score(x));
            log.debug("Filtering out search results from " + x + " to " +
                    hits.length() + ", due to their score being below " +
                    "score_threshold = " + score_threshold);
        }
        return false;
    }
    return true;
}
 
Example 7
Source File: SearchInTypeShortName.java    From gAnswer with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public  ArrayList<String> searchType(String s, double thres1, double thres2, int k) throws Exception
{		
	Hits hits = null;
	String queryString = null;
	Query query = null;
	
	IndexSearcher searcher = new IndexSearcher(Globals.localPath+"data/DBpedia2016/lucene/type_fragment_index");
	
	ArrayList<String> typeNames = new ArrayList<String>(); 
	
	//String[] array = s.split(" ");
	//queryString = array[array.length-1];
	queryString = s;

	Analyzer analyzer = new StandardAnalyzer();
	try {
		QueryParser qp = new QueryParser("SplittedTypeShortName", analyzer);
		query = qp.parse(queryString);
	} catch (ParseException e) {
		e.printStackTrace();
	}
	
	if (searcher != null) {
		hits = searcher.search(query);
		
		System.out.println("find " + hits.length() + " answars!");
		if (hits.length() > 0) {
			for (int i=0; i<hits.length(); i++) {
				if (i < k) {
					System.out.println("<<<<---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i));
				    if(hits.score(i) >= thres1){
				    	System.out.println("Score>=thres1("+thres1+") ---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i));
				    	typeNames.add(hits.doc(i).get("TypeShortName"));
				    	//if (satisfiedStrictly(hits.doc(i).get("SplittedTypeShortName"), queryString)) typeNames.add(hits.doc(i).get("TypeShortName"));
				    }
				    else {
				    	//break;
				    }
				}
				else {
				    if(hits.score(i) >= thres2){
				    	System.out.println("<<<<---" + hits.doc(i).get("TypeShortName") + " : " + hits.score(i));
				    	typeNames.add(hits.doc(i).get("TypeShortName"));
				    	//if (satisfiedStrictly(hits.doc(i).get("SplittedTypeShortName"), queryString)) typeNames.add(hits.doc(i).get("TypeShortName"));
				    }
				    else {
				    	break;
				    }						
				}
			}				
		}
	}		
	return typeNames;	
}
 
Example 8
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 4 votes vote down vote up
private List<Result> processHits(String indexName, Hits hits, Set<Term> queryTerms,
        String query, String lang)
    throws IOException {
    List<Result> retval = new ArrayList<Result>();
    for (int x = 0; x < hits.length(); x++) {
        Document doc = hits.doc(x);
        Result pr = null;
        if (!isScoreAcceptable(indexName, hits, x, query)) {
            break;
        }
        if (indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) {
            pr = new DocResult(x, hits.score(x), doc);
            String summary = lookupDocSummary(doc, query, lang);
            if (summary != null) {
                ((DocResult)pr).setSummary(summary);
            }
        }
        else if (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0) {
            pr = new HardwareDeviceResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE)  == 0) {
            pr = new SnapshotTagResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) {
            pr = new ServerCustomInfoResult(x, hits.score(x), doc);
        }
        else if (indexName.compareTo(BuilderFactory.XCCDF_IDENT_TYPE) == 0) {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("identifier").stringValue(),
                    hits.score(x));
        }
        else {
            pr = new Result(x,
                    doc.getField("id").stringValue(),
                    doc.getField("name").stringValue(),
                    hits.score(x));
        }
        if (log.isDebugEnabled()) {
            log.debug("Hit[" + x + "] Score = " + hits.score(x) + ", Result = " + pr);
        }
        /**
         * matchingField will help the webUI to understand what field was responsible
         * for this match.  Later implementation should use "Explanation" to determine
         * field, for now we will simply grab one term and return it's field.
         */
        try {
            MatchingField match = new MatchingField(query, doc, queryTerms);
            pr.setMatchingField(match.getFieldName());
            pr.setMatchingFieldValue(match.getFieldValue());
            log.info("hit[" + x + "] matchingField is being set to: <" +
                pr.getMatchingField() + "> based on passed in query field.  " +
                "matchingFieldValue = " + pr.getMatchingFieldValue());
        }
        catch (Exception e) {
            log.error("Caught exception: ", e);
        }
        if (pr != null) {
            retval.add(pr);
        }
        if (maxHits > 0 && x == maxHits) {
            break;
        }
    }
    return retval;
}
 
Example 9
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 4 votes vote down vote up
/**
 *
 * @param indexName
 * @param hits
 * @param x
 * @param query
 * @return  true - score is acceptable
 *          false - score is NOT acceptable
 * @throws IOException
 */
private boolean isScoreAcceptable(String indexName, Hits hits, int x, String queryIn)
    throws IOException {
    String guessMainQueryTerm = MatchingField.getFirstFieldName(queryIn);

    if ((indexName.compareTo(BuilderFactory.DOCS_TYPE) == 0) &&
            (!filterDocResults)) {
        return true;
    }
    /**
     * Dropping matches which are a poor fit.
     * system searches are filtered based on "system_score_threshold"
     * other searches will return 10 best matches, then filter anything below
     * "score_threshold"
     */
    if ((indexName.compareTo(BuilderFactory.SERVER_TYPE) == 0) ||
            (indexName.compareTo(BuilderFactory.SERVER_CUSTOM_INFO_TYPE) == 0) ||
            (indexName.compareTo(BuilderFactory.SNAPSHOT_TAG_TYPE)  == 0) ||
            (indexName.compareTo(BuilderFactory.HARDWARE_DEVICE_TYPE) == 0)) {
        if (hits.score(x) < system_score_threshold) {
            if (log.isDebugEnabled()) {
                log.debug("hits.score(" + x + ") is " + hits.score(x));
                log.debug("Filtering out search results from " + x + " to " +
                        hits.length() + ", due to their score being below " +
                        "system_score_threshold = " + system_score_threshold);
            }
            return false;
        }
    }
    else if (indexName.compareTo(BuilderFactory.ERRATA_TYPE) == 0) {
        if (guessMainQueryTerm.compareTo("name") == 0) {
            if (hits.score(x) < errata_advisory_score_threshold) {
                if (log.isDebugEnabled()) {
                    log.debug("hits.score(" + x + ") is " + hits.score(x));
                    log.debug("Filtering out search results from " + x + " to " +
                        hits.length() + ", due to their score being below " +
                        "errata_advisory_score_threshold = " +
                        errata_advisory_score_threshold);
                }
                return false;
            }
        }
        else {
            if (hits.score(x) < errata_score_threshold) {
                if (log.isDebugEnabled()) {
                    log.debug("hits.score(" + x + ") is " + hits.score(x));
                    log.debug("Filtering out search results from " + x + " to " +
                        hits.length() + ", due to their score being below " +
                        "errata_score_threshold = " +
                        errata_score_threshold);
                }
                return false;
            }
        }
    }
    else if (((hits.score(x) < score_threshold) && (x > 10)) ||
            (hits.score(x) < 0.001)) {
        /**
         * Dropping matches which are a poor fit.
         * First term is configurable, it allows matches like spelling errors or
         * suggestions to be possible.
         * Second term is intended to get rid of pure and utter crap hits
         */
        if (log.isDebugEnabled()) {
            log.debug("hits.score(" + x + ") is " + hits.score(x));
            log.debug("Filtering out search results from " + x + " to " +
                    hits.length() + ", due to their score being below " +
                    "score_threshold = " + score_threshold);
        }
        return false;
    }
    return true;
}