List of usage examples for org.apache.lucene.search.spans SpanOrQuery SpanOrQuery
public SpanOrQuery(SpanQuery... clauses)
From source file:aos.lucene.search.advanced.SpanQueryTest.java
License:Apache License
public void testSpanOrQuery() throws Exception { SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[] { quick, fox }, 1, true); SpanNearQuery lazy_dog = new SpanNearQuery(new SpanQuery[] { lazy, dog }, 0, true); SpanNearQuery sleepy_cat = new SpanNearQuery(new SpanQuery[] { sleepy, cat }, 0, true); SpanNearQuery qf_near_ld = new SpanNearQuery(new SpanQuery[] { quick_fox, lazy_dog }, 3, true); assertOnlyBrownFox(qf_near_ld);/* w w w.j a va 2 s . c o m*/ dumpSpans(qf_near_ld); SpanNearQuery qf_near_sc = new SpanNearQuery(new SpanQuery[] { quick_fox, sleepy_cat }, 3, true); dumpSpans(qf_near_sc); SpanOrQuery or = new SpanOrQuery(new SpanQuery[] { qf_near_ld, qf_near_sc }); assertBothFoxes(or); dumpSpans(or); }
From source file:aos.lucene.search.advanced.SpanQueryTest.java
License:Apache License
public void testPlay() throws Exception { SpanOrQuery or = new SpanOrQuery(new SpanQuery[] { quick, fox }); dumpSpans(or);//from w w w . j a v a 2s . co m SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[] { quick, fox }, 1, true); SpanFirstQuery sfq = new SpanFirstQuery(quick_fox, 4); dumpSpans(sfq); dumpSpans(new SpanTermQuery(new Term("f", "the"))); SpanNearQuery quick_brown = new SpanNearQuery(new SpanQuery[] { quick, brown }, 0, false); dumpSpans(quick_brown); }
From source file:com.leavesfly.lia.advsearching.SpanQueryTest.java
License:Apache License
public void testSpanOrQuery() throws Exception { SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[] { quick, fox }, 1, true); SpanNearQuery lazy_dog = new SpanNearQuery(new SpanQuery[] { lazy, dog }, 0, true); SpanNearQuery sleepy_cat = new SpanNearQuery(new SpanQuery[] { sleepy, cat }, 0, true); SpanNearQuery qf_near_ld = new SpanNearQuery(new SpanQuery[] { quick_fox, lazy_dog }, 3, true); assertOnlyBrownFox(qf_near_ld);//from w ww .j a v a2s. co m dumpSpans(qf_near_ld); SpanNearQuery qf_near_sc = new SpanNearQuery(new SpanQuery[] { quick_fox, sleepy_cat }, 3, true); dumpSpans(qf_near_sc); SpanOrQuery or = new SpanOrQuery(new SpanQuery[] { qf_near_ld, qf_near_sc }); assertBothFoxes(or); dumpSpans(or); }
From source file:com.leavesfly.lia.advsearching.SpanQueryTest.java
License:Apache License
public void testPlay() throws Exception { SpanOrQuery or = new SpanOrQuery(new SpanQuery[] { quick, fox }); dumpSpans(or);/* w ww . j a v a2s.c o m*/ SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[] { quick, fox }, 1, true); SpanFirstQuery sfq = new SpanFirstQuery(quick_fox, 4); dumpSpans(sfq); dumpSpans(new SpanTermQuery(new Term("f", "the"))); SpanNearQuery quick_brown = new SpanNearQuery(new SpanQuery[] { quick, brown }, 0, false); dumpSpans(quick_brown); }
From source file:com.mhs.qsol.proximity.distribute.BasicDistributable.java
License:Apache License
public Query distribute(Distributable distrib, ProxInfo proxInfo) { BooleanQuery boolQuery = new BooleanQuery(); List<Distributable> children = distrib.getChildren(); if (children == null) { Query query = distrib.distribute(this.query, proxInfo); if (logger.isLoggable(Level.FINE)) { logger.fine("return query:" + query); }/*from ww w. j a v a 2s . c o m*/ return query; } Query query; Query cacheQuery2 = null; int size = children.size(); List<Occur> connectors = distrib.getConnectors(); for (int i = 0; i < size; i++) { Occur con = connectors.get(i); // if we have already computed query2 looking for a possible SpanOr // use if (cacheQuery2 != null) { query = cacheQuery2; } else { query = children.get(i).distribute(this.query, proxInfo); } // must make sure both clauses are spans and connector is | if you // want to optimize to SpanOr if (children.size() > (i + 1) && (con == Occur.SHOULD) && query instanceof SpanQuery) { cacheQuery2 = children.get(i + 1).distribute(this.query, proxInfo); if (cacheQuery2 instanceof SpanQuery) { if (children.size() == 2) { return new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 }); } else { query = new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 }); cacheQuery2 = null; i++; } } else { cacheQuery2 = null; } } boolQuery.add(query, con); } if (logger.isLoggable(Level.FINE)) { logger.fine("distribute(Distributable) - to distrib:" + distrib + " and :" + this.query); logger.fine("boolquery:" + boolQuery.toString()); } return boolQuery; }
From source file:com.mhs.qsol.proximity.distribute.GroupDistributable.java
License:Apache License
public Query distribute(Distributable distrib, ProxInfo proxInfo) { if (distrib == null) { throw new IllegalArgumentException("distrib is null"); }/* w w w .j a v a 2s. c om*/ // compacts distrib. List<Distributable> children = distrib.getChildren(); if ((children != null) && (children.size() == 1)) { distrib = children.get(0); distrib.setParent(null); } // compacts distribs. if (distribs.size() == 1) { if (logger.isLoggable(Level.FINE)) { logger.fine("compacting distribs"); } Distributable returnDistrib = distribs.get(0); returnDistrib.setParent(null); if (logger.isLoggable(Level.FINE)) { logger.fine("compacting distribs:" + returnDistrib); logger.fine("other distrib:" + "sec" + distrib.toString()); } return returnDistrib.distribute(distrib, proxInfo); } BooleanQuery boolQuery = new BooleanQuery(); Query query; Query cacheQuery2 = null; for (int i = 0; i < distribs.size(); i++) { Occur con = connector.get(i); // if we have already computed query2 looking for a possible SpanOr use if (cacheQuery2 != null) { query = cacheQuery2; } else { if (logger.isLoggable(Level.FINE)) { logger.fine("distrib: " + distribs.get(i) + " WITH " + distrib); } query = distribs.get(i).distribute(distrib, proxInfo); if (logger.isLoggable(Level.FINE)) { logger.fine("thequery: " + query); } // logger.fine("query: " + query); } // must make sure both clauses are spans and connector is | if you want to // optimize to SpanOr if (!((i + 1) == distribs.size()) && (con == Occur.SHOULD) && query instanceof SpanQuery) { cacheQuery2 = distribs.get(i + 1).distribute(distrib, proxInfo); if (cacheQuery2 instanceof SpanQuery) { // if just a single 'or' set in this group if (distribs.size() == 2) { return new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 }); } else { query = new SpanOrQuery(new SpanQuery[] { (SpanQuery) query, (SpanQuery) cacheQuery2 }); cacheQuery2 = null; i++; } } else { cacheQuery2 = null; } } // TODO: update this if (logger.isLoggable(Level.FINE)) { logger.fine("distribute(Distributable) - adding connector:" + con); } if (logger.isLoggable(Level.FINE)) { logger.fine("distribute(Distributable) - to distrib:" + distribs + " and :" + distrib); logger.fine("query:" + query); } boolQuery.add(query, con); } return boolQuery; }
From source file:com.mhs.qsol.proximity.ProximityVisitor.java
License:Apache License
/** * Converts a token, as defined in the qsol.jtb JavaCC file, into an * appropriate query./*from ww w . java2s .c om*/ * * @param token * @return */ protected Query tokenToQuery(String token) { if (logger.isLoggable(Level.FINE)) { // logger.fine("Query tokenToQuery(String token) : token:" + token); } if (logger.isLoggable(Level.FINE)) { logger.fine("Query tokenToQuery(String token) : token:" + token); } token = removeEscapeChars(token); TokenStream source = analyzer.tokenStream(field, new StringReader(token)); CharTermAttribute charTermAtrib = source.getAttribute(CharTermAttribute.class); OffsetAttribute offsetAtrib = source.getAttribute(OffsetAttribute.class); PositionIncrementAttribute posIncAtt = source.addAttribute(PositionIncrementAttribute.class); ArrayList<Token> v = new ArrayList<Token>(); Token t; int positionCount = 0; boolean severalTokensAtSamePosition = false; while (true) { try { if (!source.incrementToken()) { break; } t = new Token(charTermAtrib.buffer(), 0, charTermAtrib.length(), offsetAtrib.startOffset(), offsetAtrib.endOffset()); t.setPositionIncrement(posIncAtt.getPositionIncrement()); } catch (IOException e) { t = null; } if (t == null) { break; } v.add(t); if (t.getPositionIncrement() != 0) { positionCount += t.getPositionIncrement(); } else { severalTokensAtSamePosition = true; } } try { source.close(); } catch (IOException e) { // ignore } if (v.size() == 0) { return null; } else if (v.size() == 1) { t = v.get(0); SpanTermQuery stq = new SpanTermQuery(new Term(field, new String(t.buffer(), 0, t.length()))); stq.setBoost(this.boost); return stq; } else { if (severalTokensAtSamePosition) { if (positionCount == 1) { // no phrase query: SpanQuery[] spanQueries = new SpanQuery[v.size()]; StringBuilder regex = new StringBuilder(); for (int i = 0; i < v.size(); i++) { spanQueries[i] = new SpanTermQuery(new Term(field, regex.toString())); } return new SpanOrQuery(spanQueries); } else { // All the Tokens in each sub-list are positioned at the the same location. ArrayList<ArrayList<Token>> identicallyPositionedTokenLists = new ArrayList<ArrayList<Token>>(); for (int i = 0; i < v.size(); i++) { if ((i == 0) || (v.get(i).getPositionIncrement() > 0)) { identicallyPositionedTokenLists.add(new ArrayList<Token>()); } ArrayList<Token> curList = identicallyPositionedTokenLists .get(identicallyPositionedTokenLists.size() - 1); curList.add(v.get(i)); } ArrayList<SpanQuery> spanNearSubclauses = new ArrayList<SpanQuery>(); for (int listNum = 0; listNum < identicallyPositionedTokenLists.size(); listNum++) { ArrayList<Token> curTokens = identicallyPositionedTokenLists.get(listNum); ArrayList<SpanTermQuery> curTermQueries = new ArrayList<SpanTermQuery>(); for (int tokenNum = 0; tokenNum < curTokens.size(); tokenNum++) { SpanTermQuery termQuery = new SpanTermQuery( new Term(field, curTokens.get(tokenNum).term())); termQuery.setBoost(this.boost); curTermQueries.add(termQuery); } int size = curTermQueries.size(); if (size <= 0) continue; else if (size == 1) spanNearSubclauses.add(curTermQueries.get(0)); else spanNearSubclauses.add(new SpanOrQuery(curTermQueries.toArray(new SpanQuery[0]))); } SpanNearQuery query = new SpanNearQuery( (SpanQuery[]) spanNearSubclauses.toArray(new SpanQuery[0]), slop, true); return query; } } else { SpanTermQuery[] clauses = new SpanTermQuery[v.size()]; for (int i = 0; i < v.size(); i++) { Token t2 = v.get(i); clauses[i] = new SpanTermQuery(new Term(field, new String(t2.buffer(), 0, t2.length()))); } SpanNearQuery query = new SpanNearQuery(clauses, slop, true); return query; } } }
From source file:com.mhs.qsol.QsolToQueryVisitor.java
License:Apache License
/** * Converts a token, as defined in the qsol.jtb JavaCC file, into an * appropriate query.// w w w . j a v a 2 s . c o m * * @param token * @return */ protected Query tokenToQuery(String token) { token = removeEscapeChars(token); TokenStream source = analyzer.tokenStream(field, new StringReader(token)); ArrayList<Token> v = new ArrayList<Token>(); Token t; int positionCount = 0; boolean severalTokensAtSamePosition = false; CharTermAttribute charTermAtrib = source.getAttribute(CharTermAttribute.class); OffsetAttribute offsetAtrib = source.getAttribute(OffsetAttribute.class); PositionIncrementAttribute posIncAtt = source.addAttribute(PositionIncrementAttribute.class); while (true) { try { if (!source.incrementToken()) { break; } t = new Token(charTermAtrib.buffer(), 0, charTermAtrib.length(), offsetAtrib.startOffset(), offsetAtrib.endOffset()); t.setPositionIncrement(posIncAtt.getPositionIncrement()); } catch (IOException e) { t = null; } if (t == null) { break; } v.add(t); if (t.getPositionIncrement() != 0) { positionCount += t.getPositionIncrement(); } else { severalTokensAtSamePosition = true; } } try { source.close(); } catch (IOException e) { // ignore } if (v.size() == 0) { // null's will get cleaned up in visitBooleanOp return null; } else if (v.size() == 1) { t = v.get(0); TermQuery termQuery = new TermQuery(new Term(field, new String(t.buffer(), 0, t.length()))); termQuery.setBoost(this.boost); return termQuery; } else { if (severalTokensAtSamePosition) { if (positionCount == 1) { // no phrase query: BooleanQuery q = new BooleanQuery(true); for (int i = 0; i < v.size(); i++) { t = v.get(i); TermQuery currentQuery = new TermQuery( new Term(field, new String(t.buffer(), 0, t.length()))); currentQuery.setBoost(this.boost); q.add(currentQuery, BooleanClause.Occur.SHOULD); } return q; } else { // All the Tokens in each sub-list are positioned at the the same location. ArrayList<ArrayList<Token>> identicallyPositionedTokenLists = new ArrayList<ArrayList<Token>>(); for (int i = 0; i < v.size(); i++) { if ((i == 0) || (v.get(i).getPositionIncrement() > 0)) { identicallyPositionedTokenLists.add(new ArrayList<Token>()); } ArrayList<Token> curList = identicallyPositionedTokenLists .get(identicallyPositionedTokenLists.size() - 1); curList.add(v.get(i)); } ArrayList<SpanQuery> spanNearSubclauses = new ArrayList<SpanQuery>(); for (int listNum = 0; listNum < identicallyPositionedTokenLists.size(); listNum++) { ArrayList<Token> curTokens = identicallyPositionedTokenLists.get(listNum); ArrayList<SpanTermQuery> curTermQueries = new ArrayList<SpanTermQuery>(); for (int tokenNum = 0; tokenNum < curTokens.size(); tokenNum++) { SpanTermQuery termQuery = new SpanTermQuery( new Term(field, curTokens.get(tokenNum).term())); termQuery.setBoost(this.boost); curTermQueries.add(termQuery); } int size = curTermQueries.size(); if (size <= 0) continue; else if (size == 1) spanNearSubclauses.add(curTermQueries.get(0)); else spanNearSubclauses.add(new SpanOrQuery(curTermQueries.toArray(new SpanQuery[0]))); } SpanNearQuery query = new SpanNearQuery( (SpanQuery[]) spanNearSubclauses.toArray(new SpanQuery[0]), slop, true); return query; } } else { SpanTermQuery[] clauses = new SpanTermQuery[v.size()]; for (int i = 0; i < v.size(); i++) { Token t2 = v.get(i); SpanTermQuery spanQuery = new SpanTermQuery( new Term(field, new String(t2.buffer(), 0, t2.length()))); spanQuery.setBoost(boost); clauses[i] = spanQuery; } // Note: There's a bug here (not by me) that where term offsets are not respected. SpanNearQuery query = new SpanNearQuery(clauses, slop, true); return query; } } }
From source file:com.mhs.qsol.spans.SpanWildcardQuery.java
License:Apache License
public Query rewrite(IndexReader reader) throws IOException { WildcardQuery wildQuery = new WildcardQuery(term); wildQuery.setRewriteMethod(WildcardQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE); BooleanQuery bq = null;/* ww w. ja v a2s . co m*/ bq = (BooleanQuery) ((ConstantScoreQuery) wildQuery.rewrite(reader)).getQuery(); BooleanClause[] clauses = bq.getClauses(); SpanQuery[] sqs = new SpanQuery[clauses.length]; for (int i = 0; i < clauses.length; i++) { BooleanClause clause = clauses[i]; // Clauses from RegexQuery.rewrite are always TermQuery's TermQuery tq = (TermQuery) clause.getQuery(); sqs[i] = new SpanTermQuery(tq.getTerm()); sqs[i].setBoost(tq.getBoost()); } SpanOrQuery query = new SpanOrQuery(sqs); query.setBoost(wildQuery.getBoost()); return query; }
From source file:com.wiseowl.WiseOwl.query.WiseOwlQParser.java
License:Apache License
@Override public Query parse() throws SyntaxError { //<start id="qqp.parse"/> Parse parse = ParserTool.parseLine(qstr, parser, 1)[0];//<co id="qqp.parseLine"/> /*//www .ja v a 2 s .c o m <calloutlist> <callout arearefs="qqp.parseLine"><para>Parse the question using the <classname>TreebankParser</classname>. The resulting <classname>Parse</classname> object can then be utilized by the classifier to determine the Answer Type.</para></callout> </calloutlist> */ //<end id="qqp.parse"/> //<start id="qqp.answerType"/> // String type = "P"; String type = atc.computeAnswerType(parse); String mt = atm.get(type); if (mt.equals("DESCRIPTION")) { BooleanQuery bq; BooleanQuery.Builder builder = new BooleanQuery.Builder(); //BooleanQuery bq=new BooleanQuery(false, 0); String field = "text"; SchemaField sf = req.getSchema().getFieldOrNull(field); try { Analyzer analyzer = sf.getType().getQueryAnalyzer(); TokenStream ts = analyzer.tokenStream(field, new StringReader(qstr)); ts.reset(); CharTermAttribute tok = null; while (ts.incrementToken()) {//<co id="qqp.addTerms"/> tok = ts.getAttribute(CharTermAttribute.class); String term = tok.toString(); //ts.reset(); //log.warn("terms {} ",term); builder.add(new TermQuery(new Term(field, term)), BooleanClause.Occur.SHOULD); } ts.close(); } catch (IOException e) { throw new SyntaxError(e.getLocalizedMessage()); } bq = builder.build(); return bq; //return new TermQuery(new Term("title", "she")); } else { //<end id="qqp.answerType"/> String field = "text"; //params.get(QUERY_FIELD); //String field="text"; SchemaField sp = req.getSchema().getFieldOrNull(field); if (sp == null) { throw new SolrException(ErrorCode.SERVER_ERROR, "Undefined field: " + field); } //<start id="qqp.query"/> List<SpanQuery> sql = new ArrayList<SpanQuery>(); if (mt != null) {//<co id="qqp.handleAT"/> String[] parts = mt.split("\\|"); if (parts.length == 1) { sql.add(new SpanTermQuery(new Term(field, mt.toLowerCase()))); } else { for (int pi = 0; pi < parts.length; pi++) { sql.add(new SpanTermQuery(new Term(field, parts[pi].toLowerCase()))); } } } log.warn("answer type mt : {} {} ", mt, type); FocusNoun fn = new FocusNoun(); String fnn[] = null; try { fnn = fn.getFocusNoun(qstr); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Analyzer analyzer = sp.getType().getQueryAnalyzer(); TokenStream ts = analyzer.tokenStream(field, new StringReader(qstr)); ts.reset(); CharTermAttribute tok = null; while (ts.incrementToken()) {//<co id="qqp.addTerms"/> tok = ts.getAttribute(CharTermAttribute.class); String term = tok.toString(); log.warn("terms boosted {} ", term); if (fnn != null) if (term.equals(fnn[0]) || term.equals(fnn[1])) { SpanQuery sq = new SpanTermQuery(new Term(field, term)); sql.add(new SpanBoostQuery(sq, 100f)); } else { SpanQuery sq = new SpanTermQuery(new Term(field, term)); sql.add(new SpanBoostQuery(sq, 5f)); } // sql.add(new SpanTermQuery(new Term(field, term))); } ts.close(); } catch (IOException e) { throw new SyntaxError(e.getLocalizedMessage()); } return new SpanOrQuery(sql.toArray(new SpanQuery[sql.size()])); // return new SpanNearQuery(sql.toArray(new SpanQuery[sql.size()]), params.getInt(OWLParams.SLOP, 10), true);//<co id="qqp.spanNear"/> /* <calloutlist> <callout arearefs="qqp.handleAT"><para>Add the AnswerType to the query</para></callout> <callout arearefs="qqp.addTerms"><para>Add the original query terms to the query</para></callout> <callout arearefs="qqp.spanNear"><para>Query the index looking for all of the parts near each other</para></callout> </calloutlist> */ //<end id="qqp.query"/> } }