Example usage for org.apache.lucene.search.spans SpanNotQuery SpanNotQuery

List of usage examples for org.apache.lucene.search.spans SpanNotQuery SpanNotQuery

Introduction

In this page you can find the example usage for org.apache.lucene.search.spans SpanNotQuery SpanNotQuery.

Prototype

public SpanNotQuery(SpanQuery include, SpanQuery exclude) 

Source Link

Document

Construct a SpanNotQuery matching spans from include which have no overlap with spans from exclude.

Usage

From source file:aos.lucene.search.advanced.SpanQueryTest.java

License:Apache License

public void testSpanNotQuery() throws Exception {
      SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[] { quick, fox }, 1, true);
      assertBothFoxes(quick_fox);/* www . j  a v a2s  .  com*/
      dumpSpans(quick_fox);

      SpanNotQuery quick_fox_dog = new SpanNotQuery(quick_fox, dog);
      assertBothFoxes(quick_fox_dog);
      dumpSpans(quick_fox_dog);

      SpanNotQuery no_quick_red_fox = new SpanNotQuery(quick_fox, red);
      assertOnlyBrownFox(no_quick_red_fox);
      dumpSpans(no_quick_red_fox);
  }

From source file:com.leavesfly.lia.advsearching.SpanQueryTest.java

License:Apache License

public void testSpanNotQuery() throws Exception {
    SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[] { quick, fox }, 1, true);
    assertBothFoxes(quick_fox);/*ww  w .  ja v  a2s  . co m*/
    dumpSpans(quick_fox);

    SpanNotQuery quick_fox_dog = new SpanNotQuery(quick_fox, dog);
    assertBothFoxes(quick_fox_dog);
    dumpSpans(quick_fox_dog);

    SpanNotQuery no_quick_red_fox = new SpanNotQuery(quick_fox, red);
    assertOnlyBrownFox(no_quick_red_fox);
    dumpSpans(no_quick_red_fox);
}

From source file:com.mhs.qsol.proximity.distribute.BasicDistributable.java

License:Apache License

public Query distribute(SpanQuery query, ProxInfo proxInfo) {
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("distribute(Distributable) - to distrib:" + query + " and :" + this.query);
    }/*ww w. ja va 2 s  .  c  o m*/

    if ((this.query == null) || (query == null)) {
        throw new QsolParseException("A proximity search cannot contain stop words: " + query + " ~"
                + proxInfo.distance + proxInfo.proxType + " " + this.query);
    }

    switch (proxInfo.proxType) {
    case WORD:

        if (logger.isLoggable(Level.FINE)) {
            logger.fine("return:" + "spanQuery(" + query + "," + this.query + ")");
        }

        SpanQuery spanQuery = new SpanNearQuery(new SpanQuery[] { query, this.query },
                Integer.parseInt(proxInfo.distance), proxInfo.ordered);

        if (proxInfo.fieldBreakMarker != null) {
            SpanTermQuery fieldBreakMarker = new SpanTermQuery(
                    new Term(this.query.getField(), proxInfo.fieldBreakMarker));
            spanQuery = new SpanNotQuery(spanQuery, fieldBreakMarker);
        }

        return spanQuery;

    case PARAGRAPH:

        SpanTermQuery paraMarker = new SpanTermQuery(new Term(this.query.getField(), proxInfo.paraMarker));
        SpanQuery querySpan = new SpanNearQuery(new SpanQuery[] { query, this.query }, 99999, false);

        if (proxInfo.fieldBreakMarker != null) {
            SpanTermQuery fieldBreakMarker = new SpanTermQuery(
                    new Term(this.query.getField(), proxInfo.fieldBreakMarker));
            querySpan = new SpanNotQuery(querySpan, fieldBreakMarker);
        }

        return new SpanWithinQuery(querySpan, paraMarker, Integer.parseInt(proxInfo.distance));

    case SENTENCE:

        SpanTermQuery sentMarker = new SpanTermQuery(new Term(this.query.getField(), proxInfo.sentMarker));
        querySpan = new SpanNearQuery(new SpanQuery[] { query, this.query }, 99999, false);

        if (proxInfo.fieldBreakMarker != null) {
            SpanTermQuery fieldBreakMarker = new SpanTermQuery(
                    new Term(this.query.getField(), proxInfo.fieldBreakMarker));
            querySpan = new SpanNotQuery(querySpan, fieldBreakMarker);
        }

        return new SpanWithinQuery(querySpan, sentMarker, Integer.parseInt(proxInfo.distance));
    }

    throw new RuntimeException("proximity search was not of type sent/para/word");
}

