下面列出了org.apache.lucene.index.Term#field ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@CheckForNull
@Override
@SuppressWarnings("StringEquality")
public Void convert(@NonNull final Index.WithTermFrequencies.TermFreq param) throws Stop {
final Term term = param.getTerm();
if (fieldName != term.field()) {
throw new Stop();
}
final int docCount = param.getFreq();
final String encBinName = term.text();
final String binName = encBinName.substring(
0,
encBinName.length() - postfixLen);
final int dotIndex = binName.lastIndexOf('.'); //NOI18N
final String pkgName = dotIndex == -1 ? "" : binName.substring(0, dotIndex); //NOI18N
final Integer typeCount = typeFreq.get(binName);
final Integer pkgCount = pkgFreq.get(pkgName);
typeFreq.put(binName, typeCount == null ? docCount : docCount + typeCount);
pkgFreq.put(pkgName, pkgCount == null ? docCount : docCount + pkgCount);
return null;
}
/**
* Create a new FuzzyQuery that will match terms with an edit distance
* of at most <code>maxEdits</code> to <code>term</code>.
* If a <code>prefixLength</code> > 0 is specified, a common prefix
* of that length is also required.
*
* @param term the term to search for
* @param maxEdits must be {@code >= 0} and {@code <=} {@link LevenshteinAutomata#MAXIMUM_SUPPORTED_DISTANCE}.
* @param prefixLength length of common (non-fuzzy) prefix
* @param maxExpansions the maximum number of terms to match. If this number is
* greater than {@link IndexSearcher#getMaxClauseCount} when the query is rewritten,
* then the maxClauseCount will be used instead.
* @param transpositions true if transpositions should be treated as a primitive
* edit operation. If this is false, comparisons will implement the classic
* Levenshtein algorithm.
*/
public FuzzyQuery(Term term, int maxEdits, int prefixLength, int maxExpansions, boolean transpositions) {
super(term.field());
if (maxEdits < 0 || maxEdits > LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE) {
throw new IllegalArgumentException("maxEdits must be between 0 and " + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE);
}
if (prefixLength < 0) {
throw new IllegalArgumentException("prefixLength cannot be negative.");
}
if (maxExpansions <= 0) {
throw new IllegalArgumentException("maxExpansions must be positive.");
}
this.term = term;
this.maxEdits = maxEdits;
this.prefixLength = prefixLength;
this.transpositions = transpositions;
this.maxExpansions = maxExpansions;
setRewriteMethod(new MultiTermQuery.TopTermsBlendedFreqScoringRewrite(maxExpansions));
}
/** Adds a term to the end of the query phrase. */
public void add(Term term) {
if (terms.size() == 0)
field = term.field();
else if (term.field() != field)
throw new IllegalArgumentException
("All phrase terms must be in the same field: " + term);
terms.addElement(term);
}
/** "A B" ⊆ "A (B C)" */
public void testExactPhraseVersusMultiPhrase() throws Exception {
Term t1 = randomTerm();
Term t2 = randomTerm();
PhraseQuery q1 = new PhraseQuery(t1.field(), t1.bytes(), t2.bytes());
Term t3 = randomTerm();
MultiPhraseQuery.Builder q2b = new MultiPhraseQuery.Builder();
q2b.add(t1);
q2b.add(new Term[] { t2, t3 });
assertSubsetOf(q1, q2b.build());
}
/** "A B" ⊆ "A B"~1 */
public void testPhraseVersusSloppyPhrase() throws Exception {
Term t1 = randomTerm();
Term t2 = randomTerm();
PhraseQuery q1 = new PhraseQuery(t1.field(), t1.bytes(), t2.bytes());
PhraseQuery q2 = new PhraseQuery(1, t1.field(), t1.bytes(), t2.bytes());
assertSubsetOf(q1, q2);
}
/**
* Adds a single term at the next position in the phrase.
*/
public Builder addTerm(Term term) {
if (!term.field().equals(field)) {
throw new IllegalArgumentException(term.getClass().getSimpleName()
+ " field \"" + term.field() + "\" cannot be different from the "
+ PhraseWildcardQuery.class.getSimpleName() + " field \"" + field + "\"");
}
phraseTerms.add(new SingleTerm(term, phraseTerms.size()));
return this;
}
/** "A B" ⊆ (+A +B) */
public void testExactPhraseVersusBooleanAnd() throws Exception {
Term t1 = randomTerm();
Term t2 = randomTerm();
PhraseQuery q1 = new PhraseQuery(t1.field(), t1.bytes(), t2.bytes());
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
q2.add(new TermQuery(t1), Occur.MUST);
q2.add(new TermQuery(t2), Occur.MUST);
assertSubsetOf(q1, q2.build());
}
/**
* Create a {@link DisjunctionMatchesIterator} over a list of terms
*
* Only terms that have at least one match in the given document will be included
*/
static MatchesIterator fromTerms(LeafReaderContext context, int doc, Query query, String field, List<Term> terms) throws IOException {
Objects.requireNonNull(field);
for (Term term : terms) {
if (Objects.equals(field, term.field()) == false) {
throw new IllegalArgumentException("Tried to generate iterator from terms in multiple fields: expected [" + field + "] but got [" + term.field() + "]");
}
}
return fromTermsEnum(context, doc, query, field, asBytesRefIterator(terms));
}
/** "A B"~N ⊆ "A B"~N+1 */
public void testIncreasingSloppiness() throws Exception {
Term t1 = randomTerm();
Term t2 = randomTerm();
for (int i = 0; i < 10; i++) {
PhraseQuery q1 = new PhraseQuery(i, t1.field(), t1.bytes(), t2.bytes());
PhraseQuery q2 = new PhraseQuery(i + 1, t1.field(), t1.bytes(), t2.bytes());
assertSubsetOf(q1, q2);
}
}
/** "A B"~∞ = +A +B if A != B */
public void testSloppyPhraseVersusBooleanAnd() throws Exception {
Term t1 = randomTerm();
Term t2 = null;
// semantics differ from SpanNear: SloppyPhrase handles repeats,
// so we must ensure t1 != t2
do {
t2 = randomTerm();
} while (t1.equals(t2));
PhraseQuery q1 = new PhraseQuery(Integer.MAX_VALUE, t1.field(), t1.bytes(), t2.bytes());
BooleanQuery.Builder q2 = new BooleanQuery.Builder();
q2.add(new TermQuery(t1), Occur.MUST);
q2.add(new TermQuery(t2), Occur.MUST);
assertSameSet(q1, q2.build());
}
/** Adds a term to the end of the query phrase. */
public void add(Term term) {
if (terms.size() == 0)
field = term.field();
else if (term.field() != field)
throw new IllegalArgumentException
("All phrase terms must be in the same field: " + term);
terms.addElement(term);
}
/** "A A A"~N ⊆ "A A A"~N+1 */
public void testRepetitiveIncreasingSloppiness3() throws Exception {
Term t = randomTerm();
for (int i = 0; i < 10; i++) {
PhraseQuery q1 = new PhraseQuery(i, t.field(), t.bytes(), t.bytes(), t.bytes());
PhraseQuery q2 = new PhraseQuery(i + 1, t.field(), t.bytes(), t.bytes(), t.bytes());
assertSubsetOf(q1, q2);
assertSubsetOf(q1, q2);
}
}
/** Phrase positions are relative. */
public void testPhraseRelativePositions() throws Exception {
Term t1 = randomTerm();
Term t2 = randomTerm();
PhraseQuery q1 = new PhraseQuery(t1.field(), t1.bytes(), t2.bytes());
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.add(t1, 10000);
builder.add(t2, 10001);
PhraseQuery q2 = builder.build();
assertSameScores(q1, q2);
}
private String getKey(Term term) {
return term.field() + term.text();
}
/** Constructs a query for terms matching <code>term</code>. */
public CrateRegexQuery(Term term) {
super(term.field());
this.term = term;
this.flags = 0;
}
/** Constructs a query for terms matching <code>term</code>. */
public CrateRegexQuery(Term term, int flags) {
super(term.field());
this.term = term;
this.flags = flags;
}
DumbRegexpQuery(Term term, int flags) {
super(term.field());
RegExp re = new RegExp(term.text(), flags);
automaton = re.toAutomaton();
}
private static Query buildRandomQuery(int level) {
if (level == 10) {
// at most 10 levels
return new MatchAllDocsQuery();
}
switch (random().nextInt(6)) {
case 0:
return new TermQuery(randomTerm());
case 1:
BooleanQuery.Builder bq = new BooleanQuery.Builder();
final int numClauses = TestUtil.nextInt(random(), 1, 3);
int numShould = 0;
for (int i = 0; i < numClauses; ++i) {
final Occur occur = RandomPicks.randomFrom(random(), Occur.values());
bq.add(buildRandomQuery(level + 1), occur);
if (occur == Occur.SHOULD) {
numShould++;
}
}
bq.setMinimumNumberShouldMatch(TestUtil.nextInt(random(), 0, numShould));
return bq.build();
case 2:
Term t1 = randomTerm();
Term t2 = randomTerm();
PhraseQuery pq = new PhraseQuery(random().nextInt(2), t1.field(), t1.bytes(), t2.bytes());
return pq;
case 3:
return new MatchAllDocsQuery();
case 4:
return new ConstantScoreQuery(buildRandomQuery(level + 1));
case 5:
List<Query> disjuncts = new ArrayList<>();
final int numQueries = TestUtil.nextInt(random(), 1, 3);
for (int i = 0; i < numQueries; ++i) {
disjuncts.add(buildRandomQuery(level + 1));
}
return new DisjunctionMaxQuery(disjuncts, random().nextFloat());
default:
throw new AssertionError();
}
}
DumbPrefixQuery(Term term) {
super(term.field());
prefix = term.bytes();
}
/**
* Create a new AutomatonQuery from an {@link Automaton}.
*
* @param term Term containing field and possibly some pattern structure. The
* term text is ignored.
* @param automaton Automaton to run, terms that are accepted are considered a
* match.
* @param maxDeterminizedStates maximum number of states in the resulting
* automata. If the automata would need more than this many states
* TooComplextToDeterminizeException is thrown. Higher number require more
* space but can process more complex automata.
* @param isBinary if true, this automaton is already binary and
* will not go through the UTF32ToUTF8 conversion
*/
public AutomatonQuery(final Term term, Automaton automaton, int maxDeterminizedStates, boolean isBinary) {
super(term.field());
this.term = term;
this.automaton = automaton;
this.automatonIsBinary = isBinary;
// TODO: we could take isFinite too, to save a bit of CPU in CompiledAutomaton ctor?:
this.compiled = new CompiledAutomaton(automaton, null, true, maxDeterminizedStates, isBinary);
this.ramBytesUsed = BASE_RAM_BYTES + term.ramBytesUsed() + automaton.ramBytesUsed() + compiled.ramBytesUsed();
}