Search in sources :

Example 31 with WildcardQuery

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

the class QueryStringQueryBuilderTests method testDisabledFieldNamesField.

public void testDisabledFieldNamesField() throws Exception {
    QueryShardContext context = createShardContext();
    context.getMapperService().merge("new_type", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("new_type", "foo", "type=text", "_field_names", "enabled=false").string()), MapperService.MergeReason.MAPPING_UPDATE, true);
    QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("foo:*");
    Query query = queryBuilder.toQuery(context);
    Query expected = new WildcardQuery(new Term("foo", "*"));
    assertThat(query, equalTo(expected));
    context.getMapperService().merge("new_type", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("new_type", "foo", "type=text", "_field_names", "enabled=true").string()), MapperService.MergeReason.MAPPING_UPDATE, true);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Term(org.apache.lucene.index.Term)

Example 32 with WildcardQuery

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

the class QueryStringQueryBuilderTests method testExpandedTerms.

public void testExpandedTerms() throws Exception {
    // Prefix
    Query query = new QueryStringQueryBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "aBc")), query);
    query = new QueryStringQueryBuilder("aBc*").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new PrefixQuery(new Term(STRING_FIELD_NAME, "abc")), query);
    // Wildcard
    query = new QueryStringQueryBuilder("aBc*D").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new WildcardQuery(new Term(STRING_FIELD_NAME, "aBc*D")), query);
    query = new QueryStringQueryBuilder("aBc*D").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new WildcardQuery(new Term(STRING_FIELD_NAME, "abc*d")), query);
    // Fuzzy
    query = new QueryStringQueryBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    FuzzyQuery fuzzyQuery = (FuzzyQuery) query;
    assertEquals(new Term(STRING_FIELD_NAME, "aBc"), fuzzyQuery.getTerm());
    query = new QueryStringQueryBuilder("aBc~1").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    fuzzyQuery = (FuzzyQuery) query;
    assertEquals(new Term(STRING_FIELD_NAME, "abc"), fuzzyQuery.getTerm());
    // Range
    query = new QueryStringQueryBuilder("[aBc TO BcD]").field(STRING_FIELD_NAME).analyzer("whitespace").toQuery(createShardContext());
    assertEquals(new TermRangeQuery(STRING_FIELD_NAME, new BytesRef("aBc"), new BytesRef("BcD"), true, true), query);
    query = new QueryStringQueryBuilder("[aBc TO BcD]").field(STRING_FIELD_NAME).analyzer("standard").toQuery(createShardContext());
    assertEquals(new TermRangeQuery(STRING_FIELD_NAME, new BytesRef("abc"), new BytesRef("bcd"), true, true), query);
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BytesRef(org.apache.lucene.util.BytesRef)

Example 33 with WildcardQuery

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

the class WildcardQueryBuilderTests method testWithMetaDataField.

public void testWithMetaDataField() throws IOException {
    QueryShardContext context = createShardContext();
    for (String field : new String[] { "_type", "_all" }) {
        WildcardQueryBuilder wildcardQueryBuilder = new WildcardQueryBuilder(field, "toto");
        Query query = wildcardQueryBuilder.toQuery(context);
        Query expected = new WildcardQuery(new Term(field, "toto"));
        assertEquals(expected, query);
    }
}
Also used : WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) WildcardQuery(org.apache.lucene.search.WildcardQuery) Term(org.apache.lucene.index.Term)

Example 34 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project querydsl by querydsl.

the class LuceneSerializer method like.

