List of usage examples for org.apache.lucene.index.memory MemoryIndex fromDocument
public static MemoryIndex fromDocument(Iterable<? extends IndexableField> document, Analyzer analyzer)
From source file:net.yacy.search.index.SingleDocumentMatcher.java
License:Open Source License
/** * Check a given Solr document against a Solr query, without requesting a Solr * index, but using instead in-memory Lucene utility. This lets checking if a * single document matches some criterias, before adding it to a Solr index. * // ww w. java2s . co m * @param solrDoc * the Solr document to check * @param query * a standard Solr query string * @param core * the Solr index core holding the Solr schema of the document * @return true when the document matches the given Solr query * @throws SyntaxError * when the query String syntax is not valid * @throws SolrException when a query required element is missing, or when a problem occurred when accessing the target core * @throws IllegalArgumentException * when a parameter is null. * @see <a href= * "http://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html">The * Solr Standard Query Parser</a> */ public static boolean matches(final SolrInputDocument solrDoc, final String query, final SolrCore core) throws SyntaxError, IllegalArgumentException { if (solrDoc == null || query == null || core == null) { throw new IllegalArgumentException("All parameters must be non null"); } final IndexSchema schema = core.getLatestSchema(); if (schema == null) { throw new IllegalArgumentException("All parameters must be non null"); } final org.apache.lucene.document.Document luceneDoc = DocumentBuilder.toDocument(solrDoc, schema); final Analyzer indexAnalyzer = schema.getIndexAnalyzer(); /* * Using the Lucene RAMDirectory could be an alternative, but it is slower with * a larger memory footprint */ final MemoryIndex index = MemoryIndex.fromDocument(luceneDoc, indexAnalyzer); final Query luceneQuery = toLuceneQuery(query, core); final float score = index.search(luceneQuery); return score > 0.0f; }
From source file:org.elasticsearch.percolator.CandidateQueryTests.java
License:Apache License
public void testDuel() throws Exception { List<Function<String, Query>> queryFunctions = new ArrayList<>(); queryFunctions.add((id) -> new PrefixQuery(new Term("field", id))); queryFunctions.add((id) -> new WildcardQuery(new Term("field", id + "*"))); queryFunctions.add((id) -> new CustomQuery(new Term("field", id))); queryFunctions.add((id) -> new SpanTermQuery(new Term("field", id))); queryFunctions.add((id) -> new TermQuery(new Term("field", id))); queryFunctions.add((id) -> {//from w w w . j ava 2s. c o m BooleanQuery.Builder builder = new BooleanQuery.Builder(); return builder.build(); }); queryFunctions.add((id) -> { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new TermQuery(new Term("field", id)), BooleanClause.Occur.MUST); if (randomBoolean()) { builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT); } if (randomBoolean()) { builder.add(new CustomQuery(new Term("field", id)), BooleanClause.Occur.MUST); } return builder.build(); }); queryFunctions.add((id) -> { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new TermQuery(new Term("field", id)), BooleanClause.Occur.SHOULD); if (randomBoolean()) { builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT); } if (randomBoolean()) { builder.add(new CustomQuery(new Term("field", id)), BooleanClause.Occur.SHOULD); } return builder.build(); }); queryFunctions.add((id) -> { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST); if (randomBoolean()) { builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT); } return builder.build(); }); queryFunctions.add((id) -> { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD); builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD); if (randomBoolean()) { builder.add(new MatchNoDocsQuery("no reason"), BooleanClause.Occur.MUST_NOT); } return builder.build(); }); queryFunctions.add((id) -> { BooleanQuery.Builder builder = new BooleanQuery.Builder(); builder.setMinimumNumberShouldMatch(randomIntBetween(0, 4)); builder.add(new TermQuery(new Term("field", id)), BooleanClause.Occur.SHOULD); builder.add(new CustomQuery(new Term("field", id)), BooleanClause.Occur.SHOULD); return builder.build(); }); queryFunctions.add((id) -> new MatchAllDocsQuery()); queryFunctions.add((id) -> new MatchNoDocsQuery("no reason at all")); int numDocs = randomIntBetween(queryFunctions.size(), queryFunctions.size() * 3); List<ParseContext.Document> documents = new ArrayList<>(); for (int i = 0; i < numDocs; i++) { String id = Integer.toString(i); Query query = queryFunctions.get(i % queryFunctions.size()).apply(id); addQuery(query, documents); } indexWriter.addDocuments(documents); indexWriter.close(); directoryReader = DirectoryReader.open(directory); IndexSearcher shardSearcher = newSearcher(directoryReader); // Disable query cache, because ControlQuery cannot be cached... shardSearcher.setQueryCache(null); for (int i = 0; i < numDocs; i++) { String id = Integer.toString(i); Iterable<? extends IndexableField> doc = Collections .singleton(new StringField("field", id, Field.Store.NO)); MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer()); duelRun(queryStore, memoryIndex, shardSearcher); } Iterable<? extends IndexableField> doc = Collections .singleton(new StringField("field", "value", Field.Store.NO)); MemoryIndex memoryIndex = MemoryIndex.fromDocument(doc, new WhitespaceAnalyzer()); duelRun(queryStore, memoryIndex, shardSearcher); // Empty percolator doc: memoryIndex = new MemoryIndex(); duelRun(queryStore, memoryIndex, shardSearcher); }
From source file:org.elasticsearch.percolator.CandidateQueryTests.java
License:Apache License
public void testDuelSpecificQueries() throws Exception { List<ParseContext.Document> documents = new ArrayList<>(); CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 128); commonTermsQuery.add(new Term("field", "quick")); commonTermsQuery.add(new Term("field", "brown")); commonTermsQuery.add(new Term("field", "fox")); addQuery(commonTermsQuery, documents); BlendedTermQuery blendedTermQuery = BlendedTermQuery.booleanBlendedQuery( new Term[] { new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox") }, false);//from w ww . ja v a 2s. c o m addQuery(blendedTermQuery, documents); SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true) .addClause(new SpanTermQuery(new Term("field", "quick"))) .addClause(new SpanTermQuery(new Term("field", "brown"))) .addClause(new SpanTermQuery(new Term("field", "fox"))).build(); addQuery(spanNearQuery, documents); SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true) .addClause(new SpanTermQuery(new Term("field", "the"))) .addClause(new SpanTermQuery(new Term("field", "lazy"))) .addClause(new SpanTermQuery(new Term("field", "doc"))).build(); SpanOrQuery spanOrQuery = new SpanOrQuery(spanNearQuery, spanNearQuery2); addQuery(spanOrQuery, documents); SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery); addQuery(spanNotQuery, documents); long lowerLong = randomIntBetween(0, 256); long upperLong = lowerLong + randomIntBetween(0, 32); addQuery(LongPoint.newRangeQuery("long_field", lowerLong, upperLong), documents); indexWriter.addDocuments(documents); indexWriter.close(); directoryReader = DirectoryReader.open(directory); IndexSearcher shardSearcher = newSearcher(directoryReader); // Disable query cache, because ControlQuery cannot be cached... shardSearcher.setQueryCache(null); Document document = new Document(); document.add(new TextField("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO)); long randomLong = randomIntBetween((int) lowerLong, (int) upperLong); document.add(new LongPoint("long_field", randomLong)); MemoryIndex memoryIndex = MemoryIndex.fromDocument(document, new WhitespaceAnalyzer()); duelRun(queryStore, memoryIndex, shardSearcher); }
From source file:org.elasticsearch.search.aggregations.MultiBucketAggregatorWrapperTests.java
License:Apache License
public void testNoNullScorerIsDelegated() throws Exception { LeafReaderContext leafReaderContext = MemoryIndex .fromDocument(Collections.emptyList(), new MockAnalyzer(random())).createSearcher().getIndexReader() .leaves().get(0);// ww w . j a v a 2s . c o m BigArrays bigArrays = new MockBigArrays(Settings.EMPTY, new NoneCircuitBreakerService()); SearchContext searchContext = mock(SearchContext.class); when(searchContext.bigArrays()).thenReturn(bigArrays); Aggregator aggregator = mock(Aggregator.class); AggregatorFactory aggregatorFactory = new TestAggregatorFactory(searchContext, aggregator); LeafBucketCollector wrappedCollector = mock(LeafBucketCollector.class); when(aggregator.getLeafCollector(leafReaderContext)).thenReturn(wrappedCollector); Aggregator wrapper = AggregatorFactory.asMultiBucketAggregator(aggregatorFactory, searchContext, null); LeafBucketCollector collector = wrapper.getLeafCollector(leafReaderContext); collector.collect(0, 0); // setScorer should not be invoked as it has not been set // Only collect should be invoked: verify(wrappedCollector).collect(0, 0); verifyNoMoreInteractions(wrappedCollector); reset(wrappedCollector); Scorer scorer = mock(Scorer.class); collector.setScorer(scorer); collector.collect(0, 1); verify(wrappedCollector).setScorer(same(scorer)); verify(wrappedCollector).collect(0, 0); verifyNoMoreInteractions(wrappedCollector); wrapper.close(); }