Java Code Examples for org.apache.lucene.search.Query

The following are top voted examples for showing how to use org.apache.lucene.search.Query. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: ait-platform   File: AitFTSRepo.java   Source Code and License 7 votes vote down vote up
@Override
public FullTextQuery getFTSQuery(String filterText, final Class<?> entityType, Integer maxResults, String... fields) {

	// entityManager para busquedas de tipo FTS
	final FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);

	// se crea el query usando Hibernate Search query DSL
	final QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(entityType).get();

	// se crea el query sobre los campos indicados
	final Query query = queryBuilder.keyword().onFields(fields).matching(filterText.trim()).createQuery();

	// se enmascara el query de Lucene en uno de Hibernate
	final FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(query, entityType);

	// se define la cantidad maxima de resultados si es mayor a cero
	if (maxResults > 0) {
		jpaQuery.setMaxResults(maxResults);
	}
	// se retorna el query listo para ejecución o para inyeccion de criterias
	return jpaQuery;
}
 
Example 2
Project: ir-generalized-translation-models   File: SimilarityParserPluginIntegrationTest.java   Source Code and License 6 votes vote down vote up
/**
 * Basic test that checks that the "/similarity-query"
 * defined in the resources/solrconfig.xml does set the query parser
 * to the <code>{@link SimilarityParserPlugin}</code>
 */
public void test_QParserGetsCalled() throws Exception {

    // arrange
    SolrQueryRequest queryRequest = req("testQueryString");

    // act
    SolrQueryResponse resp = h.queryAndResponse("/similarity-query", queryRequest);

    // assert - the only way to check that the similarity parser was used is to check
    //          the type of the query returned by the similarity parser (for a single term): AugmentedTermQuery
    BasicResultContext basicResultContext = (BasicResultContext)resp.getResponse();
    Query usedLuceneQuery = basicResultContext.getQuery();
    assertTrue(usedLuceneQuery instanceof AugmentedTermQuery);

    // cleanup
    queryRequest.close();
}
 
Example 3
Project: elasticsearch_my   File: SimpleQueryParserTests.java   Source Code and License 6 votes vote down vote up
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));
    }
}
 
Example 4
Project: lams   File: DisjunctionMaxQueryBuilder.java   Source Code and License 6 votes vote down vote up
@Override
public Query getQuery(Element e) throws ParserException {
  float tieBreaker = DOMUtils.getAttribute(e, "tieBreaker", 0.0f); 
  DisjunctionMaxQuery dq = new DisjunctionMaxQuery(tieBreaker);
  dq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f));

  NodeList nl = e.getChildNodes();
  for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element) { // all elements are disjuncts.
      Element queryElem = (Element) node;
      Query q = factory.getQuery(queryElem);
      dq.add(q);
    }
  }

  return dq;
}
 
Example 5
Project: Elasticsearch   File: PercolatorQueriesRegistry.java   Source Code and License 6 votes vote down vote up
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);
    }
}
 
Example 6
Project: lams   File: SpanNotQuery.java   Source Code and License 6 votes vote down vote up
@Override
public Query rewrite(IndexReader reader) throws IOException {
  SpanNotQuery clone = null;

  SpanQuery rewrittenInclude = (SpanQuery) include.rewrite(reader);
  if (rewrittenInclude != include) {
    clone = this.clone();
    clone.include = rewrittenInclude;
  }
  SpanQuery rewrittenExclude = (SpanQuery) exclude.rewrite(reader);
  if (rewrittenExclude != exclude) {
    if (clone == null) clone = this.clone();
    clone.exclude = rewrittenExclude;
  }

  if (clone != null) {
    return clone;                        // some clauses rewrote
  } else {
    return this;                         // no clauses rewrote
  }
}
 
Example 7
Project: sjk   File: QuickTipsService1Impl.java   Source Code and License 6 votes vote down vote up
@Override
public ScoreDoc[] prefixSearch(String q) throws IOException {
    if (StringUtils.isEmpty(q) || q.length() > appConfig.getKeywordMaxLength()) {
        logger.error("empty keywords or over-length! {}", q);
        return null;
    }

    final TopDocs[] rstTopDocs = new TopDocs[2];
    final Query nameFldQuery = new PrefixQuery(new Term(NAME.getName(), q));
    rstTopDocs[0] = indexSearcher.search(nameFldQuery, appConfig.getQuickTipsNum() * 2, sort);

    final Query downLoadRankQuery = NumericRangeQuery.newIntRange(DOWNOLOAD_RANK.getName(), MIN_DOWNLOAD_RANK,
            Integer.MAX_VALUE, true, false);
    // 从下载量最高的1000条记录中,再过滤符合关键字的记录
    rstTopDocs[1] = indexSearcher.search(downLoadRankQuery, MAX_TOP, sort);
    TopDocs rst = TopDocsUtil.mergeDuplicateDocId(TopDocs.merge(sort, MAX_TOP + appConfig.getQuickTipsNum() * 2,
            rstTopDocs));
    if (rst != null) {
        return rst.scoreDocs;
    }
    return null;
}
 
Example 8
Project: elasticsearch_my   File: Lucene.java   Source Code and License 6 votes vote down vote up
/**
 * Check whether there is one or more documents matching the provided query.
 */
