类org.apache.lucene.search.PrefixQuery源码实例Demo

下面列出了怎么用org.apache.lucene.search.PrefixQuery的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: netbeans   文件: DocumentUtil.java
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 项目: lucene-solr   文件: HighlighterTest.java
public void testGetBestFragmentsMultiTerm() throws Exception {
  TestHighlightRunner helper = new TestHighlightRunner() {

    @Override
    public void run() throws Exception {
      numHighlights = 0;
      BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
      booleanQuery.add(new TermQuery(new Term(FIELD_NAME, "john")), Occur.SHOULD);
      PrefixQuery prefixQuery = new PrefixQuery(new Term(FIELD_NAME, "kenn"));
      prefixQuery.setRewriteMethod(MultiTermQuery.SCORING_BOOLEAN_REWRITE);
      booleanQuery.add(prefixQuery, Occur.SHOULD);

      doSearching(booleanQuery.build());
      doStandardHighlights(analyzer, searcher, hits, query, HighlighterTest.this);
      assertTrue("Failed to find correct number of highlights " + numHighlights + " found",
          numHighlights == 5);
    }
  };

  helper.start();
}
 
源代码3 项目: lucene-solr   文件: TestUnifiedHighlighterMTQ.java
public void testRussianPrefixQuery() throws IOException {
  Analyzer analyzer = new StandardAnalyzer();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, analyzer);
  String field = "title";
  Document doc = new Document();
  doc.add(new Field(field, "я", fieldType)); // Russian char; uses 2 UTF8 bytes
  iw.addDocument(doc);
  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  Query query = new PrefixQuery(new Term(field, "я"));
  TopDocs topDocs = searcher.search(query, 1);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, analyzer);
  String[] snippets = highlighter.highlight(field, query, topDocs);
  assertEquals("[<b>я</b>]", Arrays.toString(snippets));
  ir.close();
}
 
源代码4 项目: lucene-solr   文件: TestHighlightingMatcher.java
public void testWildcardCombinations() throws Exception {

    final BooleanQuery bq = new BooleanQuery.Builder()
        .add(new TermQuery(new Term(FIELD, "term1")), BooleanClause.Occur.MUST)
        .add(new PrefixQuery(new Term(FIELD, "term2")), BooleanClause.Occur.MUST)
        .add(new TermQuery(new Term(FIELD, "term3")), BooleanClause.Occur.MUST_NOT)
        .build();

    try (Monitor monitor = newMonitor()) {
      monitor.register(new MonitorQuery("1", bq));

      MatchingQueries<HighlightsMatch> matches = monitor.match(buildDoc("term1 term22 term4"), HighlightsMatch.MATCHER);
      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(2, m.getHitCount());
    }

  }
 
