org.apache.lucene.search.Query Java Examples
The following examples show how to use
org.apache.lucene.search.Query.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestSolrQueryParser.java From lucene-solr with Apache License 2.0 | 7 votes |
public void testSynonymsBoost_singleTermQueryMultiTermSynonyms_shouldParseBoostedQuery() throws Exception { //leopard, big cat|0.8, bagheera|0.9, panthera pardus|0.85 Query q = QParser.getParser("leopard", req(params("df", "t_pick_best_boosted_foo"))).getQuery(); assertEquals("((t_pick_best_boosted_foo:\"big cat\")^0.8 | (t_pick_best_boosted_foo:bagheera)^0.9 | (t_pick_best_boosted_foo:\"panthera pardus\")^0.85 | t_pick_best_boosted_foo:leopard)", q.toString()); q = QParser.getParser("leopard", req(params("df", "t_as_distinct_boosted_foo"))).getQuery(); assertEquals("((t_as_distinct_boosted_foo:\"big cat\")^0.8 (t_as_distinct_boosted_foo:bagheera)^0.9 (t_as_distinct_boosted_foo:\"panthera pardus\")^0.85 t_as_distinct_boosted_foo:leopard)", q.toString()); q = QParser.getParser("leopard", req(params("df", "t_as_same_term_boosted_foo"))).getQuery(); assertEquals("((t_as_same_term_boosted_foo:\"big cat\")^0.8 (t_as_same_term_boosted_foo:bagheera)^0.9 (t_as_same_term_boosted_foo:\"panthera pardus\")^0.85 t_as_same_term_boosted_foo:leopard)", q.toString()); //lion => panthera leo|0.9, simba leo|0.8, kimba|0.75 q = QParser.getParser("lion", req(params("df", "t_pick_best_boosted_foo"))).getQuery(); assertEquals("((t_pick_best_boosted_foo:\"panthera leo\")^0.9 | (t_pick_best_boosted_foo:\"simba leo\")^0.8 | (t_pick_best_boosted_foo:kimba)^0.75)", q.toString()); q = QParser.getParser("lion", req(params("df", "t_as_distinct_boosted_foo"))).getQuery(); assertEquals("((t_as_distinct_boosted_foo:\"panthera leo\")^0.9 (t_as_distinct_boosted_foo:\"simba leo\")^0.8 (t_as_distinct_boosted_foo:kimba)^0.75)", q.toString()); q = QParser.getParser("lion", req(params("df", "t_as_same_term_boosted_foo"))).getQuery(); assertEquals("((t_as_same_term_boosted_foo:\"panthera leo\")^0.9 (t_as_same_term_boosted_foo:\"simba leo\")^0.8 (t_as_same_term_boosted_foo:kimba)^0.75)", q.toString()); }
Example #2
Source File: QueryResultKeyTest.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testFiltersOutOfOrder1() { // the hashcode should be the same even when the list // of filters is in a different order Sort sort = new Sort(new SortField("test", SortField.Type.INT)); BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add(new TermQuery(new Term("test", "field")), Occur.MUST); List<Query> filters = Arrays.<Query>asList(new TermQuery(new Term("test", "field")), new TermQuery(new Term("test2", "field2"))); QueryResultKey qrk1 = new QueryResultKey(query.build() , filters, sort, 1); List<Query> filters2 = Arrays.<Query>asList(new TermQuery(new Term("test2", "field2")), new TermQuery(new Term("test", "field"))); QueryResultKey qrk2 = new QueryResultKey(query.build() , filters2, sort, 1); assertKeyEquals(qrk1, qrk2); }
Example #3
Source File: RecursivePrefixTreeStrategy.java From lucene-solr with Apache License 2.0 | 6 votes |
@Override public Query makeQuery(SpatialArgs args) { final SpatialOperation op = args.getOperation(); Shape shape = args.getShape(); int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct)); if (op == SpatialOperation.Intersects) { if (isGridAlignedShape(args.getShape())) { return makeGridShapeIntersectsQuery(args.getShape()); } return new IntersectsPrefixTreeQuery( shape, getFieldName(), grid, detailLevel, prefixGridScanLevel); } else if (op == SpatialOperation.IsWithin) { return new WithinPrefixTreeQuery( shape, getFieldName(), grid, detailLevel, prefixGridScanLevel, -1);//-1 flag is slower but ensures correct results } else if (op == SpatialOperation.Contains) { return new ContainsPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, multiOverlappingIndexedShapes); } throw new UnsupportedSpatialOperation(op); }
Example #4
Source File: JoinQParserPlugin.java From lucene-solr with Apache License 2.0 | 6 votes |
@Override public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) { final JoinQParserPlugin plugin = this; return new QParser(qstr, localParams, params, req) { @Override public Query parse() throws SyntaxError { if (localParams != null && localParams.get(METHOD) != null) { // TODO Make sure 'method' is valid value here and give users a nice error final Method explicitMethod = Method.valueOf(localParams.get(METHOD)); return explicitMethod.makeFilter(this, plugin); } // Legacy join behavior before introduction of SOLR-13892 if(localParams!=null && localParams.get(ScoreJoinQParserPlugin.SCORE)!=null) { return new ScoreJoinQParserPlugin().createParser(qstr, localParams, params, req).parse(); } else { return Method.index.makeFilter(this, plugin); } } }; }
Example #5
Source File: FTConnLucene.java From openprodoc with GNU Affero General Public License v3.0 | 6 votes |
/** * * @param Type * @param sDocMetadata * @param sBody * @param sMetadata * @return * @throws PDException */ @Override protected ArrayList<String> Search(String Type, String sDocMetadata, String sBody, String sMetadata) throws PDException { ArrayList<String> Res=new ArrayList(); IndexSearcher isearcher=null; try { isearcher=SM.acquire(); sBody=sBody.toLowerCase(); Query query = new QueryParser(F_FULLTEXT,analyzer).parse(sBody); ScoreDoc[] hits = isearcher.search(query, MAXRESULTS).scoreDocs; for (ScoreDoc hit : hits) Res.add(isearcher.doc(hit.doc).get(F_ID)); SM.release(isearcher); //ireader.close(); //directory.close(); } catch (Exception ex) { try { SM.release(isearcher); } catch (Exception e) {} PDException.GenPDException("Error_Searching_doc_FT:", ex.getLocalizedMessage()); } return(Res); }
Example #6
Source File: WIGQPPredictor.java From lucene4ir with Apache License 2.0 | 6 votes |
@Override public double scoreQuery(String qno, Query q) { double queryLength = q.toString().split(" ").length; TrecRuns topic = run.getTopic(qno); double D = topic.get(topic.size() - 1).getScore(); double totalScore = 0; // Handle the case that the query retrieves less than k documents. int thisK = k; if (topic.size() < k) { thisK = topic.size(); } for (int i = 0; i < thisK; i++) { double d = topic.get(i).getScore(); totalScore += sumScores(queryLength, d, D); } return (1.0 / k) * totalScore; }
Example #7
Source File: SpatialTermQueryPrefixTreeStrategyFieldTypeDefinitionGeohashTest.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
public void testGeoHash() throws IOException, ParseException { BaseFieldManager fieldManager = getFieldManager(new NoStopWordStandardAnalyzer()); setupGisField(fieldManager); DirectoryReader reader = DirectoryReader.open(_dir); IndexSearcher searcher = new IndexSearcher(reader); SuperParser parser = new SuperParser(Version.LUCENE_43, fieldManager, true, null, ScoreType.SUPER, new Term( BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE)); Query query = parser.parse("fam.geo:\"GeoHash(uvgb26kqsm0)\""); TopDocs topDocs = searcher.search(query, 10); assertEquals(1, topDocs.totalHits); reader.close(); }
Example #8
Source File: TestRedisQParser.java From solr-redis with Apache License 2.0 | 6 votes |
@Test public void shouldDeflateGzipAndParseJsonTermsFromRedisOnGetCommand() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("get"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("compression")).thenReturn("gzip"); when(localParamsMock.get("serialization")).thenReturn("json"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.get(any(byte[].class))).thenReturn(Compressor.compressGzip("[100,200,300]".getBytes())); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); verify(jedisMock).get("simpleKey".getBytes()); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(3, terms.size()); }
Example #9
Source File: CrawlerTask.java From JPPF with Apache License 2.0 | 6 votes |
/** * Search for the user-specified query expression in the current page. * @throws Exception if an error occurs. */ private void search() throws Exception { final QueryParser parser = new QueryParser("contents", new StandardAnalyzer()); final Query q = parser.parse(query); final MemoryIndex index = new MemoryIndex(); final Link link = new Link(url); final PageData pageData = new SimpleHttpClientParser().load(link); index.addField("contents", pageData.getData().toString(), new StandardAnalyzer()); final IndexSearcher searcher = index.createSearcher(); final Hits hits = searcher.search(q); @SuppressWarnings("rawtypes") final Iterator it = hits.iterator(); float relevance = 0f; if (it.hasNext()) { while (it.hasNext()) { final Hit hit = (Hit) it.next(); relevance += ((float) Math.round(hit.getScore() * 1000)) / 10; } matchedLinks.add(new LinkMatch(url, relevance)); } }
Example #10
Source File: PercolatorService.java From Elasticsearch with Apache License 2.0 | 6 votes |
private void queryBasedPercolating(Engine.Searcher percolatorSearcher, PercolateContext context, QueryCollector percolateCollector) throws IOException { Query percolatorTypeFilter = context.indexService().mapperService().documentMapper(TYPE_NAME).typeFilter(); final Query filter; if (context.aliasFilter() != null) { BooleanQuery.Builder booleanFilter = new BooleanQuery.Builder(); booleanFilter.add(context.aliasFilter(), BooleanClause.Occur.MUST); booleanFilter.add(percolatorTypeFilter, BooleanClause.Occur.MUST); filter = booleanFilter.build(); } else { filter = percolatorTypeFilter; } Query query = Queries.filtered(context.percolateQuery(), filter); percolatorSearcher.searcher().search(query, percolateCollector); percolateCollector.aggregatorCollector.postCollection(); if (context.aggregations() != null) { aggregationPhase.execute(context); } }
Example #11
Source File: LuceneQueryConverter.java From dremio-oss with Apache License 2.0 | 5 votes |
private Query toRangeQuery(RangeTerm range) { return TermRangeQuery.newStringRange( range.getField(), range.hasMin() ? range.getMin() : null, range.hasMax() ? range.getMax() : null, range.getMinInclusive(), range.getMaxInclusive()); }
Example #12
Source File: LuceneAccessor.java From jstarcraft-core with Apache License 2.0 | 5 votes |
@Override public <K extends Comparable, T extends IdentityObject<K>> void iterate(StorageIterator<T> iterator, Class<T> clazz, StoragePagination pagination) { LuceneMetadata metadata = metadatas.get(clazz); Query query = new MatchAllDocsQuery(); int offset = pagination == null ? 0 : pagination.getFirst(); int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize(); engine.iterateDocuments((document) -> { iterator.iterate((T) metadata.decodeDocument(document)); }, query, null, offset, size); }
Example #13
Source File: TestRedisQParser.java From solr-redis with Apache License 2.0 | 5 votes |
@Test public void shouldTurnAnalysisOff() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("smembers"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("useAnalyzer")).thenReturn("false"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.smembers(anyString())).thenReturn(new HashSet<>(Arrays.asList("123 124", "321"))); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); verify(jedisMock).smembers("simpleKey"); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
Example #14
Source File: TestBooleanTermExtractor.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testAllDisjunctionQueriesAreIncluded() { Query bq = MonitorTestBase.parse("field1:term1 field1:term2"); Set<Term> terms = collectTerms(bq); Set<Term> expected = new HashSet<>(Arrays.asList( new Term("field1", "term1"), new Term("field1", "term2"))); assertEquals(expected, terms); }
Example #15
Source File: FilterQuery.java From lucene-solr with Apache License 2.0 | 5 votes |
public FilterQuery(Query q) { if (q == null) { this.q = new MatchNoDocsQuery(); return; } this.q = q; }
Example #16
Source File: ExplorerQuery.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
@Override public Query rewrite(IndexReader reader) throws IOException { Query rewritten = query.rewrite(reader); if (rewritten != query) { return new ExplorerQuery(rewritten, type); } return this; }
Example #17
Source File: DummyHighlighter.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings({"unchecked", "rawtypes"}) public NamedList<Object> doHighlighting(DocList docs, Query query, SolrQueryRequest req, String[] defaultFields) throws IOException { NamedList fragments = new SimpleOrderedMap(); fragments.add("dummy", "thing1"); return fragments; }
Example #18
Source File: OptimizedFeatureSet.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
@Override public List<Query> toQueries(LtrQueryContext context, Map<String, Object> params) { List<Query> queries = new ArrayList<>(features.size()); for(Feature feature : features) { if(context.isFeatureActive(feature.name())) { queries.add(feature.doToQuery(context, this, params)); } else { queries.add(new MatchNoDocsQuery("Feature " + feature.name() + " deactivated")); } } return queries; }
Example #19
Source File: ExplorerQueryTests.java From elasticsearch-learning-to-rank with Apache License 2.0 | 5 votes |
public void testInvalidStat() throws Exception { Query q = new TermQuery(new Term("text", "cow")); String statsType = "sum_invalid_stat"; ExplorerQuery eq = new ExplorerQuery(q, statsType); expectThrows(RuntimeException.class, () -> searcher.search(eq, 4)); }
Example #20
Source File: LuceneQueryBuilderTest.java From querqy with Apache License 2.0 | 5 votes |
@Test public void testStopWordRemoval() throws Exception { float tie = (float) Math.random(); Query q = buildWithStopWords("a stopA b", tie, "f1"); assertThat(q, bq(1f, dtq(Occur.SHOULD, 1f, "f1", "a"), dtq(Occur.SHOULD, 1f, "f1", "b") )); }
Example #21
Source File: FacetsCollector.java From lucene-solr with Apache License 2.0 | 5 votes |
/** Utility method, to search and also collect all hits * into the provided {@link Collector}. */ public static TopDocs searchAfter(IndexSearcher searcher, ScoreDoc after, Query q, int n, Sort sort, Collector fc) throws IOException { if (sort == null) { throw new IllegalArgumentException("sort must not be null"); } return doSearch(searcher, after, q, n, sort, false, fc); }
Example #22
Source File: MatchQueries.java From crate with Apache License 2.0 | 5 votes |
public static Query multiMatch(QueryShardContext queryShardContext, @Nullable String matchType, Map<String, Float> fieldNames, String queryString, Map<String, Object> options) throws IOException { MultiMatchQueryType type = MatchQueries.getType(matchType); ParsedOptions parsedOptions = OptionParser.parse(type, options); MultiMatchQuery multiMatchQuery = new MultiMatchQuery(queryShardContext); Float tieBreaker = parsedOptions.tieBreaker(); if (tieBreaker != null) { multiMatchQuery.setTieBreaker(tieBreaker); } String analyzer = parsedOptions.analyzer(); if (analyzer != null) { multiMatchQuery.setAnalyzer(analyzer); } multiMatchQuery.setCommonTermsCutoff(parsedOptions.commonTermsCutoff()); multiMatchQuery.setFuzziness(parsedOptions.fuzziness()); multiMatchQuery.setFuzzyPrefixLength(parsedOptions.prefixLength()); multiMatchQuery.setFuzzyRewriteMethod(parsedOptions.rewriteMethod()); multiMatchQuery.setMaxExpansions(parsedOptions.maxExpansions()); multiMatchQuery.setPhraseSlop(parsedOptions.phraseSlop()); multiMatchQuery.setTranspositions(parsedOptions.transpositions()); multiMatchQuery.setZeroTermsQuery(parsedOptions.zeroTermsQuery()); multiMatchQuery.setOccur(parsedOptions.operator()); return multiMatchQuery.parse(type, fieldNames, queryString, parsedOptions.minimumShouldMatch()); }
Example #23
Source File: ExtendedCommonTermsQuery.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override protected Query newTermQuery(Term term, TermContext context) { if (fieldType == null) { return super.newTermQuery(term, context); } final Query query = fieldType.queryStringTermQuery(term); if (query == null) { return super.newTermQuery(term, context); } else { return query; } }
Example #24
Source File: FacetsCollector.java From lucene-solr with Apache License 2.0 | 5 votes |
/** Utility method, to search and also collect all hits * into the provided {@link Collector}. */ public static TopFieldDocs search(IndexSearcher searcher, Query q, int n, Sort sort, boolean doDocScores, Collector fc) throws IOException { if (sort == null) { throw new IllegalArgumentException("sort must not be null"); } return (TopFieldDocs) doSearch(searcher, null, q, n, sort, doDocScores, fc); }
Example #25
Source File: LongRange.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public Query rewrite(IndexReader reader) throws IOException { if (fastMatchQuery != null) { final Query fastMatchRewritten = fastMatchQuery.rewrite(reader); if (fastMatchRewritten != fastMatchQuery) { return new ValueSourceQuery(range, fastMatchRewritten, valueSource); } } return super.rewrite(reader); }
Example #26
Source File: KNearestFuzzyClassifier.java From lucene-solr with Apache License 2.0 | 5 votes |
private TopDocs knnSearch(String text) throws IOException { BooleanQuery.Builder bq = new BooleanQuery.Builder(); NearestFuzzyQuery nearestFuzzyQuery = new NearestFuzzyQuery(analyzer); for (String fieldName : textFieldNames) { nearestFuzzyQuery.addTerms(text, fieldName); } bq.add(nearestFuzzyQuery, BooleanClause.Occur.MUST); Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*")); bq.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST)); if (query != null) { bq.add(query, BooleanClause.Occur.MUST); } return indexSearcher.search(bq.build(), k); }
Example #27
Source File: SpecialMultiFieldQueryParser.java From webdsl with Apache License 2.0 | 5 votes |
@Override public Query parse(String query) throws ParseException{ Query q = super.parse(query); if(defaultAndnStopword){ inQueryFix = true; Query fix = super.parse(query); List<BooleanClause> clauses = new ArrayList<BooleanClause>(); clauses.add(new BooleanClause(q,Occur.SHOULD)); clauses.add(new BooleanClause(fix,Occur.SHOULD)); return getBooleanQuery(clauses); } return q; }
Example #28
Source File: TestValueSources.java From lucene-solr with Apache License 2.0 | 5 votes |
void assertHits(Query q, float scores[]) throws Exception { ScoreDoc expected[] = new ScoreDoc[scores.length]; int expectedDocs[] = new int[scores.length]; for (int i = 0; i < expected.length; i++) { expectedDocs[i] = i; expected[i] = new ScoreDoc(i, scores[i]); } TopDocs docs = searcher.search(q, documents.size(), new Sort(new SortField("id", SortField.Type.STRING)), true); CheckHits.checkHits(random(), q, "", searcher, expectedDocs); CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs); CheckHits.checkExplanations(q, "", searcher); }
Example #29
Source File: ElasticsearchAssertions.java From crate with Apache License 2.0 | 5 votes |
public static <T extends Query> T assertDisjunctionSubQuery(Query query, Class<T> subqueryType, int i) { assertThat(query, instanceOf(DisjunctionMaxQuery.class)); DisjunctionMaxQuery q = (DisjunctionMaxQuery) query; assertThat(q.getDisjuncts().size(), greaterThan(i)); assertThat(q.getDisjuncts().get(i), instanceOf(subqueryType)); return subqueryType.cast(q.getDisjuncts().get(i)); }
Example #30
Source File: ExpandComponent.java From lucene-solr with Apache License 2.0 | 5 votes |
private Query getPointGroupQuery(SchemaField sf, int size, LongHashSet groupSet) { Iterator<LongCursor> it = groupSet.iterator(); List<String> values = new ArrayList<>(size); FieldType ft = sf.getType(); while (it.hasNext()) { LongCursor cursor = it.next(); values.add(numericToString(ft, cursor.value)); } return sf.getType().getSetQuery(null, sf, values); }