On Oct 13, 2010, at 11:37 AM, Sykes, Derek wrote: > Hi there, > > I'm currently trying to work out how I can determine the type > (string/number/date/etc)of a term. I've not seen any off the shelf way to do > it so am trying to store a payload against each term that records the type. > > I'm having a little trouble retrieving a payload I'd stored onto the term. > At the moment I'm using the TypeAsPayloadTokenFilter, however I'll change > this soon as it's not really what I'm after. > > I'm then getting a TermEnum from either reader.terms() or using a > PrefixTermEnum. > > For each term I want the payload from I'm running this function: > > private String getPayload(Term term) { > String payload = null; > try { > TermPositions termPositions = reader.termPositions(term); > termPositions.next();
next() returns a boolean as to whether there is a valid entry, so you need to check that. You may not actually have a match for that term. > if (termPositions.isPayloadAvailable()) { > byte[] payloadBytes = new > byte[termPositions.getPayloadLength()]; > payloadBytes = termPositions.getPayload(payloadBytes, 0); > payload = new String(payloadBytes); > LOG.debug(payload); > } > } > catch( IOException e) { > ... What does your Analysis process look like? Many of Lucene's analysis pieces don't bother setting type. Have you looked at the index with Luke? That should show you the payloads. Also, have a look at the SpanTermQuery. You can use the Spans object to step directly through the position matches and get the payloads, if they are available. -------------------------- Grant Ingersoll http://www.lucidimagination.com --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org