Java Code Examples for org.apache.lucene.queries.CustomScoreProvider

The following examples show how to use org.apache.lucene.queries.CustomScoreProvider. These examples are extracted from open source projects. 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 Project: solr-custom-score   Source File: MyQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CustomScoreProvider getCustomScoreProvider(LeafReaderContext context) throws IOException {
    //此处返回,定义的Provider
    MyProvider provider=new MyProvider(context);
    provider.setParams(params);
    return provider;
}
 
Example 2
Source Project: linden   Source File: LindenScoreQuery.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CustomScoreProvider getCustomScoreProvider(AtomicReaderContext context) throws IOException {
  try {
    return new LindenScoreProvider(context);
  } catch (Exception e) {
    throw new IOException(Throwables.getStackTraceAsString(e));
  }
}
 
Example 3
Source Project: solr-vector-scoring   Source File: VectorScoreQuery.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected CustomScoreProvider getCustomScoreProvider(LeafReaderContext context) throws IOException {
	return new CustomScoreProvider(context){
		@Override
		public float customScore(int docID, float subQueryScore, float valSrcScore) throws IOException {
			float score = 0;
			double docVectorNorm = 0;
			LeafReader reader = context.reader();
			Terms terms = reader.getTermVector(docID, field);
			if(vector.size() != terms.size()){
				throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "indexed and input vector array must have same length");
			}
			TermsEnum iter = terms.iterator();
		    BytesRef text;
		    while ((text = iter.next()) != null) {
		    	String term = text.utf8ToString();
		    	float payloadValue = 0f;
		    	PostingsEnum postings = iter.postings(null, PostingsEnum.ALL);
		    	while (postings.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
		    		int freq = postings.freq();
		    		while (freq-- > 0) postings.nextPosition();

		    		BytesRef payload = postings.getPayload();
		    		payloadValue = PayloadHelper.decodeFloat(payload.bytes, payload.offset); 
		    		
		    		if (cosine)
		              docVectorNorm += Math.pow(payloadValue, 2.0);
		    	}
		    		
		    	score = (float)(score + payloadValue * (vector.get(Integer.parseInt(term))));
		    }
		    
		    if (cosine) {
		      if ((docVectorNorm == 0) || (queryVectorNorm == 0)) return 0f;
		      return (float)(score / (Math.sqrt(docVectorNorm) * Math.sqrt(queryVectorNorm)));
		    }

			return score;
		}
	};
}
 
Example 4
Source Project: mtas   Source File: MtasScoreQuery.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public CustomScoreProvider getCustomScoreProvider(
    final LeafReaderContext context) {
  return new MtasScoreProvider(context);
}
 
Example 5
Source Project: lucene-query-example   Source File: CountingQuery.java    License: Apache License 2.0 4 votes vote down vote up
protected CustomScoreProvider getCustomScoreProvider(
		LeafReaderContext context) throws IOException {
	return new CountingQueryScoreProvider("tag", context);
}
 
Example 6
protected CustomScoreProvider getCustomScoreProvider(LeafReaderContext context) throws IOException {
    return new BackwardsScoreProvider(context);
}