Java 类org.apache.lucene.search.TermQuery 实例源码

项目:RedisDirectory    文件:TestLucene.java   
public void testMMapDirectory() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    FSDirectory open = FSDirectory.open(Paths.get("E:/testlucene"));
    IndexWriter indexWriter = new IndexWriter(open, indexWriterConfig);
    for (int i = 0; i < 10000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    long end = System.currentTimeMillis();
    log.error("MMapDirectory consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(open));
    int total = 0;
    for (int i = 0; i < 10000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("MMapDirectory search consumes {}ms!", (end - start));
}
项目:incubator-netbeans    文件:DocumentUtil.java   
static Query binaryNameQuery (final String resourceName) {
    final BooleanQuery query = new BooleanQuery ();
    int index = resourceName.lastIndexOf(BinaryName.PKG_SEPARATOR);  // NOI18N
    String pkgName, sName;
    if (index < 0) {
        pkgName = "";   // NOI18N
        sName = resourceName;
    }
    else {
        pkgName = resourceName.substring(0,index);
        sName = resourceName.substring(index+1);
    }
    sName = sName + WILDCARD_QUERY_WILDCARD;   //Type of type element (Enum, Class, Interface, Annotation)
    query.add (new TermQuery (new Term (FIELD_PACKAGE_NAME, pkgName)),BooleanClause.Occur.MUST);
    query.add (new WildcardQuery (new Term (FIELD_BINARY_NAME, sName)),BooleanClause.Occur.MUST);
    return query;
}
项目:Elasticsearch    文件:PercolatorQueriesRegistry.java   
private int loadQueries(IndexShard shard) {
    shard.refresh("percolator_load_queries");
    // NOTE: we acquire the searcher via the engine directly here since this is executed right
    // before the shard is marked as POST_RECOVERY
    try (Engine.Searcher searcher = shard.engine().acquireSearcher("percolator_load_queries")) {
        Query query = new TermQuery(new Term(TypeFieldMapper.NAME, PercolatorService.TYPE_NAME));
        QueriesLoaderCollector queryCollector = new QueriesLoaderCollector(PercolatorQueriesRegistry.this, logger, mapperService, indexFieldDataService);
        IndexSearcher indexSearcher = new IndexSearcher(searcher.reader());
        indexSearcher.setQueryCache(null);
        indexSearcher.search(query, queryCollector);
        Map<BytesRef, Query> queries = queryCollector.queries();
        for (Map.Entry<BytesRef, Query> entry : queries.entrySet()) {
            Query previousQuery = percolateQueries.put(entry.getKey(), entry.getValue());
            shardPercolateService.addedQuery(entry.getKey(), previousQuery, entry.getValue());
        }
        return queries.size();
    } catch (Exception e) {
        throw new PercolatorException(shardId.index(), "failed to load queries from percolator index", e);
    }
}
项目:incubator-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;
}
项目:oscm    文件:SearchServiceBean.java   
/**
 * Returns the Lucene query for the given locale and query text.
 * 
 * @param searchString
 *            the text query for the Lucene query parser
 * @param mId
 *            the marketplace id
 * @param locale
 *            the locale for the analyzer to use
 * @param isDefaultLocaleHandling
 * @return the Lucene query for the given locale and query text
 * @throws ParseException
 *             in case the query cannot be parsed
 */
private org.apache.lucene.search.Query getLuceneQuery(String searchString,
        String mId, String locale, boolean isDefaultLocaleHandling)
        throws SyntaxError, QueryNodeException {

    // construct wildcard query for the actual search part
    org.apache.lucene.search.Query textQuery = LuceneQueryBuilder
            .getServiceQuery(searchString, locale, DEFAULT_LOCALE,
                    isDefaultLocaleHandling);

    // build mId part
    TermQuery mIdQuery = new TermQuery(new Term(ProductClassBridge.MP_ID,
            QueryParser.escape(mId).toLowerCase()));

    // now construct final query
    BooleanQuery query = new BooleanQuery();
    query.add(mIdQuery, Occur.MUST);
    query.add(textQuery, Occur.MUST);
    return query;
}
项目:InComb    文件:NewsDao.java   
/**
 * Returns all {@link News} of the given news group except the {@link News} with the given id.
 * @param newsGroupId the news group id
 * @param exceptId the news which should not be returned
 * @return a {@link List} with all {@link News} of the requested news group except the {@link News} with the exceptId.
 */
private List<News> getNewsOfNewsGroup(final long newsGroupId, final long exceptId) {
    final BooleanQuery query = new BooleanQuery();

    QueryUtil.addTypeConf(query, NewsIndexType.getInstance());

    final NumericRangeQuery<Long> groupQuery = NumericRangeQuery.newLongRange(
            NewsIndexType.FIELD_NEWSGROUPID, newsGroupId, newsGroupId, true, true);
    query.add(groupQuery, Occur.MUST);

    // exclude news
    query.add(new TermQuery(new Term(IIndexElement.FIELD_ID, String.valueOf(exceptId))), Occur.MUST_NOT);

    final SearchOptions options = new SearchOptions();
    options.setSort(new Sort(ESortField.PUBLISH_DATE.getSortField(ESortOrder.DESC)));

    final DocumentsSearchResult result = IndexSearch.getInstance().search(query, options);
    return NewsIndexType.docsToNews(result.getResults());
}
项目:InComb    文件:IndexingThreadTest.java   
private void checkIndexContent(final String elementId,
        final String fieldContent, final int expectedAmount) throws IOException {
    final IndexReader reader = IndexManager.getInstance().getIndex().getIndexReader();
    final IndexSearcher searcher = new IndexSearcher(reader);
    final TopDocs topDocs = searcher.search(new TermQuery(new Term(FIELDNAME, fieldContent)), expectedAmount + 10);

    assertNotNull(topDocs);
    assertTrue(topDocs.totalHits == expectedAmount);

    if(expectedAmount > 0) {
        final ScoreDoc scoreDoc = topDocs.scoreDocs[0];
        assertNotNull(scoreDoc);

        final Document doc = reader.document(scoreDoc.doc);
        assertNotNull(doc);
        assertEquals(fieldContent, doc.get(FIELDNAME));
        assertEquals(elementId, doc.get(IIndexElement.FIELD_ID));
        assertEquals(INDEX_TYPE, doc.get(IIndexElement.FIELD_INDEX_TYPE));
    }
}
项目:Mastering-Java-EE-Development-with-WildFly    文件:SearchTestCase.java   
private List<Post> findPosts() {
    try {
        FullTextSession fullTextSession = getFullTextSession((Session) entityManager.getDelegate());
        Builder builder = new Builder();
        String[] fields = new String[] { "message.text", "topic.subject" };
        MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
        builder.add(parser.parse(POST_TEXT), MUST);
        builder.add(new TermQuery(new Term("topic.forum.id", "0")), MUST);
        builder.add(new TermQuery(new Term("topic.forum.category.id", "0")), MUST);
        builder.add(new WildcardQuery(new Term("poster.userId", "root")), MUST);
        addPostTimeQuery(builder);
        FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Post.class);
        fullTextQuery.setSort(getSort());
        fullTextQuery.setFirstResult(0);
        fullTextQuery.setMaxResults(15);
        @SuppressWarnings("unchecked")
        List<Post> posts = fullTextQuery.list();
        return posts;
    } catch (ParseException e) {
        logger.severe("error");
        return null;
    }
}
项目:RedisDirectory    文件:TestLucene.java   
public void testRamDirectory() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    RAMDirectory ramDirectory = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(ramDirectory, indexWriterConfig);
    for (int i = 0; i < 10000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    long end = System.currentTimeMillis();
    log.error("RamDirectory consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(ramDirectory));
    int total = 0;
    for (int i = 0; i < 10000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("RamDirectory search consumes {}ms!", (end - start));
}
项目:elasticsearch_my    文件:VectorHighlighterTests.java   
public void testVectorHighlighter() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));

    Document document = new Document();
    document.add(new TextField("_id", "1", Field.Store.YES));
    FieldType vectorsType = new FieldType(TextField.TYPE_STORED);
    vectorsType.setStoreTermVectors(true);
    vectorsType.setStoreTermVectorPositions(true);
    vectorsType.setStoreTermVectorOffsets(true);
    document.add(new Field("content", "the big bad dog", vectorsType));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

    assertThat(topDocs.totalHits, equalTo(1));

    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
            reader, topDocs.scoreDocs[0].doc, "content", 30);
    assertThat(fragment, notNullValue());
    assertThat(fragment, equalTo("the big <b>bad</b> dog"));
}
项目:elasticsearch_my    文件:SimpleLuceneTests.java   
public void testSimpleNumericOps() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));

    Document document = new Document();
    document.add(new TextField("_id", "1", Field.Store.YES));
    document.add(new LegacyIntField("test", 2, LegacyIntField.TYPE_STORED));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);
    Document doc = searcher.doc(topDocs.scoreDocs[0].doc);
    IndexableField f = doc.getField("test");
    assertThat(f.stringValue(), equalTo("2"));

    BytesRefBuilder bytes = new BytesRefBuilder();
    LegacyNumericUtils.intToPrefixCoded(2, 0, bytes);
    topDocs = searcher.search(new TermQuery(new Term("test", bytes.get())), 1);
    doc = searcher.doc(topDocs.scoreDocs[0].doc);
    f = doc.getField("test");
    assertThat(f.stringValue(), equalTo("2"));

    indexWriter.close();
}
项目:elasticsearch_my    文件:MultiMatchQueryTests.java   
public void testCrossFieldMultiMatchQuery() throws IOException {
    QueryShardContext queryShardContext = indexService.newQueryShardContext(
            randomInt(20), null, () -> { throw new UnsupportedOperationException(); });
    queryShardContext.setAllowUnmappedFields(true);
    Query parsedQuery = multiMatchQuery("banon").field("name.first", 2).field("name.last", 3).field("foobar").type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
    try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        Query rewrittenQuery = searcher.searcher().rewrite(parsedQuery);

        BooleanQuery.Builder expected = new BooleanQuery.Builder();
        expected.add(new TermQuery(new Term("foobar", "banon")), BooleanClause.Occur.SHOULD);
        Query tq1 = new BoostQuery(new TermQuery(new Term("name.first", "banon")), 2);
        Query tq2 = new BoostQuery(new TermQuery(new Term("name.last", "banon")), 3);
        expected.add(new DisjunctionMaxQuery(Arrays.<Query>asList(tq1, tq2), 0f), BooleanClause.Occur.SHOULD);
        assertEquals(expected.build(), rewrittenQuery);
    }
}
项目:elasticsearch_my    文件:ParentIdQueryBuilder.java   
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    DocumentMapper childDocMapper = context.getMapperService().documentMapper(type);
    if (childDocMapper == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]");
        }
    }
    ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
    if (parentFieldMapper.active() == false) {
        throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured");
    }
    String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type());

    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST);
    // Need to take child type into account, otherwise a child doc of different type with the same id could match
    query.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER);
    return query.build();
}
项目:elasticsearch_my    文件:InternalEngine.java   
/**
 * Asserts that the doc in the index operation really doesn't exist
 */