public static boolean exists(IndexSearcher searcher, Query query) throws IOException {
    final Weight weight = searcher.createNormalizedWeight(query, false);
    // the scorer API should be more efficient at stopping after the first
    // match than the bulk scorer API
    for (LeafReaderContext context : searcher.getIndexReader().leaves()) {
        final Scorer scorer = weight.scorer(context);
        if (scorer == null) {
            continue;
        }
        final Bits liveDocs = context.reader().getLiveDocs();
        final DocIdSetIterator iterator = scorer.iterator();
        for (int doc = iterator.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = iterator.nextDoc()) {
            if (liveDocs == null || liveDocs.get(doc)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 9
Project: Elasticsearch   File: DocumentMapper.java   Source Code and License 6 votes vote down vote up
/**
 * Returns the best nested {@link ObjectMapper} instances that is in the scope of the specified nested docId.
 */
public ObjectMapper findNestedObjectMapper(int nestedDocId, SearchContext sc, LeafReaderContext context) throws IOException {
    ObjectMapper nestedObjectMapper = null;
    for (ObjectMapper objectMapper : objectMappers().values()) {
        if (!objectMapper.nested().isNested()) {
            continue;
        }

        Query filter = objectMapper.nestedTypeFilter();
        if (filter == null) {
            continue;
        }
        // We can pass down 'null' as acceptedDocs, because nestedDocId is a doc to be fetched and
        // therefor is guaranteed to be a live doc.
        final Weight nestedWeight = filter.createWeight(sc.searcher(), false);
        Scorer scorer = nestedWeight.scorer(context);
        if (scorer == null) {
            continue;
        }

        if (scorer.iterator().advance(nestedDocId) == nestedDocId) {
            if (nestedObjectMapper == null) {
                nestedObjectMapper = objectMapper;
            } else {
                if (nestedObjectMapper.fullPath().length() < objectMapper.fullPath().length()) {
                    nestedObjectMapper = objectMapper;
                }
            }
        }
    }
    return nestedObjectMapper;
}
 
Example 10
Project: elasticsearch_my   File: TermsQueryBuilder.java   Source Code and License 6 votes vote down vote up
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    if (termsLookup != null) {
        throw new UnsupportedOperationException("query must be rewritten first");
    }
    if (values == null || values.isEmpty()) {
        return Queries.newMatchNoDocsQuery("No terms supplied for \"" + getName() + "\" query.");
    }
    MappedFieldType fieldType = context.fieldMapper(fieldName);

    if (fieldType != null) {
        return fieldType.termsQuery(values, context);
    } else {
        BytesRef[] filterValues = new BytesRef[values.size()];
        for (int i = 0; i < filterValues.length; i++) {
            filterValues[i] = BytesRefs.toBytesRef(values.get(i));
        }
        return new TermInSetQuery(fieldName, filterValues);
    }
}
 
Example 11
Project: Elasticsearch   File: FreqTermsEnum.java   Source Code and License 6 votes vote down vote up
public FreqTermsEnum(IndexReader reader, String field, boolean needDocFreq, boolean needTotalTermFreq, @Nullable Query filter, BigArrays bigArrays) throws IOException {
    super(reader, field, needTotalTermFreq ? PostingsEnum.FREQS : PostingsEnum.NONE, filter);
    this.bigArrays = bigArrays;
    this.needDocFreqs = needDocFreq;
    this.needTotalTermFreqs = needTotalTermFreq;
    if (needDocFreq) {
        termDocFreqs = bigArrays.newIntArray(INITIAL_NUM_TERM_FREQS_CACHED, false);
    } else {
        termDocFreqs = null;
    }
    if (needTotalTermFreq) {
        termsTotalFreqs = bigArrays.newLongArray(INITIAL_NUM_TERM_FREQS_CACHED, false);
    } else {
        termsTotalFreqs = null;
    }
    cachedTermOrds = new BytesRefHash(INITIAL_NUM_TERM_FREQS_CACHED, bigArrays);
}
 
Example 12
Project: sjk   File: SearchServiceImpl.java   Source Code and License 6 votes vote down vote up
/**
 * 查询索引
 * 
 * @param keywords
 * @return
 * @throws Exception
 */
public List<Document> searchIndex(Integer typeId, String keywords) throws Exception {
    // 1.init searcher
    Analyzer analyzer = new PaodingAnalyzer();
    IndexReader reader = IndexReader.open(typeId == appConfig.getGameTypeId() ? appConfig.getGameIndexDir()
            : appConfig.getSoftIndexDir());
    BooleanClause.Occur[] flags = new BooleanClause.Occur[] { BooleanClause.Occur.SHOULD,
            BooleanClause.Occur.SHOULD };
    Query query = MultiFieldQueryParser.parse(keywords, appConfig.getQueryFields(), flags, analyzer);
    query = query.rewrite(reader);

    // 2.search
    List<Document> docs = new ArrayList<Document>();
    Hits hits = (typeId == appConfig.getGameTypeId() ? gameSearcher.search(query, Sort.RELEVANCE) : softSearcher
            .search(query, Sort.RELEVANCE));// searcher.search(query,
                                            // Sort.RELEVANCE);
    for (int i = 0; i < hits.length(); i++) {
        docs.add(hits.doc(i));
    }

    // 3.return
    reader.close();
    return docs;
}
 
Example 13
Project: Elasticsearch   File: MapperQueryParser.java   Source Code and License 6 votes vote down vote up
private Query applySlop(Query q, int slop) {
    if (q instanceof PhraseQuery) {
        PhraseQuery pq = (PhraseQuery) q;
        PhraseQuery.Builder builder = new PhraseQuery.Builder();
        builder.setSlop(slop);
        final Term[] terms = pq.getTerms();
        final int[] positions = pq.getPositions();
        for (int i = 0; i < terms.length; ++i) {
            builder.add(terms[i], positions[i]);
        }
        pq = builder.build();
        pq.setBoost(q.getBoost());
        return pq;
    } else if (q instanceof MultiPhraseQuery) {
        ((MultiPhraseQuery) q).setSlop(slop);
        return q;
    } else {
        return q;
    }
}
 
Example 14
Project: para-search-elasticsearch   File: ElasticSearchUtils.java   Source Code and License 5 votes vote down vote up
static Query qsParsed(String query) {
	if (StringUtils.isBlank(query) || "*".equals(query.trim())) {
		return null;
	}
	try {
		StandardQueryParser parser = new StandardQueryParser();
		parser.setAllowLeadingWildcard(false);
		return parser.parse(query, "");
	} catch (Exception ex) {
		logger.warn("Failed to parse query string '{}'.", query);
	}
	return null;
}
 
Example 15
Project: elasticsearch_my   File: StringFieldType.java   Source Code and License 5 votes vote down vote up
@Override
public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) {
    failIfNotIndexed();
    return new TermRangeQuery(name(),
        lowerTerm == null ? null : indexedValueForSearch(lowerTerm),
        upperTerm == null ? null : indexedValueForSearch(upperTerm),
        includeLower, includeUpper);
}
 
Example 16
Project: elasticsearch_my   File: Queries.java   Source Code and License 5 votes vote down vote up
public static BooleanQuery filtered(@Nullable Query query, @Nullable Query filter) {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    if (query != null) {
        builder.add(new BooleanClause(query, Occur.MUST));
    }
    if (filter != null) {
        builder.add(new BooleanClause(filter, Occur.FILTER));
    }
    return builder.build();
}
 
Example 17
Project: amanda   File: ArticleRepositoryImpl.java   Source Code and License 5 votes vote down vote up
@Override
public Page<SearchResult<Article>> search(String query, Pageable pageable) {
    FullTextEntityManager manager = this.getFullTextEntityManager();

    QueryBuilder builder = manager.getSearchFactory().buildQueryBuilder()
            .forEntity(Article.class).get();

    Query luceneQuery = builder.keyword()
            .onFields("title", "keywords", "abstractContent", "category.name", "category.zhName", "mdBody")
            .matching(query)
            .createQuery();

    FullTextQuery fullTextQuery = manager.createFullTextQuery(luceneQuery, Article.class);
    // fullTextQuery.enableFullTextFilter("published").setParameter( "published", true );

    fullTextQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SCORE);

    long total = fullTextQuery.getResultSize();

    fullTextQuery.setFirstResult(pageable.getOffset())
            .setMaxResults(pageable.getPageSize());

    @SuppressWarnings("unchecked")
    List<Object[]> results = fullTextQuery.getResultList();
    List<SearchResult<Article>> list = new ArrayList<>();
    results.forEach(o -> list.add(
            new SearchResult<>((Article) o[0], (Float)o[1])
    ));

    return new PageImpl<>(list, pageable, total);
}
 
Example 18
Project: incubator-netbeans   File: PersistentClassIndex.java   Source Code and License 5 votes vote down vote up
@Override
public <T> void getDeclaredElements (
        final String ident,
        final ClassIndex.NameKind kind,
        final Convertor<? super Document, T> convertor,
        final Map<T,Set<String>> result) throws InterruptedException, IOException {
    final Pair<Convertor<? super Document, T>,Index> ctu = indexPath.getPatch(convertor);
    try {
        IndexManager.priorityAccess(() -> {
            final Query query = Queries.createTermCollectingQuery(
                    DocumentUtil.FIELD_FEATURE_IDENTS,
                    DocumentUtil.FIELD_CASE_INSENSITIVE_FEATURE_IDENTS,
                    ident,
                    DocumentUtil.translateQueryKind(kind));
            index.queryDocTerms(
                    result,
                    ctu.first(),
                    Term::text,
                    DocumentUtil.declaredTypesFieldSelector(false, false),
                    cancel.get(),
                    query);
            if (ctu.second() != null) {
                ctu.second().queryDocTerms(
                        result,
                        convertor,
                        Term::text,
                        DocumentUtil.declaredTypesFieldSelector(false, false),
                        cancel.get(),
                        query);
            }
            return null;
        });
    } catch (IOException ioe) {
        this.<Void,IOException>handleException(null, ioe, root);
    }
}
 
Example 19
Project: lams   File: MultiFieldQueryParser.java   Source Code and License 5 votes vote down vote up
@Override
protected Query getRegexpQuery(String field, String termStr)
    throws ParseException {
  if (field == null) {
    List<BooleanClause> clauses = new ArrayList<>();
    for (int i = 0; i < fields.length; i++) {
      clauses.add(new BooleanClause(getRegexpQuery(fields[i], termStr),
          BooleanClause.Occur.SHOULD));
    }
    return getBooleanQuery(clauses, true);
  }
  return super.getRegexpQuery(field, termStr);
}
 
Example 20
Project: Elasticsearch   File: MapperQueryParser.java   Source Code and License 5 votes vote down vote up
/**
 * We override this one so we can get the fuzzy part to be treated as string, so people can do: "age:10~5" or "timestamp:2012-10-10~5d"
 */
@Override
Query handleBareFuzzy(String qfield, Token fuzzySlop, String termImage) throws ParseException {
    if (fuzzySlop.image.length() == 1) {
        return getFuzzyQuery(qfield, termImage, Float.toString(fuzzyMinSim));
    }
    return getFuzzyQuery(qfield, termImage, fuzzySlop.image.substring(1));
}
 
Example 21
Project: joai-project   File: LuceneGeospatialQueryConverter.java   Source Code and License 5 votes vote down vote up
/**
 *  Test driver.
 *
 * @param  args  The command line arguments
 */
public static void main(String[] args) {
	if (args.length != 5) {
		System.err.println(
			"usage: java edu.ucsb.adl.LuceneGeospatialQueryConverter " +
			"predicate north south east west");
		System.exit(1);
	}
	Query q = convertQuery("northCoord", "southCoord", "eastCoord", "westCoord", args[0],
		Double.valueOf(args[1]).doubleValue(),
		Double.valueOf(args[2]).doubleValue(),
		Double.valueOf(args[3]).doubleValue(),
		Double.valueOf(args[4]).doubleValue());
	System.out.println(q.toString());
}
 
Example 22
Project: Elasticsearch   File: DefaultSearchContext.java   Source Code and License 5 votes vote down vote up
@Override
public Query searchFilter(String[] types) {
    Query filter = mapperService().searchFilter(types);
    if (filter == null && aliasFilter == null) {
        return null;
    }
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    if (filter != null) {
        bq.add(filter, Occur.MUST);
    }
    if (aliasFilter != null) {
        bq.add(aliasFilter, Occur.MUST);
    }
    return new ConstantScoreQuery(bq.build());
}
 
Example 23
Project: para-search-elasticsearch   File: ElasticSearchUtils.java   Source Code and License 5 votes vote down vote up
/**
 * @param query query string
 * @return a list of composite queries for matching nested objects
 */
static QueryBuilder convertQueryStringToNestedQuery(String query) {
	String queryStr = StringUtils.trimToEmpty(query).replaceAll("\\[(\\d+)\\]", "-$1");
	Query q = qsParsed(queryStr);
	if (q == null) {
		return matchAllQuery();
	}
	try {
		return rewriteQuery(q, 0);
	} catch (Exception e) {
		logger.warn(e.getMessage());
		return null;
	}
}
 
Example 24
Project: jdg-lab   File: IndexedCacheBeerService.java   Source Code and License 5 votes vote down vote up
public List<Beer> getAllBeers(boolean desc) {
    SearchManager searchManager = Search.getSearchManager(cache);
    QueryBuilder qb = searchManager.buildQueryBuilderForClass(Beer.class).get();
    Query query = qb.all().createQuery();
    CacheQuery cq = searchManager.getQuery(query); //cq is a CacheQuery wrapper of a Lucene query
    if(desc){
        Sort sort = new Sort(new SortField("id", SortField.Type.LONG));
        cq.sort(sort);
    }
    List<Beer> result = (List<Beer>)(List)cq.list();
    return result;
}
 
Example 25
Project: lams   File: SimpleQueryParser.java   Source Code and License 5 votes vote down vote up
/**
 * Factory method to generate a prefix query.
 */
protected Query newPrefixQuery(String text) {
  BooleanQuery bq = new BooleanQuery(true);
  for (Map.Entry<String,Float> entry : weights.entrySet()) {
    PrefixQuery prefix = new PrefixQuery(new Term(entry.getKey(), text));
    prefix.setBoost(entry.getValue());
    bq.add(prefix, BooleanClause.Occur.SHOULD);
  }
  return simplify(bq);
}
 
Example 26
Project: elasticsearch_my   File: MultiMatchQueryTests.java   Source Code and License 5 votes vote down vote up
public void testMultiMatchCrossFieldsWithSynonyms() throws IOException {
    QueryShardContext queryShardContext = indexService.newQueryShardContext(
        randomInt(20), null, () -> { throw new UnsupportedOperationException(); });

    // check that synonym query is used for a single field
    Query parsedQuery =
        multiMatchQuery("quick").field("name.first")
            .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
    Term[] terms = new Term[2];
    terms[0] = new Term("name.first", "quick");
    terms[1] = new Term("name.first", "fast");
    Query expectedQuery = new SynonymQuery(terms);
    assertThat(parsedQuery, equalTo(expectedQuery));

    // check that blended term query is used for multiple fields
    parsedQuery =
        multiMatchQuery("quick").field("name.first").field("name.last")
            .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS).toQuery(queryShardContext);
    terms = new Term[4];
    terms[0] = new Term("name.first", "quick");
    terms[1] = new Term("name.first", "fast");
    terms[2] = new Term("name.last", "quick");
    terms[3] = new Term("name.last", "fast");
    float[] boosts = new float[4];
    Arrays.fill(boosts, 1.0f);
    expectedQuery = BlendedTermQuery.dismaxBlendedQuery(terms, boosts, 1.0f);
    assertThat(parsedQuery, equalTo(expectedQuery));

}
 
