lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jack Krupansky" <j...@basetechnology.com>
Subject Re: Query type always Boolean Query even if * and ? are present.
Date Thu, 12 Sep 2013 16:10:22 GMT
Queries have a syntax, with terms and operators. If no operators, there is 
an implicit operator (AND/OR = mandatory/optional). Generally a sequence of 
terms will always generate a BooleanQuery.

So, any time you use some lexical construct that has meaning to the query 
parser that you don't want, then you need to escape it.

-- Jack Krupansky

-----Original Message----- 
From: Ankit Murarka
Sent: Thursday, September 12, 2013 11:36 AM
To: java-user@lucene.apache.org
Subject: Re: Query type always Boolean Query even if * and ? are present.

Bingo....This has solved my case... Thanks a ton..!!

Does this mean any input containing spaces and query being parsed using
QueryParser will result in Query type as Boolean Query UNLESS white
space is escaped..

So if the input contains white space and parsed using QueryParser it
will eventually turn into Boolean since for each separate word it will
create a term and each term will be treated for Boolean Query.. ??

Can you please point any other possible pitfall when using QueryParser
and input containing special characters...

On 9/12/2013 8:59 PM, Jack Krupansky wrote:
> You're not escaping white space, so your input will be a sequence of 
> terms, which should generate a BooleanQuery. What is the last clause of 
> the BQ? It should be your PrefixQuery.
>
> -- Jack Krupansky
>
> -----Original Message----- From: Ankit Murarka
> Sent: Thursday, September 12, 2013 11:25 AM
> To: java-user@lucene.apache.org
> Subject: Re: Query type always Boolean Query even if * and ? are present.
>
> If I remove the escape call from the function, then it works as
> expected.. Prefix/Boolean/Wildcard..
>
> But this is NOT what I want... The escape should be present else I will
> get lexical error in case of Prefix/Boolean/Wildcard since my input will
> definitely contain special characters...
>
> Help needed..
>
> TIA..
>
> On 9/12/2013 8:52 PM, Ankit Murarka wrote:
>> I also tried it with this query:
>>
>> <param name="user_name" value="USER_NAME_MENTIONED"/>*
>>
>> I am still getting it as Boolean Query.. It should be Prefix...
>>
>> On 9/12/2013 8:50 PM, Jack Krupansky wrote:
>>> The trailing asterisk in your query input is escaped with a backslash, 
>>> so the query parser will not treat it as a wildcard.
>>>
>>> -- Jack Krupansky
>>>
>>> -----Original Message----- From: Ankit Murarka
>>> Sent: Thursday, September 12, 2013 10:19 AM
>>> To: java-user@lucene.apache.org
>>> Subject: Query type always Boolean Query even if * and ? are present.
>>>
>>> Hello.
>>>
>>> I am faced with a trivial issue: Everytime my Query is being fired as a
>>> Boolean Query...
>>>
>>> Providing Input : <param name="user_name" 
>>> value="USER_NAME_MENTIONED"/>\*
>>>
>>> This input is provided. Since this contains special characters I use
>>> escape method of QueryParser (removed escaping for * and ? since they
>>> are needed for WildCard and Prefix Searches.)
>>>
>>> public static String escape(String s) {
>>>
>>>         System.out.println("CALLED");
>>>         StringBuilder sb = new StringBuilder();
>>>
>>>         try
>>>         {
>>>         for (int i = 0; i < s.length(); i++) {
>>>           char c = s.charAt(i);
>>>           // These characters are part of the query syntax and must be
>>> escaped
>>>           if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '('
>>> || c == ')' || c == ':'
>>>             || c == '^' || c == '[' || c == ']' || c == '\"' || c ==
>>> '{' || c == '}' || c == '~'
>>>             || c == '|' || c == '&' || c == '/') {
>>>             sb.append('\\');
>>>           }
>>>           sb.append(c);
>>>         }
>>>         }catch(Exception e)
>>>         {
>>>             e.printStackTrace();
>>>         }
>>>         return sb.toString();
>>>       }
>>>
>>> /*The function which is used to provide the HIT:*/
>>>
>>>             Directory directory=null;;
>>>             IndexReader reader=null;
>>>             IndexSearcher searcher=null;
>>>             Analyzer analyzer=null;
>>>             try
>>>             {
>>>                 Analyzer analyzer = new
>>> StandardAnalyzer(Version.LUCENE_44);
>>>
>>>                 directory = FSDirectory.open(new
>>> File(strMainIndexPath.replace("\\", "/")));
>>>                 reader = DirectoryReader.open(directory);  //location
>>> where indexes are.
>>>                 searcher = new IndexSearcher(reader);
>>>
>>>                 System.out.println("Searching for '" + searchString +
>>> "' using QueryParser");
>>>                 QueryParser queryParser = new
>>> QueryParser(Version.LUCENE_44,"contents",analyzer);
>>>
>>>                 searchString=CommonMethods.escape(searchString);
>>> //MENTIONED ABOVE
>>>
>>>                 System.out.println("ESCAPE STRING AFTER THE ESCAPE
>>> FUNCTION OF QUERYPARSER >>>      "  +   searchString);
>>>
>>>                 Query query = queryParser.parse(searchString);
>>>
>>>                 System.out.println("Type of query: "
>>> +query.getClass().getSimpleName());   //GETTING IT AS BOOLEAN ALWAYS..
>>>
>>> The last output is always BOOLEAN Query... Even if I am appending * at
>>> the end, the query is always Boolean...
>>> I have to use QueryParser ONLY....
>>> Absolutely no manipulation is done on string from being given as in
>>> input to the string which is provided to this search function.
>>>
>>> Kindly guide..
>>>
>>> TIA.
>>>
>>
>>
>
>


-- 
Regards

Ankit Murarka

"What lies behind us and what lies before us are tiny matters compared with 
what lies within us"


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


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


Mime
View raw message