源代码5 项目: lucene-solr   文件: TestSpans.java
public void testSpanNotWithMultiterm() throws Exception {
  SpanQuery q = spanNotQuery(
      spanTermQuery(field, "r1"),
      new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "s1"))),3,3);
  checkHits(q,  new int[] {14});

  q = spanNotQuery(
      spanTermQuery(field, "r1"),
      new SpanMultiTermQueryWrapper<>(new FuzzyQuery(new Term(field, "s12"), 1, 2)),3,3);
  checkHits(q,  new int[] {14});

  q = spanNotQuery(
      new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term(field, "r"))),
      spanTermQuery(field, "s21"),3,3);
  checkHits(q,  new int[] {13});


}
 
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.count(spanFirst));
  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  spanFirst = new SpanFirstQuery(spanWCNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  spanFirst = new SpanFirstQuery(spanRgxNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  spanFirst = new SpanFirstQuery(spanPrfxNoSuch, 10);
  assertEquals(0, searcher.count(spanFirst));
}
 
源代码7 项目: mtas   文件: MtasSpanPrefixQuery.java
/**
 * Instantiates a new mtas span prefix query.
 *
 * @param term the term
 * @param singlePosition the single position
 */
public MtasSpanPrefixQuery(Term term, boolean singlePosition) {
  super(singlePosition ? 1 : null, singlePosition ? 1 : null);
  PrefixQuery pfq = new PrefixQuery(term);
  query = new SpanMultiTermQueryWrapper<>(pfq);
  this.term = term;
  this.singlePosition = singlePosition;
  int i = term.text().indexOf(MtasToken.DELIMITER);
  if (i >= 0) {
    prefix = term.text().substring(0, i);
    value = term.text().substring((i + MtasToken.DELIMITER.length()));
    value = (value.length() > 0) ? value : null;
  } else {
    prefix = term.text();
    value = null;
  }
}
 
源代码8 项目: james-project   文件: LuceneMessageSearchIndex.java
private void update(MailboxId mailboxId, MessageUid uid, Flags f) throws IOException {
    try (IndexSearcher searcher = new IndexSearcher(IndexReader.open(writer, true))) {
        BooleanQuery query = new BooleanQuery();
        query.add(new TermQuery(new Term(MAILBOX_ID_FIELD, mailboxId.serialize())), BooleanClause.Occur.MUST);
        query.add(createQuery(MessageRange.one(uid)), BooleanClause.Occur.MUST);
        query.add(new PrefixQuery(new Term(FLAGS_FIELD, "")), BooleanClause.Occur.MUST);

        TopDocs docs = searcher.search(query, 100000);
        ScoreDoc[] sDocs = docs.scoreDocs;
        for (ScoreDoc sDoc : sDocs) {
            Document doc = searcher.doc(sDoc.doc);

            doc.removeFields(FLAGS_FIELD);
            indexFlags(doc, f);

            writer.updateDocument(new Term(ID_FIELD, doc.get(ID_FIELD)), doc);
        }
    }
}
 
源代码9 项目: james-project   文件: LuceneMessageSearchIndex.java
private Flags retrieveFlags(Mailbox mailbox, MessageUid uid) throws IOException {
    try (IndexSearcher searcher = new IndexSearcher(IndexReader.open(writer, true))) {
        Flags retrievedFlags = new Flags();

        BooleanQuery query = new BooleanQuery();
        query.add(new TermQuery(new Term(MAILBOX_ID_FIELD, mailbox.getMailboxId().serialize())), BooleanClause.Occur.MUST);
        query.add(createQuery(MessageRange.one(uid)), BooleanClause.Occur.MUST);
        query.add(new PrefixQuery(new Term(FLAGS_FIELD, "")), BooleanClause.Occur.MUST);

        TopDocs docs = searcher.search(query, 100000);
        ScoreDoc[] sDocs = docs.scoreDocs;
        for (ScoreDoc sDoc : sDocs) {
            Document doc = searcher.doc(sDoc.doc);

            Stream.of(doc.getValues(FLAGS_FIELD))
                .forEach(flag -> fromString(flag).ifPresentOrElse(retrievedFlags::add, () -> retrievedFlags.add(flag)));
        }
        return retrievedFlags;
    }
}
 
源代码10 项目: imhotep   文件: LuceneQueryTranslator.java
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());
}
 
源代码11 项目: stratio-cassandra   文件: PrefixConditionTest.java
@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);
}
 
源代码12 项目: stratio-cassandra   文件: PrefixConditionTest.java
@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);
}
 
源代码13 项目: stratio-cassandra   文件: PrefixConditionTest.java
@Test
public void testInetV6() {

    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", "2001:db8:2de:0:0:0:0:e");
    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("2001:db8:2de:0:0:0:0:e", luceneQuery.getPrefix().text());
    Assert.assertEquals(0.5f, query.getBoost(), 0);
}
 