Example 27
Project: elasticsearch_my   File: MultiMatchQueryBuilderTests.java   Source Code and License 5 votes vote down vote up
public void testToQueryMultipleTermsBooleanQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    Query query = multiMatchQuery("test1 test2").field(STRING_FIELD_NAME).useDisMax(false).toQuery(createShardContext());
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery bQuery = (BooleanQuery) query;
    assertThat(bQuery.clauses().size(), equalTo(2));
    assertThat(assertBooleanSubQuery(query, TermQuery.class, 0).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "test1")));
    assertThat(assertBooleanSubQuery(query, TermQuery.class, 1).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "test2")));
}
 
Example 28
Project: elasticsearch_my   File: IdsQueryBuilderTests.java   Source Code and License 5 votes vote down vote up
@Override
protected void doAssertLuceneQuery(IdsQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    if (queryBuilder.ids().size() == 0) {
        assertThat(query, instanceOf(MatchNoDocsQuery.class));
    } else {
        assertThat(query, instanceOf(TermInSetQuery.class));
    }
}
 
Example 29
Project: dacapobench   File: Search.java   Source Code and License 5 votes vote down vote up
public void run() throws java.io.IOException {
  Analyzer analyzer = new StandardAnalyzer();
  QueryParser parser = new QueryParser(field, analyzer);

  while (true) {
    String line = in.readLine();

    if (line == null || line.length() == -1)
      break;

    line = line.trim();
    if (line.length() == 0)
      break;

    Query query = null;
    try {
      query = parser.parse(line);
    } catch (Exception e) {
      e.printStackTrace();
    }
    searcher.search(query, null, 10);

    doPagingSearch(query);
  }

  reader.close();
  out.flush();
  out.close();
  synchronized (parent) {
    parent.completed++;
    if (parent.completed % 4 == 0) {
      System.out.println(parent.completed + " query batches completed");
    }
    parent.notify();
  }
}
 
