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 Carsten Schnober <sc...@ids-mannheim.de> on 2012/08/14 10:58:46 UTC

UnsupportedOperationException: Query should have been rewritten

Dear list,
I am trying to combine a WildcardQuery and a SpanQuery because I need to
extract spans from the index for further processing. I realise that
there have been a few public discussions about this topic around, but I
still fail to get what I am missing here. My code is this (Lucene 3.6.0):

======================================================================
WildcardQuery wildcard = new WildcardQuery(new Term("field", "bro*"));
SpanQuery query = new SpanMultiTermQueryWrapper<WildcardQuery>(wildcard);		
// query = query.rewrite(reader);		
Spans luceneSpans = query.getSpans(reader);
======================================================================

This throws the following exception:
======================================================================
Exception in thread "main" java.lang.UnsupportedOperationException:
Query should have been rewritten at
org.apache.lucene.search.spans.SpanMultiTermQueryWrapper.getSpans(SpanMultiTermQueryWrapper.java:114)
======================================================================

I am basically aware of the problem that I cannot apply a MultiTermQuery
instance (like a WildcardQuery) without calling rewrite(), but on the
other hand, rewrite() returns a Query object that I cannot use as a
SpanQuery instance.

I'm almost sure that there is a reasonable solution for this problem
that I am not able to spot. Or do I have to migrate either to Lucene 4
or use a SpanRegexQuery instead which I do not really want to because it
is marked as deprecated.

Thank you very much!
Carsten

-- 
Institut für Deutsche Sprache | http://www.ids-mannheim.de
Projekt KorAP                 | http://korap.ids-mannheim.de
Tel. +49-(0)621-43740789      | schnober@ids-mannheim.de
Korpusanalyseplattform der nächsten Generation
Next Generation Corpus Analysis Platform

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


RE: UnsupportedOperationException: Query should have been rewritten

Posted by Uwe Schindler <uw...@thetaphi.de>.
Yes, cast is safe.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Carsten Schnober [mailto:schnober@ids-mannheim.de]
> Sent: Tuesday, August 14, 2012 11:07 AM
> To: java-user@lucene.apache.org
> Subject: Re: UnsupportedOperationException: Query should have been
> rewritten
> 
> Am 14.08.2012 11:00, schrieb Uwe Schindler:
> > You have to rewrite the wrapper query.
> 
> Thanks, Uwe! I had tried that way but it failed because the rewrite()
method
> would return a Query (not a SpanQuery) object. A cast seems to solve the
> problem, I'm re-posting the code snippet to the list for the sake of
> completeness:
> 
> ================================================================
> WildcardQuery wildcard = new WildcardQuery(new Term("field", "bro*"));
> SpanQuery query = (SpanQuery) new
> SpanMultiTermQueryWrapper<WildcardQuery>(wildcard).rewrite(reader);
> 
> Spans spans = query.getSpans(reader);
> ================================================================
> 
> All I am still wondering about is whether this cast is totally safe, i.e.
robust to
> all kinds of variable search terms.
> 
> Best,
> Carsten
> 
> --
> Institut für Deutsche Sprache | http://www.ids-mannheim.de
> Projekt KorAP                 | http://korap.ids-mannheim.de
> Tel. +49-(0)621-43740789      | schnober@ids-mannheim.de
> Korpusanalyseplattform der nächsten Generation Next Generation Corpus
> Analysis Platform
> 
> ---------------------------------------------------------------------
> 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


Re: UnsupportedOperationException: Query should have been rewritten

Posted by Carsten Schnober <sc...@ids-mannheim.de>.
Am 14.08.2012 11:00, schrieb Uwe Schindler:
> You have to rewrite the wrapper query.

Thanks, Uwe! I had tried that way but it failed because the rewrite()
method would return a Query (not a SpanQuery) object. A cast seems to
solve the problem, I'm re-posting the code snippet to the list for the
sake of completeness:

================================================================
WildcardQuery wildcard = new WildcardQuery(new Term("field", "bro*"));
SpanQuery query = (SpanQuery) new
SpanMultiTermQueryWrapper<WildcardQuery>(wildcard).rewrite(reader);		
Spans spans = query.getSpans(reader);
================================================================

All I am still wondering about is whether this cast is totally safe,
i.e. robust to all kinds of variable search terms.

Best,
Carsten

-- 
Institut für Deutsche Sprache | http://www.ids-mannheim.de
Projekt KorAP                 | http://korap.ids-mannheim.de
Tel. +49-(0)621-43740789      | schnober@ids-mannheim.de
Korpusanalyseplattform der nächsten Generation
Next Generation Corpus Analysis Platform

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


Re: UnsupportedOperationException: Query should have been rewritten

Posted by Jack Krupansky <ja...@basetechnology.com>.
Uwe, if I look at the TestSpanMultiTermQueryWrapper.testPrefix test in 3.6, 
it doesn't rewrite the SMTQW, but works. What's the difference? Is that test 
wrong/broken?

