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);
}
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);
}
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);
}
}
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]));
}
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());
}
Aggregations