lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doug Cutting <>
Subject Re: "starts with" query functionality
Date Tue, 06 Apr 2004 16:47:28 GMT
Chad Small wrote:
> We have a requirement to return documents with a "title" field that starts with a certain
letter.  Is there a way to do something like this?  We're using the StandardAnalyzer
> Example title fields:
> This is the title of a document.
> And this is a title of a different document.
> This query doesn't fulfill the requirement:
> +(t*)  - just want to return the 1st document that starts with "This", and not the 2nd
article that has "this" as the 2nd word.

You could do this with SpanFirstQuery, but it would be a bit of code.

You'd want to perform the expansion of 't*', but instead of making a 
BooleanQuery with a TermQuery for each matching term, you'd need to fill 
the BooleanQuery with a SpanFirstQuery for each term, each containing a 

In your example, 't*' expands to something like:

   Query result = new BooleanQuery()
   result.add(new TermQuery(new Term("f", "this")));
   result.add(new TermQuery(new Term("f", "the")));

To construct a similar query that only matches the first word, you'd 
need something like:

   Query result = new BooleanQuery()
   result.add(new SpanFirstQuery(new SpanTermQuery(new 
Term("f","this")), 1);
   result.add(new SpanFirstQuery(new SpanTermQuery(new Term("f","the")), 1);

In order to do the term expansion you'd need something like loop in 


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message