public void testPrefix() throws Exception {
  WildcardQuery wq = new WildcardQuery(new Term("field", "extrem*"));
  SpanQuery swq = new SpanMultiTermQueryWrapper<WildcardQuery>(wq);
  // will only match "jumps over extremely very lazy broxn dog"
  SpanFirstQuery sfq = new SpanFirstQuery(swq, 3);
  assertEquals(1, searcher.search(sfq, 10).totalHits);
}

Or, is the issue simply a peculiarity of getSpans?

-- Jack Krupansky

-----Original Message----- 
From: Uwe Schindler
Sent: Tuesday, August 14, 2012 5:00 AM
To: java-user@lucene.apache.org
Subject: RE: UnsupportedOperationException: Query should have been rewritten

You have to rewrite the wrapper query.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Carsten Schnober [mailto:schnober@ids-mannheim.de]
> Sent: Tuesday, August 14, 2012 10:59 AM
> To: java-user
> Subject: UnsupportedOperationException: Query should have been rewritten
>
> Dear list,
> I am trying to combine a WildcardQuery and a SpanQuery because I need to
> extract spans from the index for further processing. I realise that there
have
> been a few public discussions about this topic around, but I still fail to
get what I
> am missing here. My code is this (Lucene 3.6.0):
>
> ================================================================
> ======
> WildcardQuery wildcard = new WildcardQuery(new Term("field", "bro*"));
> SpanQuery query = new
> SpanMultiTermQueryWrapper<WildcardQuery>(wildcard);
> // query = query.rewrite(reader);
> Spans luceneSpans = query.getSpans(reader);
> ================================================================
> ======
>
> This throws the following exception:
> ================================================================
> ======
> Exception in thread "main" java.lang.UnsupportedOperationException:
> Query should have been rewritten at
> org.apache.lucene.search.spans.SpanMultiTermQueryWrapper.getSpans(Span
> MultiTermQueryWrapper.java:114)
> ================================================================
> ======
>
> I am basically aware of the problem that I cannot apply a MultiTermQuery
> instance (like a WildcardQuery) without calling rewrite(), but on the
other
> hand, rewrite() returns a Query object that I cannot use as a SpanQuery
> instance.
>
> I'm almost sure that there is a reasonable solution for this problem that
I am
> not able to spot. Or do I have to migrate either to Lucene 4 or use a
> SpanRegexQuery instead which I do not really want to because it is marked
as
> deprecated.
>
> Thank you very much!
> Carsten
>
> --
> Institut für Deutsche Sprache | http://www.ids-mannheim.de
> Projekt KorAP                 | http://korap.ids-mannheim.de
> Tel. +49-(0)621-43740789      | schnober@ids-mannheim.de
> Korpusanalyseplattform der nächsten Generation Next Generation Corpus
> Analysis Platform
>
> ---------------------------------------------------------------------
> 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 


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


RE: UnsupportedOperationException: Query should have been rewritten

Posted by Uwe Schindler <uw...@thetaphi.de>.
You have to rewrite the wrapper query.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de


> -----Original Message-----
> From: Carsten Schnober [mailto:schnober@ids-mannheim.de]
> Sent: Tuesday, August 14, 2012 10:59 AM
> To: java-user
> Subject: UnsupportedOperationException: Query should have been rewritten
> 
> Dear list,
> I am trying to combine a WildcardQuery and a SpanQuery because I need to
> extract spans from the index for further processing. I realise that there
have
> been a few public discussions about this topic around, but I still fail to
get what I
> am missing here. My code is this (Lucene 3.6.0):
> 
> ================================================================
> ======
> WildcardQuery wildcard = new WildcardQuery(new Term("field", "bro*"));
> SpanQuery query = new
> SpanMultiTermQueryWrapper<WildcardQuery>(wildcard);
> // query = query.rewrite(reader);
> Spans luceneSpans = query.getSpans(reader);
> ================================================================
> ======
> 
> This throws the following exception:
> ================================================================
> ======
> Exception in thread "main" java.lang.UnsupportedOperationException:
> Query should have been rewritten at
> org.apache.lucene.search.spans.SpanMultiTermQueryWrapper.getSpans(Span
> MultiTermQueryWrapper.java:114)
> ================================================================
> ======
> 
> I am basically aware of the problem that I cannot apply a MultiTermQuery
> instance (like a WildcardQuery) without calling rewrite(), but on the
other
> hand, rewrite() returns a Query object that I cannot use as a SpanQuery
> instance.
> 
> I'm almost sure that there is a reasonable solution for this problem that
I am
> not able to spot. Or do I have to migrate either to Lucene 4 or use a
> SpanRegexQuery instead which I do not really want to because it is marked
as
> deprecated.
> 
> Thank you very much!
> Carsten
> 
> --
> Institut für Deutsche Sprache | http://www.ids-mannheim.de
> Projekt KorAP                 | http://korap.ids-mannheim.de
> Tel. +49-(0)621-43740789      | schnober@ids-mannheim.de
> Korpusanalyseplattform der nächsten Generation Next Generation Corpus
> Analysis Platform
> 
> ---------------------------------------------------------------------
> 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