org.apache.lucene.queries.CustomScoreProvider Java Examples

The following examples show how to use org.apache.lucene.queries.CustomScoreProvider. 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: MyQuery.java    From solr-custom-score with 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 File: LindenScoreQuery.java    From linden with 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 File: VectorScoreQuery.java    From solr-vector-scoring with 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 File: MtasScoreQuery.java    From mtas with Apache License 2.0 4 votes vote down vote up
@Override
public CustomScoreProvider getCustomScoreProvider(
    final LeafReaderContext context) {
  return new MtasScoreProvider(context);
}
 
Example #5
Source File: CountingQuery.java    From lucene-query-example with Apache License 2.0 4 votes vote down vote up
protected CustomScoreProvider getCustomScoreProvider(
		LeafReaderContext context) throws IOException {
	return new CountingQueryScoreProvider("tag", context);
}
 
Example #6
Source File: BackwardsTermCustomQuery.java    From lucene-query-example with Apache License 2.0 4 votes vote down vote up
protected CustomScoreProvider getCustomScoreProvider(LeafReaderContext context) throws IOException {
    return new BackwardsScoreProvider(context);
}