Example 30
Project: elasticsearch_my   File: MapperQueryParser.java   Source Code and License 5 votes vote down vote up
/**
 * We override this one so we can get the fuzzy part to be treated as string,
 * so people can do: "age:10~5" or "timestamp:2012-10-10~5d"
 */
@Override
Query handleBareFuzzy(String qfield, Token fuzzySlop, String termImage) throws ParseException {
    if (fuzzySlop.image.length() == 1) {
        return getFuzzyQuery(qfield, termImage, Float.toString(settings.fuzziness().asDistance(termImage)));
    }
    return getFuzzyQuery(qfield, termImage, fuzzySlop.image.substring(1));
}
 
Example 31
Project: Elasticsearch   File: MultiMatchQuery.java   Source Code and License 5 votes vote down vote up
public List<Query> buildGroupedQueries(MultiMatchQueryBuilder.Type type, Map<String, Float> fieldNames, Object value, String minimumShouldMatch) throws IOException{
    List<Query> queries = new ArrayList<>();
    for (String fieldName : fieldNames.keySet()) {
        Float boostValue = fieldNames.get(fieldName);
        Query query = parseGroup(type.matchQueryType(), fieldName, boostValue, value, minimumShouldMatch);
        if (query != null) {
            queries.add(query);
        }
    }
    return queries;
}
 
