org.apache.lucene.queries.TermsQuery Java Examples

The following examples show how to use org.apache.lucene.queries.TermsQuery. 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: TestRedisQParser.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseTermsQuery() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("smembers");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("ignoreScore")).thenReturn("true");
  when(localParamsMock.getBool("useAnalyzer", false)).thenReturn(true);
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new WhitespaceAnalyzer());
  when(jedisMock.smembers(anyString())).thenReturn(new HashSet<>(Arrays.asList("123 124", "321", "322", "323", "324",
          "325", "326", "327", "328", "329", "330", "331", "332", "333", "334", "335", "336", "337", "338")));
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).smembers("simpleKey");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  Query rewrittenQuery = searcher.rewrite(query);
  assertTrue(rewrittenQuery instanceof TermsQuery);
}
 
Example #2
Source File: LuceneSearchQueryGenerator.java    From Stargraph with MIT License 5 votes vote down vote up
@Override
public SearchQueryHolder entitiesWithIds(List<String> idList, ModifiableSearchParams searchParams) {
    List<Term> terms = new ArrayList<>();
    for (String id : idList) {
        terms.add(new Term("id", id));
    }
    Query query = new TermsQuery(terms);

    return new LuceneQueryHolder(query, searchParams);
}
 
Example #3
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static Query termsQuery(String columnName, Literal arrayLiteral) {
    List<Term> terms = getTerms(columnName, arrayLiteral);
    if (terms.isEmpty()) {
        return new MatchNoDocsQuery();
    }
    return new TermsQuery(terms);
}
 
Example #4
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query apply(Function input, Context context) {
    Tuple<Reference, Literal> tuple = super.prepare(input);
    if (tuple == null) {
        return null;
    }
    Reference reference = tuple.v1();
    Literal literal = tuple.v2();
    String columnName = reference.info().ident().columnIdent().fqn();
    if (DataTypes.isCollectionType(reference.valueType()) && DataTypes.isCollectionType(literal.valueType())) {
        List<Term> terms = getTerms(columnName, literal);
        if (terms.isEmpty()) {
            return genericFunctionFilter(input, context);
        }
        Query termsQuery = new TermsQuery(terms);

        // wrap boolTermsFilter and genericFunction filter in an additional BooleanFilter to control the ordering of the filters
        // termsFilter is applied first
        // afterwards the more expensive genericFunctionFilter
        BooleanQuery.Builder filterClauses = new BooleanQuery.Builder();
        filterClauses.add(termsQuery, BooleanClause.Occur.MUST);
        filterClauses.add(genericFunctionFilter(input, context), BooleanClause.Occur.MUST);
        return filterClauses.build();
    }
    QueryBuilderHelper builder = QueryBuilderHelper.forType(tuple.v1().valueType());
    return builder.eq(columnName, tuple.v2().value());
}
 
Example #5
Source File: XUsageTrackingQueryCachingPolicy.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
static boolean isCostly(Query query) {
  // This does not measure the cost of iterating over the filter (for this we
  // already have the DocIdSetIterator#cost API) but the cost to build the
  // DocIdSet in the first place
  // ========== BEGIN EDIT ==========
  /*return query instanceof MultiTermQuery ||
      query instanceof MultiTermQueryConstantScoreWrapper;*/
  return query instanceof MultiTermQuery ||
      query instanceof MultiTermQueryConstantScoreWrapper ||
      query instanceof TermsQuery;
  // ========== END EDIT ==========
}
 
Example #6
Source File: MoreLikeThisQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static void handleExclude(BooleanQuery boolQuery, List<Item> likeItems) {
    // artificial docs get assigned a random id and should be disregarded
    List<BytesRef> uids = new ArrayList<>();
    for (Item item : likeItems) {
        if (item.doc() != null) {
            continue;
        }
        uids.add(createUidAsBytes(item.type(), item.id()));
    }
    if (!uids.isEmpty()) {
        TermsQuery query = new TermsQuery(UidFieldMapper.NAME, uids.toArray(new BytesRef[0]));
        boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
    }
}
 
Example #7
Source File: IdFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (indexOptions() != IndexOptions.NONE || context == null) {
        return super.termQuery(value, context);
    }
    final BytesRef[] uids = Uid.createUidsForTypesAndId(context.queryTypes(), value);
    return new TermsQuery(UidFieldMapper.NAME, uids);
}
 
Example #8
Source File: IdFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termsQuery(List values, @Nullable QueryParseContext context) {
    if (indexOptions() != IndexOptions.NONE || context == null) {
        return super.termsQuery(values, context);
    }
    return new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(context.queryTypes(), values));
}
 
