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

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

Introduction

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

Prototype

public SpanFirstQuery(SpanQuery match, int end) 

Source Link

Document

Construct a SpanFirstQuery matching spans in match whose end position is less than or equal to end.

Usage

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

License:Apache License

public void testSpanFirstQuery() throws Exception {
      SpanFirstQuery sfq = new SpanFirstQuery(brown, 2);
      assertNoMatches(sfq);/*from ww  w .j a v  a2s  .c  o  m*/

      dumpSpans(sfq);

      sfq = new SpanFirstQuery(brown, 3);
      dumpSpans(sfq);
      assertOnlyBrownFox(sfq);
  }

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  av  a  2 s. com*/

      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 testSpanFirstQuery() throws Exception {
    SpanFirstQuery sfq = new SpanFirstQuery(brown, 2);
    assertNoMatches(sfq);//from   w ww . j av  a 2 s .  c om

    dumpSpans(sfq);

    sfq = new SpanFirstQuery(brown, 3);
    dumpSpans(sfq);
    assertOnlyBrownFox(sfq);
}

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);/*from   w w  w  .  jav a 2s  .  c om*/

    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:io.vertigo.dynamo.plugins.collections.lucene.RamLuceneQueryFactory.java

License:Apache License

private static Query createParsedKeywordsQuery(final Analyzer queryAnalyser, final String fieldName,
        final String keywords) throws IOException {
    final Builder queryBuilder = new BooleanQuery.Builder();
    final Reader reader = new StringReader(keywords);
    try (final TokenStream tokenStream = queryAnalyser.tokenStream(fieldName, reader)) {
        tokenStream.reset();/*from ww  w .  ja  v  a2  s.  c  om*/
        try {
            final CharTermAttribute termAttribute = tokenStream.getAttribute(CharTermAttribute.class);
            while (tokenStream.incrementToken()) {
                final String term = new String(termAttribute.buffer(), 0, termAttribute.length());
                final PrefixQuery prefixQuery = new PrefixQuery(new Term(fieldName, term));
                queryBuilder.add(prefixQuery, BooleanClause.Occur.MUST);
                final SpanFirstQuery spanSecondQuery = new SpanFirstQuery(
                        new SpanMultiTermQueryWrapper<>(prefixQuery), 1);
                queryBuilder.add(spanSecondQuery, BooleanClause.Occur.SHOULD);
            }
        } finally {
            reader.reset();
            tokenStream.end();
        }
    }
    return queryBuilder.build();
}

From source file:org.codelibs.elasticsearch.index.query.SpanFirstQueryBuilder.java

License:Apache License

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query innerSpanQuery = matchBuilder.toQuery(context);
    assert innerSpanQuery instanceof SpanQuery;
    return new SpanFirstQuery((SpanQuery) innerSpanQuery, end);
}

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

License:Apache License

public SearchEngineSpanQuery spanFirst(SearchEngineSpanQuery searchEngineSpanQuery, int end) {
    SpanQuery spanQuery = new SpanFirstQuery(
            ((LuceneSearchEngineSpanQuery) searchEngineSpanQuery).toSpanQuery(), end);
    return new LuceneSearchEngineQuery.LuceneSearchEngineSpanQuery(searchEngineFactory, spanQuery);
}

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

License:Apache License

public SearchEngineSpanQuery spanFirst(String resourcePropertyName, String value, int end) {
    SpanQuery spanQuery = new SpanFirstQuery(new SpanTermQuery(new Term(resourcePropertyName, value)), end);
    return new LuceneSearchEngineQuery.LuceneSearchEngineSpanQuery(searchEngineFactory, spanQuery);
}

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

License:Apache License

@Override
public Query parse(JsonQueryParseContext parseContext) throws IOException, QueryParsingException {
    JsonParser jp = parseContext.jp();//ww  w  .ja va  2 s . c  o m

    float boost = 1.0f;

    SpanQuery match = null;
    int end = -1;

    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 ("match".equals(currentFieldName)) {
                Query query = parseContext.parseInnerQuery();
                if (!(query instanceof SpanQuery)) {
                    throw new QueryParsingException(index, "spanFirst [match] must be of type span query");
                }
                match = (SpanQuery) query;
            }
        } else {
            if ("boost".equals(currentFieldName)) {
                if (token == JsonToken.VALUE_STRING) {
                    boost = Float.parseFloat(jp.getText());
                } else {
                    boost = jp.getFloatValue();
                }
            } else if ("end".equals(currentFieldName)) {
                if (token == JsonToken.VALUE_STRING) {
                    end = Integer.parseInt(jp.getText());
                } else {
                    end = jp.getIntValue();
                }
            }
        }
    }
    if (match == null) {
        throw new QueryParsingException(index, "spanFirst must have [match] span query clause");
    }
    if (end == -1) {
        throw new QueryParsingException(index, "spanFirst must have [end] set for it");
    }

    SpanFirstQuery query = new SpanFirstQuery(match, end);
    query.setBoost(boost);
    return query;
}

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

License:Apache License

@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    float boost = 1.0f;

    SpanQuery match = null;/*  w  w  w  .  ja  v  a 2 s .com*/
    int end = -1;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("match".equals(currentFieldName)) {
                Query query = parseContext.parseInnerQuery();
                if (!(query instanceof SpanQuery)) {
                    throw new QueryParsingException(parseContext.index(),
                            "spanFirst [match] must be of type span query");
                }
                match = (SpanQuery) query;
            } else {
                throw new QueryParsingException(parseContext.index(),
                        "[span_first] query does not support [" + currentFieldName + "]");
            }
        } else {
            if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else if ("end".equals(currentFieldName)) {
                end = parser.intValue();
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new QueryParsingException(parseContext.index(),
                        "[span_first] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (match == null) {
        throw new QueryParsingException(parseContext.index(), "spanFirst must have [match] span query clause");
    }
    if (end == -1) {
        throw new QueryParsingException(parseContext.index(), "spanFirst must have [end] set for it");
    }

    SpanFirstQuery query = new SpanFirstQuery(match, end);
    query.setBoost(boost);
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, query);
    }
    return query;
}