Search in sources :

Example 41 with TermQuery

use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.

the class NestedHelperTests method testNested.

public void testNested() throws IOException {
    QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
    NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.Avg);
    ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("_type", "__nested1")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested1", new TermQueryBuilder("nested1.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // this time we do not add a filter since the inner query only matches inner docs
    expectedChildQuery = new TermQuery(new Term("nested1.foo", "bar"));
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested2", new TermQueryBuilder("nested2.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // we need to add the filter again because of include_in_parent
    expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested2")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested3", new TermQueryBuilder("nested3.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // we need to add the filter again because of include_in_root
    expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested3")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiReader(org.apache.lucene.index.MultiReader) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Term(org.apache.lucene.index.Term) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 42 with TermQuery

use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.

the class NestedHelperTests method testTermQuery.

public void testTermQuery() {
    Query termQuery = mapperService.fullName("foo").termQuery("bar", null);
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
    termQuery = mapperService.fullName("nested1.foo").termQuery("bar", null);
    assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
    assertFalse(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
    termQuery = mapperService.fullName("nested2.foo").termQuery("bar", null);
    assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
    termQuery = mapperService.fullName("nested3.foo").termQuery("bar", null);
    assertTrue(new NestedHelper(mapperService).mightMatchNestedDocs(termQuery));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(termQuery, "nested_missing"));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 43 with TermQuery

use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.

the class SpanMultiTermQueryBuilderTests method testUnsupportedInnerQueryType.

/**
     * test checks that we throw an {@link UnsupportedOperationException} if the query wrapped
     * by {@link SpanMultiTermQueryBuilder} does not generate a lucene {@link MultiTermQuery}.
     * This is currently the case for {@link RangeQueryBuilder} when the target field is mapped
     * to a date.
     */
public void testUnsupportedInnerQueryType() throws IOException {
    MultiTermQueryBuilder query = new MultiTermQueryBuilder() {

        @Override
        public Query toQuery(QueryShardContext context) throws IOException {
            return new TermQuery(new Term("foo", "bar"));
        }

        @Override
        public Query toFilter(QueryShardContext context) throws IOException {
            return toQuery(context);
        }

        @Override
        public QueryBuilder queryName(String queryName) {
            return this;
        }

        @Override
        public String queryName() {
            return "foo";
        }

        @Override
        public float boost() {
            return 1f;
        }

        @Override
        public QueryBuilder boost(float boost) {
            return this;
        }

        @Override
        public String getName() {
            return "foo";
        }

        @Override
        public String getWriteableName() {
            return "foo";
        }

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            return builder;
        }

        @Override
        public void writeTo(StreamOutput out) throws IOException {
        }
    };
    SpanMultiTermQueryBuilder spamMultiTermQuery = new SpanMultiTermQueryBuilder(query);
    UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> spamMultiTermQuery.toQuery(createShardContext()));
    assertThat(e.getMessage(), containsString("unsupported inner query, should be " + MultiTermQuery.class.getName()));
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) Term(org.apache.lucene.index.Term) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 44 with TermQuery

use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.

the class SimpleQueryParserTests method testAnalyzeWildcard.

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));
    }
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) HashMap(java.util.HashMap) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) PrefixQuery(org.apache.lucene.search.PrefixQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 45 with TermQuery

use of org.apache.lucene.search.TermQuery in project elasticsearch by elastic.

the class SimpleQueryParserTests method testQuoteFieldSuffix.

public void testQuoteFieldSuffix() {
    SimpleQueryParser.Settings sqpSettings = new SimpleQueryParser.Settings();
    sqpSettings.quoteFieldSuffix(".quote");
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_INDEX_UUID, "some_uuid").put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build();
    IndexMetaData indexState = IndexMetaData.builder("index").settings(indexSettings).build();
    IndexSettings settings = new IndexSettings(indexState, Settings.EMPTY);
    QueryShardContext mockShardContext = new QueryShardContext(0, settings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            return new MockFieldMapper.FakeFieldType();
        }
    };
    SimpleQueryParser parser = new SimpleQueryParser(new StandardAnalyzer(), Collections.singletonMap("foo", 1f), -1, sqpSettings, mockShardContext);
    assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("bar"));
    assertEquals(new TermQuery(new Term("foo.quote", "bar")), parser.parse("\"bar\""));
    // Now check what happens if foo.quote does not exist
    mockShardContext = new QueryShardContext(0, settings, null, null, null, null, null, xContentRegistry(), null, null, System::currentTimeMillis) {

        @Override
        public MappedFieldType fieldMapper(String name) {
            if (name.equals("foo.quote")) {
                return null;
            }
            return new MockFieldMapper.FakeFieldType();
        }
    };
    parser = new SimpleQueryParser(new StandardAnalyzer(), Collections.singletonMap("foo", 1f), -1, sqpSettings, mockShardContext);
    assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("bar"));
    assertEquals(new TermQuery(new Term("foo", "bar")), parser.parse("\"bar\""));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexSettings(org.elasticsearch.index.IndexSettings) MockFieldMapper(org.elasticsearch.index.mapper.MockFieldMapper) Term(org.apache.lucene.index.Term) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Aggregations

TermQuery (org.apache.lucene.search.TermQuery)826 Term (org.apache.lucene.index.Term)707 BooleanQuery (org.apache.lucene.search.BooleanQuery)412 Query (org.apache.lucene.search.Query)318 IndexSearcher (org.apache.lucene.search.IndexSearcher)280 Document (org.apache.lucene.document.Document)229 TopDocs (org.apache.lucene.search.TopDocs)195 Directory (org.apache.lucene.store.Directory)171 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)139 PhraseQuery (org.apache.lucene.search.PhraseQuery)135 IndexReader (org.apache.lucene.index.IndexReader)132 Test (org.junit.Test)117 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)116 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)114 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)106 ArrayList (java.util.ArrayList)101 BoostQuery (org.apache.lucene.search.BoostQuery)95 Field (org.apache.lucene.document.Field)84 PrefixQuery (org.apache.lucene.search.PrefixQuery)80 IOException (java.io.IOException)69