From source file:com.o19s.solr.swan.nodes.SwanNotNode.java

License:Apache License

@Override
public SpanQuery getSpanQuery(String field) {
    return new SpanNotQuery(_left.getSpanQuery(field), _right.getSpanQuery(field));
}

From source file:com.o19s.solr.swan.nodes.SwanXOrOperationNode.java

License:Apache License

@Override
public SpanQuery getSpanQuery(String field) {
    SpanOrQuery query = new SpanOrQuery();

    List<SwanNode> inc;/*from   w  ww .  java 2s.  c o m*/
    for (int x = 0; x < _nodes.size(); x++) {
        inc = new ArrayList<SwanNode>();
        inc.addAll(_nodes);
        inc.remove(x);

        SpanOrQuery or = new SpanOrQuery();
        for (SwanNode n : inc) {
            or.addClause(n.getSpanQuery(field));
        }
        if (or.getClauses().length > 0) {
            SpanNotQuery not = new SpanNotQuery(_nodes.get(x).getSpanQuery(field), or);
            query.addClause(not);
        }
    }

    return query;
}

From source file:es.ua.labidiomas.corpus.searcher.Searcher.java

private SpanQuery _prepareQuery(List<SearchNode> searchNodes, String field, boolean order, boolean precise) {
    SpanQuery[] clauses = new SpanQuery[] {
            new SpanTermQuery(new Term(field, searchNodes.get(0).getWord().toLowerCase())) };
    SpanQuery query = new SpanNearQuery(clauses, 0, true);
    for (int i = 1; i < searchNodes.size(); i++) {
        SearchNode node = searchNodes.get(i);
        SearchNode prevNode = searchNodes.get(i - 1);
        clauses = new SpanQuery[] { query, new SpanTermQuery(new Term(field, node.getWord().toLowerCase())) };
        query = new SpanNearQuery(clauses, prevNode.getDistance(), order);
        if (precise && prevNode.getDistance() > 0) {
            SpanNearQuery spanNearQuery = new SpanNearQuery(clauses, prevNode.getDistance() - 1, order);
            query = new SpanNotQuery(query, spanNearQuery);
        }/*from  w w w  .ja  v a 2  s  . c om*/
    }

    return query;
}

From source file:es.ua.labidiomas.corpus.searcher.Searcher.java

private BooleanQuery _prepareLetterQuery(SearchConfiguration params, SpanQuery query) throws ParseException {
    BooleanQuery searchQuery = new BooleanQuery();

    SpanQuery prefixQUery = new SpanMultiTermQueryWrapper(
            new PrefixQuery(new Term("text", params.getSort().getLetter())));

    SpanNearQuery spanNear1 = new SpanNearQuery(new SpanQuery[] { query, prefixQUery },
            params.getSort().getPosition() - 1, true);

    if (params.getSort().getPosition() != 1) {
        SpanNearQuery spanNear2 = new SpanNearQuery(new SpanQuery[] { query, prefixQUery },
                params.getSort().getPosition() - 2, true);

        SpanNotQuery textQUery = new SpanNotQuery(spanNear1, spanNear2);

        searchQuery.add(textQUery, BooleanClause.Occur.MUST);
    } else {//from   w ww .ja  v a 2s  .  c o m
        searchQuery.add(spanNear1, BooleanClause.Occur.MUST);
    }

    searchQuery.add(_prepareDiscourseQuery(params), BooleanClause.Occur.MUST);

    return searchQuery;
}