源代码14 项目: fess   文件: QueryHelper.java
protected QueryBuilder convertPrefixQuery(final QueryContext context, final PrefixQuery prefixQuery, final float boost) {
    final String field = getSearchField(context, prefixQuery.getField());
    if (Constants.DEFAULT_FIELD.equals(field)) {
        context.addFieldLog(field, prefixQuery.getPrefix().text());
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhrasePrefixQuery(f,
                toLowercaseWildcard(prefixQuery.getPrefix().text())).boost(b * boost));
    } else if (isSearchField(field)) {
        context.addFieldLog(field, prefixQuery.getPrefix().text());
        if (notAnalyzedFieldSet.contains(field)) {
            return QueryBuilders.prefixQuery(field, toLowercaseWildcard(prefixQuery.getPrefix().text())).boost(boost);
        } else {
            return QueryBuilders.matchPhrasePrefixQuery(field, toLowercaseWildcard(prefixQuery.getPrefix().text())).boost(boost);
        }
    } else {
        final String query = prefixQuery.getPrefix().toString();
        final String origQuery = toLowercaseWildcard(query);
        context.addFieldLog(Constants.DEFAULT_FIELD, query);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhrasePrefixQuery(f, origQuery).boost(b * boost));
    }
}
 
源代码15 项目: jstarcraft-core   文件: LuceneQueryTestCase.java
@Test
public void testPrefixQuery() throws Exception {
    // 前缀查询
    PrefixQuery query = new PrefixQuery(new Term("title", "Touc"));
    TopDocs search = searcher.search(query, 1000);
    Assert.assertEquals(2, search.totalHits.value);
}
 
源代码16 项目: Elasticsearch   文件: TypeFieldMapper.java
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (indexOptions() == IndexOptions.NONE) {
        return new ConstantScoreQuery(new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))));
    }
    return new ConstantScoreQuery(new TermQuery(createTerm(value)));
}
 
/**
 * Test it does *not* highlight the same term's not next to the span-near.  "charlie" in this case.
 * This particular example exercises "Rewrite" plus "MTQ" in the same query.
 */
public void testRewriteAndMtq() throws IOException {
  indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
  initReaderSearcherHighlighter();

  SpanNearQuery snq = new SpanNearQuery(
      new SpanQuery[]{
          new SpanTermQuery(new Term("body", "bravo")),
          new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch")))}, // REWRITES
      0, true);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(snq, BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "al")), BooleanClause.Occur.MUST) // MTQ
      .add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST)
      // add queries for other fields; we shouldn't highlight these because of that.
      .add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);

  if (highlighter.getFlags("body").contains(HighlightFlag.WEIGHT_MATCHES)) {
    assertArrayEquals(new String[]{"<b>alpha bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  } else {
    assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  }

  // do again, this time with MTQ disabled.  We should only find "alpha bravo".
  highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setHandleMultiTermQuery(false);//disable but leave phrase processing enabled

  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  snippets = highlighter.highlight("body", query, topDocs);

  assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha"},
      snippets);
}
 
/**
   * Like {@link #testRewriteAndMtq} but no freestanding MTQ
   */
  public void testRewrite() throws IOException {
    indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
    initReaderSearcherHighlighter();

    SpanNearQuery snq = new SpanNearQuery(
        new SpanQuery[]{
            new SpanTermQuery(new Term("body", "bravo")),
            new SpanMultiTermQueryWrapper<>(new PrefixQuery(new Term("body", "ch")))}, // REWRITES
        0, true);
    BooleanQuery query = new BooleanQuery.Builder()
        .add(snq, BooleanClause.Occur.MUST)
//          .add(new PrefixQuery(new Term("body", "al")), BooleanClause.Occur.MUST) // MTQ
        .add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST)
        // add queries for other fields; we shouldn't highlight these because of that.
        .add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD)
        .build();

    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    String[] snippets = highlighter.highlight("body", query, topDocs);

    if (highlighter.getFlags("body").contains(HighlightFlag.WEIGHT_MATCHES)) {
      assertArrayEquals(new String[]{"<b>alpha bravo</b> <b>charlie</b> - charlie bravo alpha"}, snippets);
    } else {
      assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo alpha"}, snippets);
    }

    // do again, this time with MTQ disabled.  We should only find "alpha bravo".
    highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
    highlighter.setHandleMultiTermQuery(false);//disable but leave phrase processing enabled

    topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    snippets = highlighter.highlight("body", query, topDocs);

    assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> charlie - charlie bravo alpha"},
        snippets);
  }
 