Example #9
Source File: MappedFieldType.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Query termsQuery(List values, @Nullable QueryParseContext context) {
    BytesRef[] bytesRefs = new BytesRef[values.size()];
    for (int i = 0; i < bytesRefs.length; i++) {
        bytesRefs[i] = indexedValueForSearch(values.get(i));
    }
    return new TermsQuery(names.indexName(), bytesRefs);
}
 
Example #10
Source File: TestLuceneIndexer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearchIndex() throws Exception {
  try (LuceneSearchIndex index = new LuceneSearchIndex(null, "test", true, CommitWrapper.NO_OP)) {

    final Document doc1 = new Document();
    doc1.add(new StringField(CoreIndexedStore.ID_FIELD_NAME, new BytesRef("1".getBytes()), Store.YES));
    doc1.add(new StringField("ds", "space1.ds1", Field.Store.NO));
    doc1.add(new StringField("job", "job1", Field.Store.YES));
    // since we want to sort on version add docvalues
    doc1.add(new StringField("version", "v1", Field.Store.NO));
    doc1.add(new SortedDocValuesField("version", new BytesRef("v1")));
    doc1.add(new StringField("foo", "bar1", Store.NO));
    doc1.add(new SortedDocValuesField("foo", new BytesRef("bar1")));

    final Document doc2 = new Document();
    doc2.add(new StringField(CoreIndexedStore.ID_FIELD_NAME, new BytesRef("2".getBytes()), Store.YES));
    doc2.add(new StringField("ds", "space1.ds1", Field.Store.NO));
    doc2.add(new StringField("job", "job3", Field.Store.YES));
    doc2.add(new StringField("version", "v2", Field.Store.NO));
    doc2.add(new SortedDocValuesField("version", new BytesRef("v2")));
    doc2.add(new StringField("foo", "bar2", Store.NO));
    doc2.add(new SortedDocValuesField("foo", new BytesRef("bar2")));

    final Document doc3 = new Document();
    doc3.add(new StringField(CoreIndexedStore.ID_FIELD_NAME, new BytesRef("3".getBytes()), Store.YES));
    doc3.add(new StringField("ds", "space2.ds2", Field.Store.NO));
    doc3.add(new StringField("job", "job2", Field.Store.YES));
    doc3.add(new StringField("version", "v1", Field.Store.NO));
    doc3.add(new SortedDocValuesField("version", new BytesRef("v1")));

    index.add(doc1);

    assertEquals(1, index.count(new TermQuery(new Term("ds", "space1.ds1"))));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term("ds", "space1.ds1")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term("version", "v1")), BooleanClause.Occur.MUST));
    assertEquals(1, index.count(builder.build()));

    assertEquals(0, index.count(new TermQuery(new Term("ds", "space2.ds2"))));
    assertEquals(0, index.count(new TermQuery(new Term("version", "v2"))));

    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term("ds", "space1.ds1")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term("version", "v2")), BooleanClause.Occur.MUST));
    assertEquals(0, index.count(builder.build()));
    assertEquals(1, index.count(new TermsQuery(new Term("ds", "space1.ds1"), new Term("version1", "v2"))));

    index.add(doc2);
    index.add(doc3);

    assertEquals(2, index.count(new TermQuery(new Term("ds", "space1.ds1"))));
    assertEquals(1, index.count(new TermQuery(new Term("ds", "space2.ds2"))));

    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term("ds", "space2.ds2")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term("version", "v1")), BooleanClause.Occur.MUST));
    assertEquals(1, index.count(builder.build()));

    builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(new Term("ds", "space1.ds1")), BooleanClause.Occur.MUST));
    builder.add(new BooleanClause(new TermQuery(new Term("version", "v1")), BooleanClause.Occur.MUST));
    assertEquals(1, index.count(builder.build()));

    Sort sorter = new Sort();
    sorter.setSort(new SortField("version", SortField.Type.STRING));
    Collection<Document> documents = index.searchForDocuments(new TermQuery(new Term("ds", "space1.ds1")), 1000, sorter);
    assertEquals(2, documents.size());

    // exists queries
    assertEquals(2, index.count(LuceneQueryConverter.INSTANCE.toLuceneQuery(SearchQueryUtils.newExistsQuery("foo"))));
    assertEquals(1, index.count(LuceneQueryConverter.INSTANCE.toLuceneQuery(SearchQueryUtils.newDoesNotExistQuery("foo"))));
  }
}
 
Example #11
Source File: XJoinQParserPlugin.java    From BioSolr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
Query makeQuery(String fname, Iterator<BytesRef> it) {
  return new TermsQuery(fname, IteratorUtils.toList(it));
}