[ 
https://issues.apache.org/jira/browse/LUCENE-4541?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13491262#comment-13491262
 ] 

David Smiley commented on LUCENE-4541:
--------------------------------------

{code:java}
  public class ValueSourceAccessor {
    // implement FunctionValues ?
    private final List<AtomicReaderContext> readerContexts;
    private final FunctionValues[] docValuesArr;
    private final ValueSource valueSource;
    private final Map fContext;

    private int localId;
    private FunctionValues values;

    public ValueSourceAccessor(IndexSearcher searcher, ValueSource valueSource) 
{
      readerContexts = searcher.getIndexReader().leaves();
      this.valueSource = valueSource;
      docValuesArr = new FunctionValues[readerContexts.size()];
      fContext = ValueSource.newContext(searcher);
    }

    private void setState(int docid) throws IOException {
      int idx = ReaderUtil.subIndex(docid, readerContexts);
      AtomicReaderContext rcontext = readerContexts.get(idx);
      values = docValuesArr[idx];
      if (values == null) {
        docValuesArr[idx] = values = valueSource.getValues(fContext, rcontext);
      }
      localId = docid - rcontext.docBase;
    }

    public double doubleVal(int docid) throws IOException {
      setState(docid);
      return values.doubleVal(localId);
    }

    public Object objectVal(int docid) throws IOException {
      setState(docid);
      return values.objectVal(localId);
    }
    
    //...
  }
{code}
                
> Easier way to access ValueSource given docid
> --------------------------------------------
>
>                 Key: LUCENE-4541
>                 URL: https://issues.apache.org/jira/browse/LUCENE-4541
>             Project: Lucene - Core
>          Issue Type: New Feature
>          Components: modules/other
>            Reporter: David Smiley
>            Priority: Minor
>
> I was working on improving some sample code and I needed to access a value 
> via a ValueSource given a docid in search results.  I was disappointed to see 
> how many steps were needed.  Incidentally, Solr had to solve the same 
> problem, and it's got ValueSourceAugmenter for this.  I propose a small 
> utility class be created to do something similar, such that the client can 
> merely create an instance and then call a method with a doc id to retrieve 
> the value.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to