You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Mufaddal Khumri <MK...@allegromedical.com> on 2005/04/07 08:21:59 UTC

Escaping special characters

Hi,

 

Am new to Lucene. I found the following page:
http://lucene.apache.org/java/docs/queryparsersyntax.html. At the bottom
of the page there is a section that in order to escape special
characters one would use "\".

 

I have an Indexer that indexes product names. Some product names have
"-" character in them. When I use my search class to search for product
names with - in them it wont find those products.

 

The code below returns no result.

 

public void searchIndex()

{

            try

            {

              SearchManager sm = new
ManagerFactory().getSearchManager();

              

              String searchPath = "Product-search-index";

              sm.initIndex(searchPath);

              sm.buildIndex(this.daoFactory);

              

              String tk = "Chimi-Bose-765";

              if(tk.contains("-"));

                  tk = tk.replaceAll("-" , "\\-");

                  

              if(tk != null && tk.trim().equals("") == false)

                  tk = tk + "*";

              else

                  tk = "";

 

              try

              {

                  Hits hits = sm.search(searchPath, tk);

                    

                  if(hits != null && hits.length() > 0)

                  {

                      int length = hits.length();

                      log.info(length + " result(s) were found matching
your search criteria.");

                      for(int i = 0; i < length; i++)

                      {

                          Document doc = hits.doc(i);

                          log.info("Score: " + hits.score(i) + " Product
id = " + doc.getField("id").stringValue() + " prod name = " +
doc.getField("name").stringValue());

                      }

                  }

                  else

                      log.info("No results were found matching your
search.");

              }

              catch(IOException ex)

              {

                  ex.printStackTrace();

              }

              catch(Exception ex)

              {

                  log.info("No results were found matching your
search.");

              }

            }

            catch (Exception ex)

            {

                  ex.printStackTrace();

                  Assert.fail();

            }

}

 


------------------------------------------------------------------------------------------
This email and any files transmitted with it are confidential 
and intended solely for the use of the individual or entity 
to whom they are addressed. If you have received this 
email in error please notify the system manager. Please
note that any views or opinions presented in this email 
are solely those of the author and do not necessarily
represent those of the company. Finally, the recipient
should check this email and any attachments for the 
presence of viruses. The company accepts no liability for
any damage caused by any virus transmitted by this email.
Consult your physician prior to the use of any medical
supplies or product.
------------------------------------------------------------------------------------------


Re: Escaping special characters

Posted by Andy Roberts <ma...@andy-roberts.net>.
On Thursday 07 Apr 2005 06:38, Chuck Williams wrote:
> Mufaddal Khumri writes (4/6/2005 11:21 PM):
> >Hi,
> >
> >Am new to Lucene. I found the following page:
> >http://lucene.apache.org/java/docs/queryparsersyntax.html. At the bottom
> >of the page there is a section that in order to escape special
> >characters one would use "\".
> >
> >
> >
> >I have an Indexer that indexes product names. Some product names have
> >"-" character in them. When I use my search class to search for product
> >names with - in them it wont find those products.
>
> How did you index those product names?  I.e., if you used a tokenized
> field for the product names and an analyzer that breaks on the hyphens,
> then there are no hyphenated tokens for you to match.  I would suggest
> using Luke to browse your index and see what you have.
>
> Chuck

The Lucene In Action book has an excellent chapter on Analysers - well worth a 
read. Of particular interest is some code that allows you to see how a given 
Analyser tokenises an input string.

You can download the source code from the book 
(http://www.lucenebook.com/LuceneInAction.zip). If you unzip this file you 
will find a directory called "LuceneInAction/src/lia/analysis" and in there 
is a class called AnalyzerDemo (which depends in AnalyzerUtils). Compile this 
and run to see how the Analysers work. Put in your hyphenated strings to see 
how they work too.

HTH
Andy

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Escaping special characters

Posted by Chuck Williams <ch...@allthingslocal.com>.
Mufaddal Khumri writes (4/6/2005 11:21 PM):

>Hi,
>
> 
>
>Am new to Lucene. I found the following page:
>http://lucene.apache.org/java/docs/queryparsersyntax.html. At the bottom
>of the page there is a section that in order to escape special
>characters one would use "\".
>
> 
>
>I have an Indexer that indexes product names. Some product names have
>"-" character in them. When I use my search class to search for product
>names with - in them it wont find those products.
>  
>
How did you index those product names?  I.e., if you used a tokenized 
field for the product names and an analyzer that breaks on the hyphens, 
then there are no hyphenated tokens for you to match.  I would suggest 
using Luke to browse your index and see what you have.

Chuck


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org