Example 32
Project: Elasticsearch   File: MatchAllQueryParser.java   Source Code and License 5 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    float boost = 1.0f;
    String currentFieldName = null;

    XContentParser.Token token;
    while (((token = parser.nextToken()) != XContentParser.Token.END_OBJECT && token != XContentParser.Token.END_ARRAY)) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new QueryParsingException(parseContext, "[match_all] query does not support [" + currentFieldName + "]");
            }
        }
    }

    if (boost == 1.0f) {
        return Queries.newMatchAllQuery();
    }

    MatchAllDocsQuery query = new MatchAllDocsQuery();
    query.setBoost(boost);
    return query;
}
 
Example 33
Project: TextHIN   File: FbEntitySearcher.java   Source Code and License 5 votes vote down vote up
private ScoreDoc[] getHits(String question) throws IOException {
 try {
  Query luceneQuery = queryParser.parse(question);
  ScoreDoc[] hits = indexSearcher.search(luceneQuery, numOfDocs).scoreDocs;
  return hits;
 } 
 catch (ParseException e) {
  System.out.println("ParseException question = " + question);
  e.printStackTrace();
  return new ScoreDoc[0];
 }
  
}
 
Example 34
Project: Elasticsearch   File: ChildrenConstantScoreQuery.java   Source Code and License 5 votes vote down vote up
public ChildrenConstantScoreQuery(IndexParentChildFieldData parentChildIndexFieldData, Query childQuery, String parentType, String childType, Filter parentFilter, int shortCircuitParentDocSet, BitSetProducer nonNestedDocsFilter) {
    this.parentChildIndexFieldData = parentChildIndexFieldData;
    this.parentFilter = parentFilter;
    this.parentType = parentType;
    this.childType = childType;
    this.childQuery = childQuery;
    this.shortCircuitParentDocSet = shortCircuitParentDocSet;
    this.nonNestedDocsFilter = nonNestedDocsFilter;
}
 
Example 35
Project: elasticsearch_my   File: FiltersFunctionScoreQuery.java   Source Code and License 5 votes vote down vote up
public FiltersFunctionScoreQuery(Query subQuery, ScoreMode scoreMode, FilterFunction[] filterFunctions, float maxBoost, Float minScore, CombineFunction combineFunction) {
    this.subQuery = subQuery;
    this.scoreMode = scoreMode;
    this.filterFunctions = filterFunctions;
    this.maxBoost = maxBoost;
    this.combineFunction = combineFunction;
    this.minScore = minScore;
}
 