/**
 * Like {@link #testRewriteAndMtq} but no rewrite.
 */
public void testMtq() throws IOException {
  indexWriter.addDocument(newDoc("alpha bravo charlie - charlie bravo alpha"));
  initReaderSearcherHighlighter();

  SpanNearQuery snq = new SpanNearQuery(
      new SpanQuery[]{
          new SpanTermQuery(new Term("body", "bravo")),
          new SpanTermQuery(new Term("body", "charlie"))}, // does NOT rewrite
      0, true);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(snq, BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "al")), BooleanClause.Occur.MUST) // MTQ
      .add(newPhraseQuery("body", "alpha bravo"), BooleanClause.Occur.MUST)
      // add queries for other fields; we shouldn't highlight these because of that.
      .add(newPhraseQuery("title", "bravo alpha"), BooleanClause.Occur.SHOULD)
      .build();

  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String[] snippets = highlighter.highlight("body", query, topDocs);

  if (highlighter.getFlags("body").contains(HighlightFlag.WEIGHT_MATCHES)) {
    assertArrayEquals(new String[]{"<b>alpha bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  } else {
    assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo <b>alpha</b>"}, snippets);
  }

  // do again, this time with MTQ disabled.
  highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setHandleMultiTermQuery(false);//disable but leave phrase processing enabled

  topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  snippets = highlighter.highlight("body", query, topDocs);

  //note: without MTQ, the WEIGHT_MATCHES is disabled which affects the snippet boundaries
  assertArrayEquals(new String[]{"<b>alpha</b> <b>bravo</b> <b>charlie</b> - charlie bravo alpha"},
      snippets);
}
 
源代码20 项目: lucene-solr   文件: TestUnifiedHighlighterMTQ.java
public void testWithMaxLen() throws IOException {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("Alpha Bravo foo foo foo. Foo foo Alpha Bravo");//44 char long, 2 sentences
  iw.addDocument(doc);

  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setMaxLength(25);//a little past first sentence

  BooleanQuery query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("body", "alpha")), BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "bra")), BooleanClause.Occur.MUST)
      .build();
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String snippets[] = highlighter.highlight("body", query, topDocs, 2);//ask for 2 but we'll only get 1
  assertArrayEquals(
      new String[]{"<b>Alpha</b> <b>Bravo</b> foo foo foo. "}, snippets
  );

  ir.close();
}
 
源代码21 项目: lucene-solr   文件: TestUnifiedHighlighterMTQ.java
public void testWithMaxLenAndMultipleWildcardMatches() throws IOException {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  //tests interleaving of multiple wildcard matches with the CompositePostingsEnum
  //In this case the CompositePostingsEnum will have an underlying PostingsEnum that jumps form pos 1 to 9 for bravo
  //and a second with position 2 for Bravado
  body.setStringValue("Alpha Bravo Bravado foo foo foo. Foo foo Alpha Bravo");
  iw.addDocument(doc);

  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setMaxLength(32);//a little past first sentence

  BooleanQuery query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("body", "alpha")), BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "bra")), BooleanClause.Occur.MUST)
      .build();
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String snippets[] = highlighter.highlight("body", query, topDocs, 2);//ask for 2 but we'll only get 1
  assertArrayEquals(
      new String[]{"<b>Alpha</b> <b>Bravo</b> <b>Bravado</b> foo foo foo."}, snippets
  );

  ir.close();
}
 
源代码22 项目: lucene-solr   文件: AnalyzingInfixSuggester.java
/** This is called if the last token isn't ended
 *  (e.g. user did not type a space after it).  Return an
 *  appropriate Query clause to add to the BooleanQuery. */
protected Query getLastTokenQuery(String token) throws IOException {
  if (token.length() < minPrefixChars) {
    // The leading ngram was directly indexed:
    return new TermQuery(new Term(TEXTGRAMS_FIELD_NAME, token));
  }

  return new PrefixQuery(new Term(TEXT_FIELD_NAME, token));
}
 