From source file:org.compass.core.lucene.engine.query.LuceneSearchEngineQueryBuilder.java

License:Apache License

public SearchEngineSpanQuery spanNot(SearchEngineSpanQuery include, SearchEngineSpanQuery exclude) {
    SpanNotQuery spanNotQuery = new SpanNotQuery(((LuceneSearchEngineSpanQuery) include).toSpanQuery(),
            ((LuceneSearchEngineSpanQuery) exclude).toSpanQuery());
    return new LuceneSearchEngineQuery.LuceneSearchEngineSpanQuery(searchEngineFactory, spanNotQuery);
}

From source file:org.elasticsearch.index.query.json.SpanNotJsonQueryParser.java

License:Apache License

@Override
public Query parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException {
    JsonParser jp = parseContext.jp();//from   w w w  .  j  a  v  a2 s .c  om

    float boost = 1.0f;

    SpanQuery include = null;
    SpanQuery exclude = null;

    String currentFieldName = null;
    JsonToken token;
    while ((token = jp.nextToken()) != JsonToken.END_OBJECT) {
        if (token == JsonToken.FIELD_NAME) {
            currentFieldName = jp.getCurrentName();
        } else if (token == JsonToken.START_OBJECT) {
            if ("include".equals(currentFieldName)) {
                Query query = parseContext.parseInnerQuery();
                if (!(query instanceof SpanQuery)) {
                    throw new QueryParsingException(index, "spanNot [include] must be of type span query");
                }
                include = (SpanQuery) query;
            } else if ("exclude".equals(currentFieldName)) {
                Query query = parseContext.parseInnerQuery();
                if (!(query instanceof SpanQuery)) {
                    throw new QueryParsingException(index, "spanNot [exclude] must be of type span query");
                }
                exclude = (SpanQuery) query;
            }
        } else {
            if ("boost".equals(currentFieldName)) {
                if (token == JsonToken.VALUE_STRING) {
                    boost = Float.parseFloat(jp.getText());
                } else {
                    boost = jp.getFloatValue();
                }
            }
        }
    }
    if (include == null) {
        throw new QueryParsingException(index, "spanNot must have [include] span query clause");
    }
    if (exclude == null) {
        throw new QueryParsingException(index, "spanNot must have [exclude] span query clause");
    }

    SpanNotQuery query = new SpanNotQuery(include, exclude);
    query.setBoost(boost);
    return query;
}

From source file:org.elasticsearch.index.query.PercolateQueryTests.java

License:Apache License

public void testDuelSpecificQueries() throws Exception {
    CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD, 128);
    commonTermsQuery.add(new Term("field", "quick"));
    commonTermsQuery.add(new Term("field", "brown"));
    commonTermsQuery.add(new Term("field", "fox"));
    addPercolatorQuery("_id1", commonTermsQuery);

    BlendedTermQuery blendedTermQuery = BlendedTermQuery.booleanBlendedQuery(
            new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") },
            false);/*from  ww w.  j a  v a2 s.co  m*/
    addPercolatorQuery("_id2", blendedTermQuery);

    SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true)
            .addClause(new SpanTermQuery(new Term("field", "quick")))
            .addClause(new SpanTermQuery(new Term("field", "brown")))
            .addClause(new SpanTermQuery(new Term("field", "fox"))).build();
    addPercolatorQuery("_id3", spanNearQuery);

    SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true)
            .addClause(new SpanTermQuery(new Term("field", "the")))
            .addClause(new SpanTermQuery(new Term("field", "lazy")))
            .addClause(new SpanTermQuery(new Term("field", "doc"))).build();
    SpanOrQuery spanOrQuery = new SpanOrQuery(spanNearQuery, spanNearQuery2);
    addPercolatorQuery("_id4", spanOrQuery);

    SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery);
    addPercolatorQuery("_id5", spanNotQuery);

    indexWriter.close();
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);

    MemoryIndex memoryIndex = new MemoryIndex();
    memoryIndex.addField("field", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer());
    duelRun(memoryIndex, shardSearcher);
}