本文整理匯總了Java中org.apache.lucene.search.PrefixQuery類的典型用法代碼示例。如果您正苦於以下問題:Java PrefixQuery類的具體用法?Java PrefixQuery怎麽用?Java PrefixQuery使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PrefixQuery類屬於org.apache.lucene.search包,在下文中一共展示了PrefixQuery類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createFQNQuery
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
private static BooleanQuery createFQNQuery(final String resourceName) {
String pkgName;
String sName;
int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR);
if (index < 0) {
pkgName = ""; //NOI18N
sName = resourceName;
} else {
pkgName = resourceName.substring(0, index);
sName = resourceName.substring(index+1);
}
final BooleanQuery snQuery = new BooleanQuery();
snQuery.add (new WildcardQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + DocumentUtil.WILDCARD_QUERY_WILDCARD)),Occur.SHOULD);
snQuery.add (new PrefixQuery (new Term (DocumentUtil.FIELD_BINARY_NAME, sName + '$')),Occur.SHOULD); //NOI18N
if (pkgName.length() == 0) {
return snQuery;
}
final BooleanQuery fqnQuery = new BooleanQuery();
fqnQuery.add(new TermQuery(new Term(DocumentUtil.FIELD_PACKAGE_NAME,pkgName)), Occur.MUST);
fqnQuery.add(snQuery, Occur.MUST);
return fqnQuery;
}
示例2: addSearchTermQueries
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
/**
* Adds for every field in {@link IIndexTypeConf} a {@link TermQuery}, {@link PrefixQuery} and
* a {@link FuzzyQuery} with a suitable boost relative to the given base boost.
*
* @param indexType the type in which should be searched.
* @param search the search text.
* @param subQuery the {@link BooleanQuery} to add the sub queries.
* @param baseBoost highest possible boost of the query. The more the match is exact
* than a bigger boost will be used.
*/
private static void addSearchTermQueries(final IIndexTypeConf indexType, final String search,
final BooleanQuery subQuery, final float baseBoost) {
for(final IIndexFieldConf<?> field : indexType.getFields()) {
final Term term = new Term(field.getName(), search);
final TermQuery exactQuery = new TermQuery(term);
exactQuery.setBoost(baseBoost);
subQuery.add(exactQuery, Occur.SHOULD);
final PrefixQuery pfQuery = new PrefixQuery(term);
pfQuery.setBoost(0.7f*baseBoost);
subQuery.add(pfQuery, Occur.SHOULD);
final FuzzyQuery fuzzyQuery = new FuzzyQuery(term);
fuzzyQuery.setBoost(0.5f*baseBoost);
subQuery.add(fuzzyQuery, Occur.SHOULD);
}
}
示例3: newPrefixQuery
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
/**
* Dispatches to Lucene's SimpleQueryParser's newPrefixQuery, optionally
* lowercasing the term first or trying to analyze terms
*/
@Override
public Query newPrefixQuery(String text) {
BooleanQuery.Builder bq = new BooleanQuery.Builder();
bq.setDisableCoord(true);
for (Map.Entry<String,Float> entry : weights.entrySet()) {
final String fieldName = entry.getKey();
try {
if (settings.analyzeWildcard()) {
Query analyzedQuery = newPossiblyAnalyzedQuery(fieldName, text);
if (analyzedQuery != null) {
bq.add(wrapWithBoost(analyzedQuery, entry.getValue()), BooleanClause.Occur.SHOULD);
}
} else {
Term term = new Term(fieldName, getAnalyzer().normalize(fieldName, text));
Query query = new PrefixQuery(term);
bq.add(wrapWithBoost(query, entry.getValue()), BooleanClause.Occur.SHOULD);
}
} catch (RuntimeException e) {
return rethrowUnlessLenient(e);
}
}
return super.simplify(bq.build());
}
示例4: testAnalyzeWildcard
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
public void testAnalyzeWildcard() {
SimpleQueryParser.Settings settings = new SimpleQueryParser.Settings();
settings.analyzeWildcard(true);
Map<String, Float> weights = new HashMap<>();
weights.put("field1", 1.0f);
SimpleQueryParser parser = new MockSimpleQueryParser(new StandardAnalyzer(), weights, -1, settings);
for (Operator op : Operator.values()) {
BooleanClause.Occur defaultOp = op.toBooleanClauseOccur();
parser.setDefaultOperator(defaultOp);
Query query = parser.parse("first foo-bar-foobar* last");
Query expectedQuery =
new BooleanQuery.Builder()
.add(new BooleanClause(new TermQuery(new Term("field1", "first")), defaultOp))
.add(new BooleanQuery.Builder()
.add(new BooleanClause(new TermQuery(new Term("field1", "foo")), defaultOp))
.add(new BooleanClause(new TermQuery(new Term("field1", "bar")), defaultOp))
.add(new BooleanClause(new PrefixQuery(new Term("field1", "foobar")), defaultOp))
.build(), defaultOp)
.add(new BooleanClause(new TermQuery(new Term("field1", "last")), defaultOp))
.build();
assertThat(query, equalTo(expectedQuery));
}
}
示例5: addGeneFilter
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
private void addGeneFilter(BooleanQuery.Builder builder) {
if (genes != null && !genes.field.isEmpty()) {
BooleanQuery.Builder genesBuilder = new BooleanQuery.Builder();
for (int i = 0; i < genes.field.size(); i++) {
PrefixQuery geneIdPrefixQuery = new PrefixQuery(new Term(FeatureIndexFields.GENE_ID.getFieldName(),
genes.field.get(i).toLowerCase()));
PrefixQuery geneNamePrefixQuery = new PrefixQuery(
new Term(FeatureIndexFields.GENE_NAME.getFieldName(), genes.field.get(i).toLowerCase()));
BooleanQuery.Builder geneIdOrNameQuery = new BooleanQuery.Builder();
geneIdOrNameQuery.add(geneIdPrefixQuery, BooleanClause.Occur.SHOULD);
geneIdOrNameQuery.add(geneNamePrefixQuery, BooleanClause.Occur.SHOULD);
genesBuilder.add(geneIdOrNameQuery.build(), genes.conjunction ? BooleanClause.Occur.MUST :
BooleanClause.Occur.SHOULD);
}
builder.add(genesBuilder.build(), BooleanClause.Occur.MUST);
}
}
示例6: prefixSearch
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
@Override
public ScoreDoc[] prefixSearch(String keywords) throws IOException {
if (StringUtils.isEmpty(keywords) || keywords.length() > appConfig.getKeywordMaxLength()) {
logger.error("empty keywords or over-length! {}", keywords);
return null;
}
Sort sort = new Sort(new SortField("downloadRank", SortField.INT, true));
Term nameFldTerm = new Term(fieldName, keywords);
PrefixQuery nameFldQuery = new PrefixQuery(nameFldTerm);
NumericRangeQuery<Integer> catalogQuery = NumericRangeQuery.newIntRange("catalog",
(int) EnumCatalog.SOFT.getCatalog(), (int) EnumCatalog.GAME.getCatalog(), true, true);
BooleanQuery booleanQuery = new BooleanQuery();
booleanQuery.add(catalogQuery, Occur.MUST);
booleanQuery.add(nameFldQuery, Occur.MUST);
TopDocs topDocs = quickTipsSearcher.search(booleanQuery, appConfig.getQuickTipsNum() * 2, sort);
ScoreDoc[] docs = topDocs.scoreDocs;
return docs;
}
示例7: prefixSearch
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
@Override
public ScoreDoc[] prefixSearch(String q) throws IOException {
if (StringUtils.isEmpty(q) || q.length() > appConfig.getKeywordMaxLength()) {
logger.error("empty keywords or over-length! {}", q);
return null;
}
final TopDocs[] rstTopDocs = new TopDocs[2];
final Query nameFldQuery = new PrefixQuery(new Term(NAME.getName(), q));
rstTopDocs[0] = indexSearcher.search(nameFldQuery, appConfig.getQuickTipsNum() * 2, sort);
final Query downLoadRankQuery = NumericRangeQuery.newIntRange(DOWNOLOAD_RANK.getName(), MIN_DOWNLOAD_RANK,
Integer.MAX_VALUE, true, false);
// 從下載量最高的1000條記錄中,再過濾符合關鍵字的記錄
rstTopDocs[1] = indexSearcher.search(downLoadRankQuery, MAX_TOP, sort);
TopDocs rst = TopDocsUtil.mergeDuplicateDocId(TopDocs.merge(sort, MAX_TOP + appConfig.getQuickTipsNum() * 2,
rstTopDocs));
if (rst != null) {
return rst.scoreDocs;
}
return null;
}
示例8: prefixSearch
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
@Override
public ScoreDoc[] prefixSearch(String q) throws IOException {
if (StringUtils.isEmpty(q) || q.length() > appConfig.getKeywordMaxLength()) {
logger.error("empty keywords or over-length! {}", q);
return null;
}
final TopDocs[] rstTopDocs = new TopDocs[2];
final Query nameFldQuery = new PrefixQuery(new Term(NAME.getName(), q));
rstTopDocs[0] = indexSearcher.search(nameFldQuery, appConfig.getQuickTipsNum() * 2, sort);
final Query downLoadRankQuery = NumericRangeQuery.newIntRange(DOWNOLOAD_RANK.getName(), MIN_DOWNLOAD_RANK, Integer.MAX_VALUE, true, false);
//從下載量最高的1000條記錄中,再過濾符合關鍵字的記錄
rstTopDocs[1] = indexSearcher.search(downLoadRankQuery, MAX_TOP, sort);
TopDocs rst = TopDocsUtil.mergeDuplicateDocId(TopDocs.merge(sort, MAX_TOP + appConfig.getQuickTipsNum() * 2, rstTopDocs));
if(rst != null) {
return rst.scoreDocs;
}
return null;
}
示例9: execute
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
@Override
public void execute(boolean interactive) throws CommandException {
logger.debug("Executing search command");
try (IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)))) {
IndexSearcher searcher = new IndexSearcher(reader);
QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer());
BooleanQuery booleanQuery = new BooleanQuery.Builder()
.add(new PrefixQuery(new Term("contributor", queryString)), BooleanClause.Occur.SHOULD)
.add(queryParser.parse(String.format("%s*", queryString)), BooleanClause.Occur.SHOULD).build();
doPagingSearch(new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8)), searcher,
booleanQuery, 10, interactive);
} catch (IOException | ParseException e) {
logger.error("Exception while executing search command.", e);
throw new CommandException("Error while executing command. Excution failed.", e);
}
}
示例10: testNoSuchMultiTermsInSpanFirst
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
public void testNoSuchMultiTermsInSpanFirst() throws Exception {
//this hasn't been a problem
FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
SpanQuery spanFirst = new SpanFirstQuery(spanNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
assertEquals(0, searcher.search(spanFirst, 10).totalHits);
}
示例11: getLOINCMatches
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
/**
* Enhancements: match any LOINC term (includes synonyms)? permit any loinc code (not just chemistries)
* @param search
* @return
*/
private Map<String,String> getLOINCMatches(String search) {
// try to match the search terms against a narrow portion of the LOINC higherarchy
BooleanQuery q1 = new BooleanQuery();
q1.add(new TermQuery(new Term("parent", LNC_CHEM)), BooleanClause.Occur.MUST);
q1.add(new PrefixQuery(new Term("term", search)), BooleanClause.Occur.MUST);
// match any exact loinc code
BooleanQuery q2 = new BooleanQuery();
q2.add(new TermQuery(new Term("sab", "lnc")), BooleanClause.Occur.MUST);
q2.add(new TermQuery(new Term("code", search)), BooleanClause.Occur.MUST);
BooleanQuery q = new BooleanQuery();
q.add(q1, BooleanClause.Occur.SHOULD);
q.add(q2, BooleanClause.Occur.SHOULD);
Map<String,String> ret = new HashMap<>();
for (String urn : eng.search(q)) {
String desc = eng.getDescription(urn);
ret.put(urn, desc);
}
return ret;
}
示例12: addTextAndImageLinks
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
private void addTextAndImageLinks(List<Link> links, DocumentIndex reindexingIndex, UserDomainObject user,
HttpServletRequest request, IntRange range) {
BooleanQuery query = new BooleanQuery();
query.add(new PrefixQuery(new Term(DocumentIndex.FIELD__NONSTRIPPED_TEXT, "http")), Occur.SHOULD);
query.add(new PrefixQuery(new Term(DocumentIndex.FIELD__NONSTRIPPED_TEXT, "href")), Occur.SHOULD);
query.add(new PrefixQuery(new Term(DocumentIndex.FIELD__IMAGE_LINK_URL, "http")), Occur.SHOULD);
List textDocuments = reindexingIndex.search(new SimpleDocumentQuery(query), user);
for (Object textDocument1 : textDocuments) {
TextDocumentDomainObject textDocument = (TextDocumentDomainObject) textDocument1;
if (!range.containsInteger(textDocument.getId())) {
continue;
}
addTextLinks(links, textDocument, request);
addImageLinks(links, textDocument, request);
}
}
示例13: rewrite
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
public static Query rewrite(org.apache.lucene.search.Query q, Set<String> intFields) {
if (q instanceof TermQuery) {
return rewrite((TermQuery)q, intFields);
} else if (q instanceof BooleanQuery) {
return rewrite((BooleanQuery)q, intFields);
} else if (q instanceof RangeQuery) {
return rewrite((RangeQuery)q, intFields);
} else if (q instanceof ConstantScoreRangeQuery) {
return rewrite((ConstantScoreRangeQuery)q, intFields);
} else if (q instanceof PrefixQuery) {
return rewrite((PrefixQuery)q, intFields);
} else if (q instanceof PhraseQuery) {
return rewrite((PhraseQuery)q, intFields);
}
throw new IllegalArgumentException("unsupported lucene query type: " + q.getClass().getSimpleName());
}
示例14: testString
import org.apache.lucene.search.PrefixQuery; //導入依賴的package包/類
@Test
public void testString() {
Map<String, ColumnMapper> map = new HashMap<>();
map.put("name", new ColumnMapperString());
Schema mappers = new Schema(map, null, EnglishAnalyzer.class.getName());
PrefixCondition prefixCondition = new PrefixCondition(0.5f, "name", "tr");
Query query = prefixCondition.query(mappers);
Assert.assertNotNull(query);
Assert.assertEquals(PrefixQuery.class, query.getClass());
PrefixQuery luceneQuery = (PrefixQuery) query;
Assert.assertEquals("name", luceneQuery.getField());
Assert.assertEquals("tr", luceneQuery.getPrefix().text());
Assert.assertEquals(0.5f, query.getBoost(), 0);
}
示例15: testInetV4
import org.apache.lucene.search.PrefixQuery; //導入依賴的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());
PrefixCondition wildcardCondition = new PrefixCondition(0.5f, "name", "192.168.");
Query query = wildcardCondition.query(mappers);
Assert.assertNotNull(query);
Assert.assertEquals(PrefixQuery.class, query.getClass());
PrefixQuery luceneQuery = (PrefixQuery) query;
Assert.assertEquals("name", luceneQuery.getField());
Assert.assertEquals("192.168.", luceneQuery.getPrefix().text());
Assert.assertEquals(0.5f, query.getBoost(), 0);
}