源代码23 项目: lucene-solr   文件: TestHighlightingMatcher.java
public void testDisjunctionMaxQuery() throws IOException {
  final DisjunctionMaxQuery query = new DisjunctionMaxQuery(Arrays.asList(
      new TermQuery(new Term(FIELD, "term1")), new PrefixQuery(new Term(FIELD, "term2"))
  ), 1.0f);

  try (Monitor monitor = newMonitor()) {
    monitor.register(new MonitorQuery("1", query));
    MatchingQueries<HighlightsMatch> matches = monitor.match(buildDoc("term1 term2 term3"), HighlightsMatch.MATCHER);
    HighlightsMatch m = matches.matches("1");
    assertNotNull(m);
    assertEquals(2, m.getHitCount());
  }

}
 
源代码24 项目: lucene-solr   文件: TestHighlightingMatcher.java
public void testWildcardBooleanRewrites() throws Exception {

    final Query wc = new PrefixQuery(new Term(FIELD, "term1"));

    final Query wrapper = new BooleanQuery.Builder()
        .add(wc, BooleanClause.Occur.MUST)
        .build();

    final Query wrapper2 = new BooleanQuery.Builder()
        .add(wrapper, BooleanClause.Occur.MUST)
        .build();

    final BooleanQuery bq = new BooleanQuery.Builder()
        .add(new PrefixQuery(new Term(FIELD, "term2")), BooleanClause.Occur.MUST)
        .add(wrapper2, BooleanClause.Occur.MUST_NOT)
        .build();

    try (Monitor monitor = new Monitor(ANALYZER)) {

      monitor.register(new MonitorQuery("1", bq));
      MatchingQueries<HighlightsMatch> matches = monitor.match(buildDoc("term2 term"), HighlightsMatch.MATCHER);
      HighlightsMatch m = matches.matches("1");
      assertNotNull(m);
      assertEquals(1, m.getHitCount());

      matches = monitor.match(buildDoc("term2 term"), HighlightsMatch.MATCHER);
      m = matches.matches("1");
      assertNotNull(m);
      assertEquals(1, m.getHitCount());
    }
  }
 
@Override
public PrefixQuery build(QueryNode queryNode) throws QueryNodeException {    

  PrefixWildcardQueryNode wildcardNode = (PrefixWildcardQueryNode) queryNode;

  String text = wildcardNode.getText().subSequence(0, wildcardNode.getText().length() - 1).toString();
  PrefixQuery q = new PrefixQuery(new Term(wildcardNode.getFieldAsString(), text));
  
  MultiTermQuery.RewriteMethod method = (MultiTermQuery.RewriteMethod)queryNode.getTag(MultiTermRewriteMethodProcessor.TAG_ID);
  if (method != null) {
    q.setRewriteMethod(method);
  }
  
  return q;
}
 
源代码26 项目: lucene-solr   文件: TestQPHelper.java
public void testConstantScoreAutoRewrite() throws Exception {
  StandardQueryParser qp = new StandardQueryParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
  Query q = qp.parse("foo*bar", "field");
  assertTrue(q instanceof WildcardQuery);
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());

  q = qp.parse("foo*", "field");
  assertTrue(q instanceof PrefixQuery);
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());

  q = qp.parse("[a TO z]", "field");
  assertTrue(q instanceof TermRangeQuery);
  assertEquals(MultiTermQuery.CONSTANT_SCORE_REWRITE, ((MultiTermQuery) q).getRewriteMethod());
}
 
public void testNoSuchMultiTermsInNear() throws Exception {
  //test to make sure non existent multiterms aren't throwing null pointer exceptions  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
  SpanQuery near = new SpanNearQuery(new SpanQuery[]{term, spanNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));
  //flip order
  near = new SpanNearQuery(new SpanQuery[]{spanNoSuch, term}, 1, true);
  assertEquals(0, searcher.count(near));
  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  near = new SpanNearQuery(new SpanQuery[]{term, spanWCNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  near = new SpanNearQuery(new SpanQuery[]{term, spanRgxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  near = new SpanNearQuery(new SpanQuery[]{term, spanPrfxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));

  //test single noSuch
  near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));
  
  //test double noSuch
  near = new SpanNearQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch}, 1, true);
  assertEquals(0, searcher.count(near));

}
 