Example 36
Project: marathonv5   File: IndexSearcher.java   Source Code and License 5 votes vote down vote up
public Map<DocumentType, List<SearchResult>> search(String searchString) throws ParseException {
    Map<DocumentType, List<SearchResult>> resultMap = new TreeMap<DocumentType, List<SearchResult>>();
    try {
        Query query = parser.parse(searchString);
        final SecondPassGroupingCollector collector = new SecondPassGroupingCollector("documentType", searchGroups,
                Sort.RELEVANCE, null, 5, true, false, true);
        searcher.search(query, collector);
        final TopGroups groups = collector.getTopGroups(0);
        for (GroupDocs groupDocs : groups.groups) {
            DocumentType docType = DocumentType.valueOf(groupDocs.groupValue);
            List<SearchResult> results = new ArrayList<SearchResult>();
            for (ScoreDoc scoreDoc : groupDocs.scoreDocs) {
                Document doc = searcher.doc(scoreDoc.doc);
                SearchResult result = new SearchResult(
                        docType,
                        doc.get("name"),
                        doc.get("url"),
                        doc.get("className"),
                        doc.get("package"),
                        doc.get("ensemblePath"),
                        doc.get("shortDescription")
                );
                results.add(result);
            }
            resultMap.put(docType, results);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return resultMap;
}
 
Example 37
Project: elasticsearch_my   File: MultiMatchQueryBuilderTests.java   Source Code and License 5 votes vote down vote up
@Override
protected void doAssertLuceneQuery(MultiMatchQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    // we rely on integration tests for deeper checks here
    assertThat(query, either(instanceOf(BoostQuery.class)).or(instanceOf(TermQuery.class)).or(instanceOf(AllTermQuery.class))
            .or(instanceOf(BooleanQuery.class)).or(instanceOf(DisjunctionMaxQuery.class))
            .or(instanceOf(FuzzyQuery.class)).or(instanceOf(MultiPhrasePrefixQuery.class))
            .or(instanceOf(MatchAllDocsQuery.class)).or(instanceOf(ExtendedCommonTermsQuery.class))
            .or(instanceOf(MatchNoDocsQuery.class)).or(instanceOf(PhraseQuery.class))
            .or(instanceOf(LegacyNumericRangeQuery.class))
            .or(instanceOf(PointRangeQuery.class)).or(instanceOf(IndexOrDocValuesQuery.class)));
}
 
Example 38
Project: elasticsearch_my   File: MatchPhrasePrefixQueryBuilder.java   Source Code and License 5 votes vote down vote up
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    // validate context specific fields
    if (analyzer != null && context.getIndexAnalyzers().get(analyzer) == null) {
        throw new QueryShardException(context, "[" + NAME + "] analyzer [" + analyzer + "] not found");
    }

    MatchQuery matchQuery = new MatchQuery(context);
    matchQuery.setAnalyzer(analyzer);
    matchQuery.setPhraseSlop(slop);
    matchQuery.setMaxExpansions(maxExpansions);

    return matchQuery.parse(MatchQuery.Type.PHRASE_PREFIX, fieldName, value);
}
 
Example 39
Project: elasticsearch_my   File: MultiMatchQueryBuilderTests.java   Source Code and License 5 votes vote down vote up
public void testToQueryMultipleFieldsBooleanQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    Query query = multiMatchQuery("test").field(STRING_FIELD_NAME).field(STRING_FIELD_NAME_2).useDisMax(false).toQuery(createShardContext());
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery bQuery = (BooleanQuery) query;
    assertThat(bQuery.clauses().size(), equalTo(2));
    assertThat(assertBooleanSubQuery(query, TermQuery.class, 0).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "test")));
    assertThat(assertBooleanSubQuery(query, TermQuery.class, 1).getTerm(), equalTo(new Term(STRING_FIELD_NAME_2, "test")));
}
 
Example 40
Project: elasticsearch_my   File: QueryStringQueryBuilderTests.java   Source Code and License 5 votes vote down vote up
public void testToQueryMultipleFieldsDisMaxQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    Query query = queryStringQuery("test").field(STRING_FIELD_NAME).field(STRING_FIELD_NAME_2)
        .useDisMax(true)
        .toQuery(createShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disMaxQuery = (DisjunctionMaxQuery) query;
    List<Query> disjuncts = disMaxQuery.getDisjuncts();
    assertThat(((TermQuery) disjuncts.get(0)).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "test")));
    assertThat(((TermQuery) disjuncts.get(1)).getTerm(), equalTo(new Term(STRING_FIELD_NAME_2, "test")));
}
 
Example 41
Project: lams   File: BufferedUpdates.java   Source Code and License 5 votes vote down vote up
public void addQuery(Query query, int docIDUpto) {
  Integer current = queries.put(query, docIDUpto);
  // increment bytes used only if the query wasn't added so far.
  if (current == null) {
    bytesUsed.addAndGet(BYTES_PER_DEL_QUERY);
  }
}
 
Example 42
Project: elasticsearch_my   File: AdjacencyMatrixAggregatorFactory.java   Source Code and License 5 votes vote down vote up
public AdjacencyMatrixAggregatorFactory(String name, List<KeyedFilter> filters, String separator, 
        SearchContext context, AggregatorFactory<?> parent, AggregatorFactories.Builder subFactories, 
        Map<String, Object> metaData) throws IOException {
    super(name, context, parent, subFactories, metaData);
    IndexSearcher contextSearcher = context.searcher();
    this.separator = separator;
    weights = new Weight[filters.size()];
    keys = new String[filters.size()];
    for (int i = 0; i < filters.size(); ++i) {
        KeyedFilter keyedFilter = filters.get(i);
        this.keys[i] = keyedFilter.key();
        Query filter = keyedFilter.filter().toFilter(context.getQueryShardContext());
        this.weights[i] = contextSearcher.createNormalizedWeight(filter, false);
    }
}
 
