lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jon Loken" <>
Subject RE: Wildcard and Literal Searches combined
Date Tue, 24 Jun 2008 13:33:58 GMT
Our approach is:
If the keyword is a single word, then append the star (e.g. replace ->
If the keyword is a phrase containing one of more spaces, then it is
treated as an exact phrase (e.g. replace this -> 'replace this') 


-----Original Message-----
From: mick l [] 
Sent: 24 June 2008 14:22
Subject: RE: Wildcard and Literal Searches combined

Out of interest, how did you go about it in the end?

jloken wrote:
> Hi,
> I posed a similar question on 09 May 08. 
> The response was as below. I did not go down this route however, as a 
> wild carded 'exact' phrase is in a way contradictory.
> Regards
> Jon
> Hi,
> Here's a searchable mailing list archive: 
> As regards the wildcard phrase queries, here's one way I think you 
> could do it, but it's a bit of extra work. If you're using 
> QueryParser, you'd have to override the "getFieldQuery" method to use 
> span queries instead of phrase queries.
> A phrase query can be implemented as a span query with a span or
> factor of 1. So, once you have the PhraseQuery object, you would:
> 1. Extract the terms
> 2. For each one, check if it contains a "*" or a "?"
> 3. If it does, create a WildcardQuery using that term, and re-write it

> using IndexReader.rewrite method. This expands the wildcard query into

> all it's matches.
> 4. Create an array of SpanTermQuery objects, (one SpanTermQuery for 
> each term that matched you wildcard); then add that array to a
> 5. Repeat 2 to 4 for each wildcard term in the phrase.
> 6. Finally (!), create a SpanNearQuery, adding all the original terms 
> in order, but substituting your SpanOrQuerys for the wildcard terms. 
> Use a slop of 1, and set the inOrder flag to true.
> So, essentially, you'd end up with: (you'll have to excuse me if I 
> haven't rendered the span queries correctly as strings here - but this

> should give the general idea...)
> spanNear[boiler (spanOr[replacement replacing])]
> So it will accept *either* "replacement" or "replacing" adjacent to 
> "boiler", which is what you want.
> As you can see, it's a bit of work - but if you add this functionality

> to the QueryParser, you'll can re-use it a lot!
> Hope that helps!
> -JB
> -----Original Message-----
> From: mick l []
> Sent: 24 June 2008 13:29
> To:
> Subject: Wildcard and Literal Searches combined
> Folks,
> My users require wildcard searches. Sometimes their search phrases 
> contain spaces. I am having trouble trying to implement a wildcard 
> search on strings containing spaces, so if the term includes spaces I 
> force a literal search by adding double quotes to the search term.
> So the search string for 'Dublin' becomes search term (Dublin*) 
> whereas search string 'Dublin City' becomes ("Dublin City")
> If I use (Dublin City*) I get all instances of Dublin OR City in the 
> results which is not what I am looking for.
> Is there any way I can combine the wildcard search and the literal?
> Heres my existing code. Its in c# with Lucene.Net
> //if input has spaces we do a literal search if
> ") < 0) { sSearchQuery = "(" + sSearchQuery + "*)"; } else { 
> sSearchQuery = "(\"" + sSearchQuery + "\")"; } IndexSearcher searcher 
> = new IndexSearcher(sIndexLocation); Hits oHitColl = 
> searcher.Search(oParser.Parse(sSearchQuery));
> Thanks folks
> --
> View this message in context:
> 0p
> 18089950.html
> Sent from the Lucene - Java Users mailing list archive at

BiP Solutions Limited is a company registered in Scotland with Company Number SC086146 and
VAT number 383030966 and having its registered office at Park House, 300 Glasgow Road, Shawfield,
Glasgow, G73 1SQ ****************************************************************************
This e-mail (and any attachment) is intended only for the attention of the addressee(s). Its
unauthorised use, disclosure, storage or copying is not permitted. If you are not the intended
recipient, please destroyall copies and inform the sender by return e-mail.
This e-mail (whether you are the sender or the recipient) may be monitored, recorded and retained
by BiP Solutions Ltd.
E-mail monitoring/ blocking software may be used, and e-mail content may be read at any time.
You have a responsibility to ensure laws are not broken when composing or forwarding e-mails
and their contents.

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

View raw message