public void testNoSuchMultiTermsInNotNear() throws Exception {
  //test to make sure non existent multiterms aren't throwing non-matching field exceptions  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
  SpanNotQuery notNear = new SpanNotQuery(term, spanNoSuch, 0,0);
  assertEquals(1, searcher.count(notNear));

  //flip
  notNear = new SpanNotQuery(spanNoSuch, term, 0,0);
  assertEquals(0, searcher.count(notNear));
  
  //both noSuch
  notNear = new SpanNotQuery(spanNoSuch, spanNoSuch, 0,0);
  assertEquals(0, searcher.count(notNear));

  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  notNear = new SpanNotQuery(term, spanWCNoSuch, 0,0);
  assertEquals(1, searcher.count(notNear));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  notNear = new SpanNotQuery(term, spanRgxNoSuch, 1, 1);
  assertEquals(1, searcher.count(notNear));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  notNear = new SpanNotQuery(term, spanPrfxNoSuch, 1, 1);
  assertEquals(1, searcher.count(notNear));
  
}
 
public void testNoSuchMultiTermsInOr() throws Exception {
  //test to make sure non existent multiterms aren't throwing null pointer exceptions  
  FuzzyQuery fuzzyNoSuch = new FuzzyQuery(new Term("field", "noSuch"), 1, 0, 1, false);
  SpanQuery spanNoSuch = new SpanMultiTermQueryWrapper<>(fuzzyNoSuch);
  SpanQuery term = new SpanTermQuery(new Term("field", "brown"));
  SpanOrQuery near = new SpanOrQuery(new SpanQuery[]{term, spanNoSuch});
  assertEquals(1, searcher.count(near));
  
  //flip
  near = new SpanOrQuery(new SpanQuery[]{spanNoSuch, term});
  assertEquals(1, searcher.count(near));

  
  WildcardQuery wcNoSuch = new WildcardQuery(new Term("field", "noSuch*"));
  SpanQuery spanWCNoSuch = new SpanMultiTermQueryWrapper<>(wcNoSuch);
  near = new SpanOrQuery(new SpanQuery[]{term, spanWCNoSuch});
  assertEquals(1, searcher.count(near));

  RegexpQuery rgxNoSuch = new RegexpQuery(new Term("field", "noSuch"));
  SpanQuery spanRgxNoSuch = new SpanMultiTermQueryWrapper<>(rgxNoSuch);
  near = new SpanOrQuery(new SpanQuery[]{term, spanRgxNoSuch});
  assertEquals(1, searcher.count(near));
  
  PrefixQuery prfxNoSuch = new PrefixQuery(new Term("field", "noSuch"));
  SpanQuery spanPrfxNoSuch = new SpanMultiTermQueryWrapper<>(prfxNoSuch);
  near = new SpanOrQuery(new SpanQuery[]{term, spanPrfxNoSuch});
  assertEquals(1, searcher.count(near));
  
  near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch});
  assertEquals(0, searcher.count(near));
  
  near = new SpanOrQuery(new SpanQuery[]{spanPrfxNoSuch, spanPrfxNoSuch});
  assertEquals(0, searcher.count(near));

}
 
@Test
public void testWrappedQueryIsNotModified() {
  final PrefixQuery pq = new PrefixQuery(new Term("field", "test"));
  int pqHash = pq.hashCode();
  SpanMultiTermQueryWrapper<PrefixQuery> wrapper = new SpanMultiTermQueryWrapper<>(pq);
  assertEquals(pqHash, pq.hashCode());
  wrapper.setRewriteMethod(new SpanMultiTermQueryWrapper.SpanRewriteMethod() {
    @Override
    public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
      return null;
    }
  });
  assertEquals(pqHash, pq.hashCode());
}
 
 类所在包
 同包方法