protected Query like(Operation<?> operation, QueryMetadata metadata) {
    verifyArguments(operation);
    Path<?> path = getPath(operation.getArg(0));
    String field = toField(path);
    String[] terms = convert(path, operation.getArg(1));
    if (terms.length > 1) {
        BooleanQuery bq = new BooleanQuery();
        for (String s : terms) {
            bq.add(new WildcardQuery(new Term(field, "*" + s + "*")), Occur.MUST);
        }
        return bq;
    }
    return new WildcardQuery(new Term(field, terms[0]));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Term(org.apache.lucene.index.Term)

Example 35 with WildcardQuery

use of org.apache.lucene.search.WildcardQuery in project graphdb by neo4j-attic.

the class ImdbExampleTest method doQueriesForNodes.

@Test
public void doQueriesForNodes() {
    IndexManager index = graphDb.index();
    Index<Node> actors = index.forNodes("actors");
    Index<Node> movies = index.forNodes("movies");
    Set<String> found = new HashSet<String>();
    @SuppressWarnings("serial") Set<String> expectedActors = new HashSet<String>() {

        {
            add("Monica Bellucci");
            add("Keanu Reeves");
        }
    };
    @SuppressWarnings("serial") Set<String> expectedMovies = new HashSet<String>() {

        {
            add("The Matrix");
        }
    };
    // START SNIPPET: actorsQuery
    for (Node actor : actors.query("name", "*e*")) {
        // This will return Reeves and Bellucci
        // END SNIPPET: actorsQuery
        found.add((String) actor.getProperty("name"));
    // START SNIPPET: actorsQuery
    }
    // END SNIPPET: actorsQuery
    assertEquals(expectedActors, found);
    found.clear();
    // START SNIPPET: matrixQuery
    for (Node movie : movies.query("title:*Matrix* AND year:1999")) {
        // This will return "The Matrix" from 1999 only.
        // END SNIPPET: matrixQuery
        found.add((String) movie.getProperty("title"));
    // START SNIPPET: matrixQuery
    }
    // END SNIPPET: matrixQuery
    assertEquals(expectedMovies, found);
    // START SNIPPET: matrixSingleQuery
    Node matrix = movies.query("title:*Matrix* AND year:2003").getSingle();
    // END SNIPPET: matrixSingleQuery
    assertEquals("The Matrix Reloaded", matrix.getProperty("title"));
    // START SNIPPET: queryWithScore
    IndexHits<Node> hits = movies.query("title", "The*");
    for (Node movie : hits) {
        System.out.println(movie.getProperty("title") + " " + hits.currentScore());
        // END SNIPPET: queryWithScore
        assertTrue(((String) movie.getProperty("title")).startsWith("The"));
    // START SNIPPET: queryWithScore
    }
    // END SNIPPET: queryWithScore
    assertEquals(2, hits.size());
    // START SNIPPET: queryWithRelevance
    hits = movies.query("title", new QueryContext("The*").sortByScore());
    // END SNIPPET: queryWithRelevance
    float previous = Float.MAX_VALUE;
    // START SNIPPET: queryWithRelevance
    for (Node movie : hits) {
        // hits sorted by relevance (score)
        // END SNIPPET: queryWithRelevance
        assertTrue(hits.currentScore() <= previous);
        previous = hits.currentScore();
    // START SNIPPET: queryWithRelevance
    }
    // END SNIPPET: queryWithRelevance
    assertEquals(2, hits.size());
    // START SNIPPET: termQuery
    // a TermQuery will give exact matches
    Node actor = actors.query(new TermQuery(new Term("name", "Keanu Reeves"))).getSingle();
    // END SNIPPET: termQuery
    assertEquals("Keanu Reeves", actor.getProperty("name"));
    Node theMatrix = movies.get("title", "The Matrix").getSingle();
    Node theMatrixReloaded = movies.get("title", "The Matrix Reloaded").getSingle();
    // START SNIPPET: wildcardTermQuery
    hits = movies.query(new WildcardQuery(new Term("title", "The Matrix*")));
    for (Node movie : hits) {
        System.out.println(movie.getProperty("title"));
        // END SNIPPET: wildcardTermQuery
        assertTrue(((String) movie.getProperty("title")).startsWith("The Matrix"));
    // START SNIPPET: wildcardTermQuery
    }
    // END SNIPPET: wildcardTermQuery
    assertEquals(2, hits.size());
    // START SNIPPET: numericRange
    movies.add(theMatrix, "year-numeric", new ValueContext(1999L).indexNumeric());
    movies.add(theMatrixReloaded, "year-numeric", new ValueContext(2003L).indexNumeric());
    // Query for range
    long startYear = 1997;
    long endYear = 2001;
    hits = movies.query(NumericRangeQuery.newLongRange("year-numeric", startYear, endYear, true, true));
    // END SNIPPET: numericRange
    assertEquals(theMatrix, hits.getSingle());
    // START SNIPPET: compoundQueries
    hits = movies.query("title:*Matrix* AND year:1999");
    // END SNIPPET: compoundQueries
    assertEquals(theMatrix, hits.getSingle());
    // START SNIPPET: defaultOperator
    QueryContext query = new QueryContext("title:*Matrix* year:1999").defaultOperator(Operator.AND);
    hits = movies.query(query);
    // END SNIPPET: defaultOperator
    // with OR the result would be 2 hits
    assertEquals(1, hits.size());
    // START SNIPPET: sortedResult
    hits = movies.query("title", new QueryContext("*").sort("title"));
    for (Node hit : hits) {
    // all movies with a title in the index, ordered by title
    }
    // END SNIPPET: sortedResult
    assertEquals(3, hits.size());
    // START SNIPPET: sortedResult
    // or
    hits = movies.query(new QueryContext("title:*").sort("year", "title"));
    for (Node hit : hits) {
    // all movies with a title in the index, ordered by year, then title
    }
    // END SNIPPET: sortedResult
    assertEquals(3, hits.size());
}
Also used : ValueContext(org.neo4j.index.lucene.ValueContext) TermQuery(org.apache.lucene.search.TermQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Node(org.neo4j.graphdb.Node) QueryContext(org.neo4j.index.lucene.QueryContext) Term(org.apache.lucene.index.Term) IndexManager(org.neo4j.graphdb.index.IndexManager) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

WildcardQuery (org.apache.lucene.search.WildcardQuery)71 Term (org.apache.lucene.index.Term)63 BooleanQuery (org.apache.lucene.search.BooleanQuery)30 PrefixQuery (org.apache.lucene.search.PrefixQuery)24 Query (org.apache.lucene.search.Query)22 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)20 TermQuery (org.apache.lucene.search.TermQuery)20 Document (org.apache.lucene.document.Document)17 BoostQuery (org.apache.lucene.search.BoostQuery)15 IndexSearcher (org.apache.lucene.search.IndexSearcher)15 RegexpQuery (org.apache.lucene.search.RegexpQuery)15 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)14 PhraseQuery (org.apache.lucene.search.PhraseQuery)14 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)14 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)14 Field (org.apache.lucene.document.Field)13 IndexReader (org.apache.lucene.index.IndexReader)13 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)13 TopDocs (org.apache.lucene.search.TopDocs)13 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)13