Example 43
Project: elasticsearch_my   File: NumberFieldTypeTests.java   Source Code and License 5 votes vote down vote up
public void testRangeQuery() {
    MappedFieldType ft = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    Query expected = new IndexOrDocValuesQuery(
            LongPoint.newRangeQuery("field", 1, 3),
            SortedNumericDocValuesField.newRangeQuery("field", 1, 3));
    assertEquals(expected, ft.rangeQuery("1", "3", true, true, null));

    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
            () -> ft.rangeQuery("1", "3", true, true, null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
 
Example 44
Project: elasticsearch_my   File: FieldNamesFieldMapper.java   Source Code and License 5 votes vote down vote up
@Override
public Query termQuery(Object value, QueryShardContext context) {
    if (isEnabled() == false) {
        throw new IllegalStateException("Cannot run [exists] queries if the [_field_names] field is disabled");
    }
    return super.termQuery(value, context);
}
 
Example 45
Project: elasticsearch_my   File: MultiMatchQueryBuilderTests.java   Source Code and License 5 votes vote down vote up
public void testToQueryFieldsWildcard() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    Query query = multiMatchQuery("test").field("mapped_str*").useDisMax(false).toQuery(createShardContext());
    assertThat(query, instanceOf(BooleanQuery.class));
    BooleanQuery bQuery = (BooleanQuery) query;
    assertThat(bQuery.clauses().size(), equalTo(2));
    assertThat(assertBooleanSubQuery(query, TermQuery.class, 0).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "test")));
    assertThat(assertBooleanSubQuery(query, TermQuery.class, 1).getTerm(), equalTo(new Term(STRING_FIELD_NAME_2, "test")));
}
 
Example 46
Project: elasticsearch_my   File: Queries.java   Source Code and License 5 votes vote down vote up
public static boolean isConstantMatchAllQuery(Query query) {
    if (query instanceof ConstantScoreQuery) {
        return isConstantMatchAllQuery(((ConstantScoreQuery) query).getQuery());
    } else if (query instanceof MatchAllDocsQuery) {
        return true;
    }
    return false;
}
 
Example 47
Project: elasticsearch_my   File: SpanMultiTermQueryBuilderTests.java   Source Code and License 4 votes vote down vote up
public void testToQueryInnerSpanMultiTerm() throws IOException {
    Query query = new SpanOrQueryBuilder(createTestQueryBuilder()).toQuery(createShardContext());
    //verify that the result is still a span query, despite the boost that might get set (SpanBoostQuery rather than BoostQuery)
    assertThat(query, instanceOf(SpanQuery.class));
}
 
Example 48
Project: Elasticsearch   File: AllFieldMapper.java   Source Code and License 4 votes vote down vote up
@Override
public Query queryStringTermQuery(Term term) {
    return new AllTermQuery(term);
}
 
Example 49
Project: Equella   File: ItemIndex.java   Source Code and License 4 votes vote down vote up
public String suggestTerm(final Search request, final String prefix, final boolean isSearchAttachment)
{
	return search(new Searcher<String>()
	{
		@Override
		public String search(IndexSearcher searcher) throws IOException
		{
			// Get the reader
			IndexReader reader = searcher.getIndexReader();

			// Get all the docs from the filters
			Collection<Filter> filters = getFilters(request);
			Query query = getQuery(request, reader, isSearchAttachment);
			filters.add(new QueryWrapperFilter(query));
			ChainedFilter chain = new ChainedFilter(filters.toArray(new Filter[filters.size()]), ChainedFilter.AND);
			DocIdSetIterator iterator = chain.getDocIdSet(reader).iterator();

			// Get docs that contain terms that begin with the prefix
			List<Term> termList = Lists.newArrayList();
			termList.add(new Term(FreeTextQuery.FIELD_BODY_NOSTEM, prefix));
			termList.add(new Term(FreeTextQuery.FIELD_ATTACHMENT_VECTORED_NOSTEM, prefix));

			for( Term term : termList )
			{
				TermDocs termDocs = reader.termDocs(term);
				TermEnum terms = reader.terms(term);

				// Check if doc is in the filter and return the term
				Term t;
				for( t = terms.term(); t != null && t.text().startsWith(prefix); t = terms.term() )
				{
					termDocs.seek(t);
					while( termDocs.next() )
					{
						int docId = termDocs.doc();
						if( docId == iterator.advance(docId) )
						{
							return t.text();
						}
					}
					terms.next();
				}
			}
			return "";
		}
	});
}
 
Example 50
Project: elasticsearch_my   File: IndicesQueryCache.java   Source Code and License 4 votes vote down vote up
@Override
protected void onMiss(Object readerCoreKey, Query filter) {
    super.onMiss(readerCoreKey, filter);
    final Stats shardStats = getOrCreateStats(readerCoreKey);
    shardStats.missCount += 1;
}
 
Example 51
Project: elasticsearch_my   File: MatchAllQueryBuilderTests.java   Source Code and License 4 votes vote down vote up
@Override
protected void doAssertLuceneQuery(MatchAllQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    assertThat(query, instanceOf(MatchAllDocsQuery.class));
}
 
Example 52
Project: elasticsearch_my   File: CustomQueryScorer.java   Source Code and License 4 votes vote down vote up
public CustomQueryScorer(Query query, String field) {
    super(query, field);
}
 
