本文整理汇总了Java中org.apache.lucene.search.TermRangeQuery类的典型用法代码示例。如果您正苦于以下问题:Java TermRangeQuery类的具体用法?Java TermRangeQuery怎么用?Java TermRangeQuery使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TermRangeQuery类属于org.apache.lucene.search包,在下文中一共展示了TermRangeQuery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: termRange
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
private static QueryBuilder termRange(Query q) {
QueryBuilder qb = null;
TermRangeQuery trq = (TermRangeQuery) q;
if (!StringUtils.isBlank(trq.getField())) {
String from = trq.getLowerTerm() != null ? Term.toString(trq.getLowerTerm()) : "*";
String to = trq.getUpperTerm() != null ? Term.toString(trq.getUpperTerm()) : "*";
boolean nestedMode = nestedMode() && trq.getField().matches(PROPS_REGEX);
qb = rangeQuery(nestedMode ? getValueFieldNameFromRange(from, to) : trq.getField());
if ("*".equals(from) && "*".equals(to)) {
qb = matchAllQuery();
}
if (!"*".equals(from)) {
((RangeQueryBuilder) qb).from(getNumericValue(from)).includeLower(trq.includesLower());
}
if (!"*".equals(to)) {
((RangeQueryBuilder) qb).to(getNumericValue(to)).includeUpper(trq.includesUpper());
}
if (nestedMode) {
qb = nestedPropsQuery(keyValueBoolQuery(trq.getField(), qb));
}
}
return qb;
}
示例2: testReaderChaining
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
public void testReaderChaining() throws Exception {
assertTrue(reader.getRefCount() > 0);
IndexReader wrappedReader = SlowCompositeReaderWrapper.wrap(reader);
wrappedReader = new ParallelAtomicReader((AtomicReader) wrappedReader);
IndexSearcher searcher = newSearcher(wrappedReader);
TermRangeQuery query = TermRangeQuery.newStringRange("field", "a", "z", true, true);
searcher.search(query, 5);
reader.close(); // close original child reader
try {
searcher.search(query, 5);
} catch (AlreadyClosedException ace) {
assertEquals(
"this IndexReader cannot be used anymore as one of its child readers was closed",
ace.getMessage()
);
} finally {
// shutdown executor: in case of wrap-wrap-wrapping
searcher.getIndexReader().close();
}
}
示例3: getRangeQuery
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Override
public Query getRangeQuery(QParser parser, SchemaField field, String part1, String part2, boolean minInclusive, boolean maxInclusive) {
String f = field.getName();
BytesRef low = part1 == null ? null : getCollationKey(f, part1);
BytesRef high = part2 == null ? null : getCollationKey(f, part2);
if (!field.indexed() && field.hasDocValues()) {
if (field.multiValued()) {
return new ConstantScoreQuery(DocTermOrdsRangeFilter.newBytesRefRange(
field.getName(), low, high, minInclusive, maxInclusive));
} else {
return new ConstantScoreQuery(FieldCacheRangeFilter.newBytesRefRange(
field.getName(), low, high, minInclusive, maxInclusive));
}
} else {
return new TermRangeQuery(field.getName(), low, high, minInclusive, maxInclusive);
}
}
示例4: toString
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
protected String toString(TermRangeQuery termRangeQuery, String field) {
StringBuilder buffer = new StringBuilder();
boolean includeLower = termRangeQuery.includesLower();
BytesRef lowerTerm = termRangeQuery.getLowerTerm();
BytesRef upperTerm = termRangeQuery.getUpperTerm();
boolean includeUpper = termRangeQuery.includesUpper();
if (!termRangeQuery.getField().equals(field)) {
buffer.append(termRangeQuery.getField());
buffer.append(":");
}
buffer.append(includeLower ? '[' : '{');
// TODO: all these toStrings for queries should just output the bytes,
// it might not be UTF-8!
buffer.append(
lowerTerm != null ? ("*".equals(Term.toString(lowerTerm)) ? "\\*" : Term.toString(lowerTerm)) : "*");
buffer.append(" TO ");
buffer.append(
upperTerm != null ? ("*".equals(Term.toString(upperTerm)) ? "\\*" : Term.toString(upperTerm)) : "*");
buffer.append(includeUpper ? ']' : '}');
return buffer.toString();
}
示例5: newRangeQuery
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
/**
* Builds a new {@link TermRangeQuery} instance
*
* @param field
* Field
* @param part1
* min
* @param part2
* max
* @param startInclusive
* true if the start of the range is inclusive
* @param endInclusive
* true if the end of the range is inclusive
* @return new {@link TermRangeQuery} instance
*/
protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive,
boolean endInclusive) {
final BytesRef start;
final BytesRef end;
if (part1 == null) {
start = null;
} else {
start = analyzeRangeTerms ? getAnalyzer().normalize(field, part1) : new BytesRef(part1);
}
if (part2 == null) {
end = null;
} else {
end = analyzeRangeTerms ? getAnalyzer().normalize(field, part2) : new BytesRef(part2);
}
final TermRangeQuery query = new TermRangeQuery(field, start, end, startInclusive, endInclusive);
query.setRewriteMethod(multiTermRewriteMethod);
return query;
}
示例6: addLocaleSpecificTextRange
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
private void addLocaleSpecificTextRange(String expandedFieldName, String part1, String part2, boolean includeLower, boolean includeUpper, BooleanQuery booleanQuery,
Locale locale, AnalysisMode analysisMode, IndexTokenisationMode tokenisationMode, IndexTokenisationMode preferredtokenisationMode) throws ParseException
{
String field = getFieldName(expandedFieldName, locale, tokenisationMode, preferredtokenisationMode);
StringBuilder builder = new StringBuilder();
builder.append("\u0000").append(locale.toString()).append("\u0000").append(part1);
String first = getToken(field, builder.toString(), analysisMode);
if ((first == null) && (false == field.endsWith(".u")))
{
first = getToken(field + ".u", builder.toString(), analysisMode);
}
builder = new StringBuilder();
builder.append("\u0000").append(locale.toString()).append("\u0000").append(part2);
String last = getToken(field, builder.toString(), analysisMode);
if ((last == null) && (false == field.endsWith(".u")))
{
last = getToken(field + ".u", builder.toString(), analysisMode);
}
Query query = new TermRangeQuery(field, first, last, includeLower, includeUpper);
booleanQuery.add(query, Occur.SHOULD);
}
示例7: testStringClose
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void testStringClose() {
Map<String, ColumnMapper> map = new HashMap<>();
map.put("name", new ColumnMapperBoolean());
Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());
RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", "beta", true, true);
Query query = rangeCondition.query(mappers);
Assert.assertNotNull(query);
Assert.assertEquals(TermRangeQuery.class, query.getClass());
Assert.assertEquals("name", ((TermRangeQuery) query).getField());
Assert.assertEquals("alpha", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
Assert.assertEquals("beta", ((TermRangeQuery) query).getUpperTerm().utf8ToString());
Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper());
Assert.assertEquals(0.5f, query.getBoost(), 0);
}
示例8: testStringOpen
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void testStringOpen() {
Map<String, ColumnMapper> map = new HashMap<>();
map.put("name", new ColumnMapperBoolean());
Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());
RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "alpha", null, true, false);
Query query = rangeCondition.query(mappers);
Assert.assertNotNull(query);
Assert.assertEquals(TermRangeQuery.class, query.getClass());
Assert.assertEquals("name", ((TermRangeQuery) query).getField());
Assert.assertEquals("alpha", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
Assert.assertEquals(null, ((TermRangeQuery) query).getUpperTerm());
Assert.assertNull(((TermRangeQuery) query).getUpperTerm());
Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
Assert.assertEquals(false, ((TermRangeQuery) query).includesUpper());
Assert.assertEquals(0.5f, query.getBoost(), 0);
}
示例9: testInetV4
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void testInetV4() {
Map<String, ColumnMapper> map = new HashMap<>();
map.put("name", new ColumnMapperInet());
Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());
RangeCondition rangeCondition = new RangeCondition(0.5f, "name", "192.168.0.01", "192.168.0.045", true, true);
Query query = rangeCondition.query(mappers);
Assert.assertNotNull(query);
Assert.assertEquals(TermRangeQuery.class, query.getClass());
Assert.assertEquals("name", ((TermRangeQuery) query).getField());
Assert.assertEquals("192.168.0.1", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
Assert.assertEquals("192.168.0.45", ((TermRangeQuery) query).getUpperTerm().utf8ToString());
Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper());
Assert.assertEquals(0.5f, query.getBoost(), 0);
}
示例10: testInetV6
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void testInetV6() {
Map<String, ColumnMapper> map = new HashMap<>();
map.put("name", new ColumnMapperInet());
Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());
RangeCondition rangeCondition = range("name").boost(0.5f)
.lower("2001:DB8:2de::e13")
.upper("2001:DB8:02de::e23")
.includeLower(true)
.includeUpper(true)
.build();
Query query = rangeCondition.query(mappers);
Assert.assertNotNull(query);
Assert.assertEquals(TermRangeQuery.class, query.getClass());
Assert.assertEquals("name", ((TermRangeQuery) query).getField());
Assert.assertEquals("2001:db8:2de:0:0:0:0:e13", ((TermRangeQuery) query).getLowerTerm().utf8ToString());
Assert.assertEquals("2001:db8:2de:0:0:0:0:e23", ((TermRangeQuery) query).getUpperTerm().utf8ToString());
Assert.assertEquals(true, ((TermRangeQuery) query).includesLower());
Assert.assertEquals(true, ((TermRangeQuery) query).includesUpper());
Assert.assertEquals(0.5f, query.getBoost(), 0);
}
示例11: test5
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void test5() throws ParseException, IOException {
parser = new SuperParser(LUCENE_VERSION, getFieldManager(new WhitespaceAnalyzer(LUCENE_VERSION)), true, null,
ScoreType.SUPER, new Term("_primedoc_"));
Query query = parser.parse("<a.a:a a.d:{e TO f} a.b:b a.test:hello\\<> -<g.c:c g.d:d>");
BooleanQuery booleanQuery1 = new BooleanQuery();
booleanQuery1.add(new TermQuery(new Term("a.a", "a")), Occur.SHOULD);
booleanQuery1.add(new TermRangeQuery("a.d", new BytesRef("e"), new BytesRef("f"), false, false), Occur.SHOULD);
booleanQuery1.add(new TermQuery(new Term("a.b", "b")), Occur.SHOULD);
// std analyzer took the "<" out
booleanQuery1.add(new TermQuery(new Term("a.test", "hello<")), Occur.SHOULD);
BooleanQuery booleanQuery2 = new BooleanQuery();
booleanQuery2.add(new TermQuery(new Term("g.c", "c")), Occur.SHOULD);
booleanQuery2.add(new TermQuery(new Term("g.d", "d")), Occur.SHOULD);
SuperQuery superQuery1 = new SuperQuery(booleanQuery1, ScoreType.SUPER, new Term("_primedoc_"));
SuperQuery superQuery2 = new SuperQuery(booleanQuery2, ScoreType.SUPER, new Term("_primedoc_"));
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(superQuery1, Occur.SHOULD);
booleanQuery.add(superQuery2, Occur.MUST_NOT);
assertQuery(booleanQuery, query);
}
示例12: learnVocab
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Override
public void learnVocab() throws IOException {
super.learnVocab();
final String field = ((LuceneIndexConfig)config).getField();
final Terms terms = MultiFields.getTerms(reader, field);
final BytesRef maxTerm = terms.getMax();
final BytesRef minTerm = terms.getMin();
Query q = new TermRangeQuery(field, minTerm, maxTerm, true, true);
IndexSearcher searcher = new IndexSearcher(reader);
topDocs = searcher.search(q, Integer.MAX_VALUE);
TermsEnum termsEnum = null;
termsEnum = terms.iterator(termsEnum);
termsEnum.seekCeil(new BytesRef());
BytesRef term = termsEnum.term();
while(term != null){
int p = addWordToVocab(term.utf8ToString());
vocab[p].setCn((int)termsEnum.totalTermFreq());
term = termsEnum.next();
}
}
示例13: testTermRangeQueryBigger
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void testTermRangeQueryBigger() {
JsonObject json = Json.createObjectBuilder()
.add("query", Json.createObjectBuilder()
.add("type", "RangeQuery")
.add("rangeType", "String")
.add("field", "field")
.add("lowerTerm", "value")
.add("upperTerm", JsonValue.NULL)
.add("includeLower", JsonValue.FALSE)
.add("includeUpper", JsonValue.FALSE))
.build();
QueryData q = new QueryData(new StringReader(json.toString()), queryConverter);
TermRangeQuery query = TermRangeQuery.newStringRange("field", "value", null, false, false);
assertEquals(query, q.query);
}
示例14: testTermRangeQueryLower
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
@Test
public void testTermRangeQueryLower() {
JsonObject json = Json.createObjectBuilder()
.add("query", Json.createObjectBuilder()
.add("type", "RangeQuery")
.add("rangeType", "String")
.add("field", "field")
.add("lowerTerm", JsonValue.NULL)
.add("upperTerm", "value")
.add("includeLower", JsonValue.TRUE)
.add("includeUpper", JsonValue.TRUE))
.build();
QueryData q = new QueryData(new StringReader(json.toString()), queryConverter);
TermRangeQuery query = TermRangeQuery.newStringRange("field", null, "value", true, true);
assertEquals(query, q.query);
}
示例15: getRangeQuery
import org.apache.lucene.search.TermRangeQuery; //导入依赖的package包/类
public Query getRangeQuery(String field,
String part1,
String part2,
boolean inclusive)
throws ParseException {
TermRangeQuery query = (TermRangeQuery)
super.getRangeQuery(field, part1, part2,
inclusive);
if ("price".equals(field)) {
return NumericRangeQuery.newDoubleRange(
"price",
Double.parseDouble(
query.getLowerTerm()),
Double.parseDouble(
query.getUpperTerm()),
query.includesLower(),
query.includesUpper());
} else {
return query;
}
}