private boolean assertDocDoesNotExist(final Index index, final boolean allowDeleted) throws IOException {
    final VersionValue versionValue = versionMap.getUnderLock(index.uid());
    if (versionValue != null) {
        if (versionValue.delete() == false || allowDeleted == false) {
            throw new AssertionError("doc [" + index.type() + "][" + index.id() + "] exists in version map (version " + versionValue + ")");
        }
    } else {
        try (Searcher searcher = acquireSearcher("assert doc doesn't exist")) {
            final long docsWithId = searcher.searcher().count(new TermQuery(index.uid()));
            if (docsWithId > 0) {
                throw new AssertionError("doc [" + index.type() + "][" + index.id() + "] exists [" + docsWithId + "] times in index");
            }
        }
    }
    return true;
}
项目:dremio-oss    文件:TestLuceneIndexer.java   
@Override
public void run() {
  int i = 0;
  while (i < 10000) {
    try {
      if (data.size() <= i) {
        sleep(1);
        continue;
      }
      final String key = "key" + i;
      final String val = "value" + i;
      final List<Document> documents = index.searchForDocuments(new TermQuery(new Term(key, val)), 10, new Sort(new SortField(key, SortField.Type.STRING)));
      if (documents.size() != 1) {
        throw new RuntimeException("Invalid number of matching documents for " + key + ", found " + documents);
      }
      ++i;
    } catch (IOException ioe) {
      error = ioe;
      break;
    } catch (InterruptedException e) {
    } catch (AlreadyClosedException ace) {
      error = ace;
      break;
    }
  }
}
项目:elasticsearch_my    文件:QueryProfilerTests.java   
public void testBasic() throws IOException {
    QueryProfiler profiler = new QueryProfiler();
    searcher.setProfiler(profiler);
    Query query = new TermQuery(new Term("foo", "bar"));
    searcher.search(query, 1);
    List<ProfileResult> results = profiler.getTree();
    assertEquals(1, results.size());
    Map<String, Long> breakdown = results.get(0).getTimeBreakdown();
    assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.ADVANCE.toString()).longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.SCORE.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.MATCH.toString()).longValue(), equalTo(0L));

    assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.ADVANCE.toString() + "_count").longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.SCORE.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.MATCH.toString() + "_count").longValue(), equalTo(0L));

    long rewriteTime = profiler.getRewriteTime();
    assertThat(rewriteTime, greaterThan(0L));
}
项目:elasticsearch_my    文件:QueryProfilerTests.java   
public void testNoScoring() throws IOException {
    QueryProfiler profiler = new QueryProfiler();
    searcher.setProfiler(profiler);
    Query query = new TermQuery(new Term("foo", "bar"));
    searcher.search(query, 1, Sort.INDEXORDER); // scores are not needed
    List<ProfileResult> results = profiler.getTree();
    assertEquals(1, results.size());
    Map<String, Long> breakdown = results.get(0).getTimeBreakdown();
    assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString()).longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.ADVANCE.toString()).longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.SCORE.toString()).longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.MATCH.toString()).longValue(), equalTo(0L));

    assertThat(breakdown.get(QueryTimingType.CREATE_WEIGHT.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.BUILD_SCORER.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.NEXT_DOC.toString() + "_count").longValue(), greaterThan(0L));
    assertThat(breakdown.get(QueryTimingType.ADVANCE.toString() + "_count").longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.SCORE.toString() + "_count").longValue(), equalTo(0L));
    assertThat(breakdown.get(QueryTimingType.MATCH.toString() + "_count").longValue(), equalTo(0L));

    long rewriteTime = profiler.getRewriteTime();
    assertThat(rewriteTime, greaterThan(0L));
}
项目:Equella    文件:ItemSyncer.java   
@Override
public Collection<IndexedItem> search(IndexSearcher searcher) throws IOException
{
    BooleanQuery overall = new BooleanQuery();
    BooleanQuery collections = new BooleanQuery();
    for( Institution inst : institutions )
    {
        collections.add(
            new TermQuery(new Term(FreeTextQuery.FIELD_INSTITUTION, Long.toString(inst.getUniqueId()))),
            Occur.SHOULD);
    }
    overall.add(collections, Occur.MUST);
    overall.add(NumericRangeQuery.newLongRange(FreeTextQuery.FIELD_ID_RANGEABLE, firstId, lastId, true, true),
        Occur.MUST);
    searcher.search(overall, compareDates);
    return compareDates.getModifiedDocs();
}
项目:Elasticsearch    文件:CustomFieldQuery.java   
@Override
void flatten(Query sourceQuery, IndexReader reader, Collection<Query> flatQueries, float boost) throws IOException {
    if (sourceQuery instanceof SpanTermQuery) {
        super.flatten(new TermQuery(((SpanTermQuery) sourceQuery).getTerm()), reader, flatQueries, boost);
    } else if (sourceQuery instanceof ConstantScoreQuery) {
        flatten(((ConstantScoreQuery) sourceQuery).getQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FunctionScoreQuery) {
        flatten(((FunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhrasePrefixQuery) {
        flatten(sourceQuery.rewrite(reader), reader, flatQueries, boost);
    } else if (sourceQuery instanceof FiltersFunctionScoreQuery) {
        flatten(((FiltersFunctionScoreQuery) sourceQuery).getSubQuery(), reader, flatQueries, boost);
    } else if (sourceQuery instanceof MultiPhraseQuery) {
        MultiPhraseQuery q = ((MultiPhraseQuery) sourceQuery);
        convertMultiPhraseQuery(0, new int[q.getTermArrays().size()], q, q.getTermArrays(), q.getPositions(), reader, flatQueries);
    } else if (sourceQuery instanceof BlendedTermQuery) {
        final BlendedTermQuery blendedTermQuery = (BlendedTermQuery) sourceQuery;
        flatten(blendedTermQuery.rewrite(reader), reader, flatQueries, boost);
    } else {
        super.flatten(sourceQuery, reader, flatQueries, boost);
    }
}
项目:elasticsearch_my    文件:SimpleQueryParserTests.java   
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));
    }
}
项目:LucenePlus    文件:LuceneIndexTest.java   
@Test
public void testKeywords() throws IOException {
    LuceneIndex index = new LuceneIndex();
    try (Reference<IndexWriter> writer = index.provideWriter()) {
        Document doc1 = new Document();
        LuceneFields.Keyword.add(doc1,"name", "John", LuceneFields.FieldOptions.STORE_INDEX);
        writer.use().addDocument(doc1);

        Document doc2 = new Document();
        LuceneFields.Keyword.add(doc2,"name", "James", LuceneFields.FieldOptions.STORE_INDEX);
        writer.use().addDocument(doc2);
    }

    LuceneSearchResults results = index.search(LuceneSearch.builder().query(new TermQuery(new Term("name", "James"))).build());
    assertTrue(results.hasCount());
    assertEquals(1, (int)results.count());
    assertEquals("James", results.toList().get(0).getField("name").stringValue());
}
项目:elasticsearch_my    文件:TermQueryBuilderTests.java   
@Override
protected void doAssertLuceneQuery(TermQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, either(instanceOf(TermQuery.class)).or(instanceOf(PointRangeQuery.class)));
    MappedFieldType mapper = context.getQueryShardContext().fieldMapper(queryBuilder.fieldName());
    if (query instanceof TermQuery) {
        TermQuery termQuery = (TermQuery) query;
        assertThat(termQuery.getTerm().field(), equalTo(queryBuilder.fieldName()));
        if (mapper != null) {
            Term term = ((TermQuery) mapper.termQuery(queryBuilder.value(), null)).getTerm();
            assertThat(termQuery.getTerm(), equalTo(term));
        } else {
            assertThat(termQuery.getTerm().bytes(), equalTo(BytesRefs.toBytesRef(queryBuilder.value())));
        }
    } else {
        assertEquals(query, mapper.termQuery(queryBuilder.value(), null));
    }
}
项目:elasticsearch_my    文件:VectorHighlighterTests.java   
public void testVectorHighlighterNoStore() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter indexWriter = new IndexWriter(dir, new IndexWriterConfig(Lucene.STANDARD_ANALYZER));

    Document document = new Document();
    document.add(new TextField("_id", "1", Field.Store.YES));
    FieldType vectorsType = new FieldType(TextField.TYPE_NOT_STORED);
    vectorsType.setStoreTermVectors(true);
    vectorsType.setStoreTermVectorPositions(true);
    vectorsType.setStoreTermVectorOffsets(true);
    document.add(new Field("content", "the big bad dog", vectorsType));
    indexWriter.addDocument(document);

    IndexReader reader = DirectoryReader.open(indexWriter);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs topDocs = searcher.search(new TermQuery(new Term("_id", "1")), 1);

    assertThat(topDocs.totalHits, equalTo(1));

    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    String fragment = highlighter.getBestFragment(highlighter.getFieldQuery(new TermQuery(new Term("content", "bad"))),
            reader, topDocs.scoreDocs[0].doc, "content", 30);
    assertThat(fragment, nullValue());
}
项目:elasticsearch_my    文件:BooleanFieldTypeTests.java   
public void testTermQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    assertEquals(new TermQuery(new Term("field", "T")), ft.termQuery("true", null));
    assertEquals(new TermQuery(new Term("field", "F")), ft.termQuery("false", null));

    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
            () -> ft.termQuery("true", null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
项目:NGB-master    文件:VcfFilterForm.java   
private void addAdditionalFilter(BooleanQuery.Builder builder,
        Map.Entry<String, Object> entry) {
    String key = entry.getKey().toLowerCase();
    if (entry.getValue() instanceof List) {
        addFiltersFromList(builder, entry, key);
    } else if (entry.getValue() instanceof Integer || entry.getValue() instanceof Long) {
        builder.add(IntPoint.newExactQuery(key, (Integer) entry.getValue()), BooleanClause.Occur.MUST);
    } else if (entry.getValue() instanceof Float || entry.getValue() instanceof Double) {
        builder.add(FloatPoint.newExactQuery(key, (Float) entry.getValue()), BooleanClause.Occur.MUST);
    } else {
        builder.add(new TermQuery(new Term(key, entry.getValue().toString().toLowerCase())),
                BooleanClause.Occur.MUST);
    }
}
项目:incubator-netbeans    文件:ArtifactDependencyIndexCreator.java   
static Query query(String groupId, String artifactId, String version) {
    final BooleanQuery q = new BooleanQuery();
    q.add(new BooleanClause(new TermQuery(new Term(NB_DEPENDENCY_GROUP, groupId)), BooleanClause.Occur.MUST));
    q.add(new BooleanClause(new TermQuery(new Term(NB_DEPENDENCY_ARTIFACT, artifactId)), BooleanClause.Occur.MUST));
    q.add(new BooleanClause(new TermQuery(new Term(NB_DEPENDENCY_VERSION, version)), BooleanClause.Occur.MUST));
    return q;
}
项目:elasticsearch_my    文件:TextFieldTypeTests.java   
public void testTermQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    assertEquals(new TermQuery(new Term("field", "foo")), ft.termQuery("foo", null));

    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
            () -> ft.termQuery("bar", null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
项目:RedisDirectory    文件:TestLucene.java   
public void
testRedisDirectoryWithRemoteJedisPool() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "10.97.19.55", 6379, Constants.TIME_OUT);
    RedisDirectory redisDirectory = new RedisDirectory(new JedisPoolStream(jedisPool));
    IndexWriter indexWriter = new IndexWriter(redisDirectory, indexWriterConfig);
    for (int i = 0; i < 5000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    redisDirectory.close();
    long end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedisPool consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(new RedisDirectory(new JedisStream("localhost",
            6379))));
    int total = 0;
    for (int i = 0; i < 1000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedisPool search consumes {}ms!", (end - start));
}
项目:RedisDirectory    文件:TestLucene.java   
public void testRedisDirectoryWithJedisPool() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    //indexWriterConfig.setInfoStream(System.out);
    //indexWriterConfig.setRAMBufferSizeMB(2048);
    //LogByteSizeMergePolicy logByteSizeMergePolicy = new LogByteSizeMergePolicy();
    //logByteSizeMergePolicy.setMinMergeMB(1);
    //logByteSizeMergePolicy.setMaxMergeMB(64);
    //logByteSizeMergePolicy.setMaxCFSSegmentSizeMB(64);
    //indexWriterConfig.setRAMBufferSizeMB(1024).setMergePolicy(logByteSizeMergePolicy).setUseCompoundFile(false);
    //GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    //获取连接等待时间
    //genericObjectPoolConfig.setMaxWaitMillis(3000);
    //10s超时时间
    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "localhost", 6379, Constants.TIME_OUT);
    RedisDirectory redisDirectory = new RedisDirectory(new JedisPoolStream(jedisPool));
    IndexWriter indexWriter = new IndexWriter(redisDirectory, indexWriterConfig);
    for (int i = 0; i < 10000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    redisDirectory.close();
    long end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedisPool consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(new RedisDirectory(new JedisStream("localhost",
            6379))));
    int total = 0;
    for (int i = 0; i < 10000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedisPool search consumes {}ms!", (end - start));
}
项目:RedisDirectory    文件:TestLucene.java   
public void testRedisDirectoryWithJedis() throws IOException {
    long start = System.currentTimeMillis();
    IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer()).setOpenMode(IndexWriterConfig
            .OpenMode.CREATE);
    //indexWriterConfig.setInfoStream(System.out);
    //indexWriterConfig.setRAMBufferSizeMB(2048);
    //LogByteSizeMergePolicy logByteSizeMergePolicy = new LogByteSizeMergePolicy();
    //logByteSizeMergePolicy.setMinMergeMB(1);
    //logByteSizeMergePolicy.setMaxMergeMB(64);
    //logByteSizeMergePolicy.setMaxCFSSegmentSizeMB(64);
    //indexWriterConfig.setRAMBufferSizeMB(1024).setMergePolicy(logByteSizeMergePolicy).setUseCompoundFile(false);
    //GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    //获取连接等待时间
    //genericObjectPoolConfig.setMaxWaitMillis(3000);
    //10s超时时间
    RedisDirectory redisDirectory = new RedisDirectory(new JedisStream("localhost", 6379));
    IndexWriter indexWriter = new IndexWriter(redisDirectory, indexWriterConfig);
    for (int i = 0; i < 10000000; i++) {
        indexWriter.addDocument(addDocument(i));
    }
    indexWriter.commit();
    indexWriter.close();
    redisDirectory.close();
    long end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedis consumes {}s!", (end - start) / 1000);
    start = System.currentTimeMillis();
    IndexSearcher indexSearcher = new IndexSearcher(DirectoryReader.open(new RedisDirectory(new JedisStream("localhost",
            6379))));
    int total = 0;
    for (int i = 0; i < 10000000; i++) {
        TermQuery key1 = new TermQuery(new Term("key1", "key" + i));
        TopDocs search = indexSearcher.search(key1, 10);
        total += search.totalHits;
    }
    System.out.println(total);
    end = System.currentTimeMillis();
    log.error("RedisDirectoryWithJedis search consumes {}ms!", (end - start));
}
项目:elasticsearch_my    文件:PercolatorFieldMapper.java   
Query createCandidateQuery(IndexReader indexReader) throws IOException {
    List<BytesRef> extractedTerms = new ArrayList<>();
    LeafReader reader = indexReader.leaves().get(0).reader();
    Fields fields = reader.fields();
    for (String field : fields) {
        Terms terms = fields.terms(field);
        if (terms == null) {
            continue;
        }

        BytesRef fieldBr = new BytesRef(field);
        TermsEnum tenum = terms.iterator();
        for (BytesRef term = tenum.next(); term != null; term = tenum.next()) {
            BytesRefBuilder builder = new BytesRefBuilder();
            builder.append(fieldBr);
            builder.append(FIELD_VALUE_SEPARATOR);
            builder.append(term);
            extractedTerms.add(builder.toBytesRef());
        }
    }
    Query extractionSuccess = new TermInSetQuery(queryTermsField.name(), extractedTerms);
    // include extractionResultField:failed, because docs with this term have no extractedTermsField
    // and otherwise we would fail to return these docs. Docs that failed query term extraction
    // always need to be verified by MemoryIndex:
    Query extractionFailure = new TermQuery(new Term(extractionResultField.name(), EXTRACTION_FAILED));

    return new BooleanQuery.Builder()
            .add(extractionSuccess, Occur.SHOULD)
            .add(extractionFailure, Occur.SHOULD)
            .build();
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_booleanQuery() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    builder.add(phraseQuery, BooleanClause.Occur.SHOULD);

    BooleanQuery.Builder subBuilder = new BooleanQuery.Builder();
    TermQuery termQuery2 = new TermQuery(new Term("_field1", "_term"));
    subBuilder.add(termQuery2, BooleanClause.Occur.MUST);
    TermQuery termQuery3 = new TermQuery(new Term("_field3", "_long_term"));
    subBuilder.add(termQuery3, BooleanClause.Occur.MUST);
    builder.add(subBuilder.build(), BooleanClause.Occur.SHOULD);

    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat("Should clause with phrase query isn't verified, so entire query can't be verified", result.verified, is(false));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(3));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
    assertThat(terms.get(1).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(1).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
    assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
    assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_booleanQuery_onlyShould() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term1"));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    TermQuery termQuery2 = new TermQuery(new Term("_field", "_term2"));
    builder.add(termQuery2, BooleanClause.Occur.SHOULD);

    BooleanQuery.Builder subBuilder = new BooleanQuery.Builder();
    TermQuery termQuery3 = new TermQuery(new Term("_field1", "_term"));
    subBuilder.add(termQuery3, BooleanClause.Occur.SHOULD);
    TermQuery termQuery4 = new TermQuery(new Term("_field3", "_long_term"));
    subBuilder.add(termQuery4, BooleanClause.Occur.SHOULD);
    builder.add(subBuilder.build(), BooleanClause.Occur.SHOULD);

    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(4));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
    assertThat(terms.get(1).field(), equalTo(termQuery2.getTerm().field()));
    assertThat(terms.get(1).bytes(), equalTo(termQuery2.getTerm().bytes()));
    assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
    assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
    assertThat(terms.get(3).field(), equalTo(termQuery4.getTerm().field()));
    assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_booleanQueryWithMustNot() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    builder.add(termQuery1, BooleanClause.Occur.MUST_NOT);
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    builder.add(phraseQuery, BooleanClause.Occur.SHOULD);

    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat(result.verified, is(false));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(0).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_constantScoreQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(termQuery1);
    Result result = analyze(constantScoreQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
}
项目:elasticsearch_my    文件:NestedSortingTests.java   
private TopDocs getTopDocs(IndexSearcher searcher, IndexFieldData<?> indexFieldData, String missingValue, MultiValueMode sortMode, int n, boolean reverse) throws IOException {
    Query parentFilter = new TermQuery(new Term("__type", "parent"));
    Query childFilter = new TermQuery(new Term("__type", "child"));
    XFieldComparatorSource nestedComparatorSource = indexFieldData.comparatorSource(missingValue, sortMode, createNested(searcher, parentFilter, childFilter));
    Query query = new ConstantScoreQuery(parentFilter);
    Sort sort = new Sort(new SortField("f", nestedComparatorSource, reverse));
    return searcher.search(query, n, sort);
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_matchNoDocsQuery() {
    Result result = analyze(new MatchNoDocsQuery("sometimes there is no reason at all"));
    assertThat(result.verified, is(true));
    assertEquals(0, result.terms.size());

    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.MUST);
    bq.add(new MatchNoDocsQuery("sometimes there is no reason at all"), BooleanClause.Occur.MUST);
    result = analyze(bq.build());
    assertThat(result.verified, is(false));
    assertEquals(0, result.terms.size());

    bq = new BooleanQuery.Builder();
    bq.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.SHOULD);
    bq.add(new MatchNoDocsQuery("sometimes there is no reason at all"), BooleanClause.Occur.SHOULD);
    result = analyze(bq.build());
    assertThat(result.verified, is(true));
    assertTermsEqual(result.terms, new Term("field", "value"));

    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(
            Arrays.asList(new TermQuery(new Term("field", "value")), new MatchNoDocsQuery("sometimes there is no reason at all")),
            1f
    );
    result = analyze(disjunctionMaxQuery);
    assertThat(result.verified, is(true));
    assertTermsEqual(result.terms, new Term("field", "value"));
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_unsupportedQuery() {
    TermRangeQuery termRangeQuery = new TermRangeQuery("_field", null, null, true, false);
    UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(termRangeQuery));
    assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery));

    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    builder.add(termRangeQuery, BooleanClause.Occur.SHOULD);
    BooleanQuery bq = builder.build();

    e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq));
    assertThat(e.getUnsupportedQuery(), sameInstance(termRangeQuery));
}
项目:elasticsearch_my    文件:QueryAnalyzerTests.java   
public void testExtractQueryMetadata_unsupportedQueryInBoolQueryWithMustClauses() {
    TermRangeQuery unsupportedQuery = new TermRangeQuery("_field", null, null, true, false);

    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.MUST);
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    BooleanQuery bq1 = builder.build();

    Result result = analyze(bq1);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, termQuery1.getTerm());

    TermQuery termQuery2 = new TermQuery(new Term("_field", "_longer_term"));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.MUST);
    builder.add(termQuery2, BooleanClause.Occur.MUST);
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    bq1 = builder.build();
    result = analyze(bq1);
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, termQuery2.getTerm());

    builder = new BooleanQuery.Builder();
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    builder.add(unsupportedQuery, BooleanClause.Occur.MUST);
    BooleanQuery bq2 = builder.build();
    UnsupportedQueryException e = expectThrows(UnsupportedQueryException.class, () -> analyze(bq2));
    assertThat(e.getUnsupportedQuery(), sameInstance(unsupportedQuery));
}
项目:NGB-master    文件:VcfFilterForm.java   
private void addFeatureTypeFilter(FeatureType featureType, BooleanQuery.Builder builder) {
    if (featureType != null) {
        builder.add(new TermQuery(new Term(FeatureIndexFields.FEATURE_TYPE.getFieldName(),
                                           featureType.getFileValue())),
                BooleanClause.Occur.MUST);
    }
}