Example 53
Project: elasticsearch_my   File: Murmur3FieldMapper.java   Source Code and License 4 votes vote down vote up
@Override
public Query termQuery(Object value, QueryShardContext context) {
    throw new QueryShardException(context, "Murmur3 fields are not searchable: [" + name() + "]");
}
 
Example 54
Project: elasticsearch_my   File: GeoShapeFieldMapper.java   Source Code and License 4 votes vote down vote up
@Override
public Query termQuery(Object value, QueryShardContext context) {
    throw new QueryShardException(context, "Geo fields do not support exact searching, use dedicated geo queries instead");
}
 
Example 55
Project: Elasticsearch   File: FiltersFunctionScoreQuery.java   Source Code and License 4 votes vote down vote up
public FilterFunction(Query filter, ScoreFunction function) {
    this.filter = filter;
    this.function = function;
}
 
Example 56
Project: elasticsearch_my   File: BoostingQueryBuilder.java   Source Code and License 4 votes vote down vote up
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    Query positive = positiveQuery.toQuery(context);
    Query negative = negativeQuery.toQuery(context);
    return new BoostingQuery(positive, negative, negativeBoost);
}
 
Example 57
Project: Elasticsearch   File: ArrayFieldType.java   Source Code and License 4 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    return innerFieldType.termQuery(value, context);
}
 
Example 58
Project: elasticsearch_my   File: GeoBoundingBoxQueryBuilderTests.java   Source Code and License 4 votes vote down vote up
private void assertGeoBoundingBoxQuery(String query) throws IOException {
    QueryShardContext shardContext = createShardContext();
    Query parsedQuery = parseQuery(query).toQuery(shardContext);
}
 
Example 59
Project: incubator-netbeans   File: Queries.java   Source Code and License 4 votes vote down vote up
private static Query createQueryImpl(
        final @NonNull String fieldName,
        final @NonNull String caseInsensitiveFieldName,
        final @NonNull String value,
        final @NonNull QueryKind kind,
        final @NonNull QueryFactory f,
        final @NonNull Map<String,Object> options) {
    switch (kind) {
        case EXACT:
                return f.createTermQuery(fieldName, value);
        case PREFIX:
            if (value.length() == 0) {
                return f.createAllDocsQuery(fieldName);
            }
            else {
                return f.createPrefixQuery(fieldName, value);
            }
        case CASE_INSENSITIVE_PREFIX:
            if (value.length() == 0) {
                return f.createAllDocsQuery(caseInsensitiveFieldName);
            }
            else {
                return f.createPrefixQuery(caseInsensitiveFieldName, value.toLowerCase());
            }
        case CAMEL_CASE:
            if (value.length() == 0) {
                throw new IllegalArgumentException ();
            } else {
                return f.createRegExpQuery(fieldName,createCamelCaseRegExp(value, getOption(options, OPTION_CAMEL_CASE_SEPARATOR, String.class), getOption(options, OPTION_CAMEL_CASE_PART, String.class), true), true);
            }
        case CASE_INSENSITIVE_REGEXP:
            if (value.length() == 0) {
                throw new IllegalArgumentException ();
            } else {
                return f.createRegExpQuery(caseInsensitiveFieldName, value.toLowerCase(), false);
            }
        case REGEXP:
            if (value.length() == 0) {
                throw new IllegalArgumentException ();
            } else {
                return f.createRegExpQuery(fieldName, value, true);
            }
        case CASE_INSENSITIVE_CAMEL_CASE:
            if (value.length() == 0) {
                //Special case (all) handle in different way
                return f.createAllDocsQuery(caseInsensitiveFieldName);
            } else {
                return f.createRegExpQuery(caseInsensitiveFieldName, createCamelCaseRegExp(value, getOption(options, OPTION_CAMEL_CASE_SEPARATOR, String.class), getOption(options, OPTION_CAMEL_CASE_PART, String.class), false), false);
            }
        default:
            throw new UnsupportedOperationException (kind.toString());
    }
}
 
Example 60
Project: elasticsearch_my   File: ESToParentBlockJoinQueryTests.java   Source Code and License 4 votes vote down vote up
public void testEquals() {
    Query q1 = new ESToParentBlockJoinQuery(
            new TermQuery(new Term("is", "child")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
            ScoreMode.Avg, "nested");

    Query q2 = new ESToParentBlockJoinQuery(
            new TermQuery(new Term("is", "child")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
            ScoreMode.Avg, "nested");
    assertEquals(q1, q2);
    assertEquals(q1.hashCode(), q2.hashCode());

    Query q3 = new ESToParentBlockJoinQuery(
            new TermQuery(new Term("is", "not_child")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
            ScoreMode.Avg, "nested");
    assertFalse(q1.equals(q3));
    assertFalse(q1.hashCode() == q3.hashCode());

    Query q4 = new ESToParentBlockJoinQuery(
            new TermQuery(new Term("is", "child")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "other_parent"))),
            ScoreMode.Avg, "nested");
    assertFalse(q1.equals(q4));
    assertFalse(q1.hashCode() == q4.hashCode());

    Query q5 = new ESToParentBlockJoinQuery(
            new TermQuery(new Term("is", "child")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
            ScoreMode.Total, "nested");
    assertFalse(q1.equals(q5));
    assertFalse(q1.hashCode() == q5.hashCode());

    Query q6 = new ESToParentBlockJoinQuery(
            new TermQuery(new Term("is", "child")),
            new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))),
            ScoreMode.Avg, "nested2");
    assertFalse(q1.equals(q6));
    assertFalse(q1.hashCode() == q6.hashCode());
}