use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.
the class OLuceneIndexType method getQueryParser.
protected static Query getQueryParser(OIndexDefinition index, String key, Analyzer analyzer) throws ParseException {
QueryParser queryParser;
if ((key).startsWith("(")) {
queryParser = new QueryParser("", analyzer);
} else {
String[] fields = null;
if (index.isAutomatic()) {
fields = index.getFields().toArray(new String[index.getFields().size()]);
} else {
int length = index.getTypes().length;
fields = new String[length];
for (int i = 0; i < length; i++) {
fields[i] = "k" + i;
}
}
queryParser = new MultiFieldQueryParser(fields, analyzer);
}
return queryParser.parse(key);
}
use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.
the class VertexIndexTest method testSpacesInQuery.
@Test
public void testSpacesInQuery() throws IOException, ParseException {
IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
final RAMDirectory directory = new RAMDirectory();
final IndexWriter writer = new IndexWriter(directory, conf);
Document doc = new Document();
doc.add(new TextField("name", "Max Water", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new TextField("name", "Max Waterson", Field.Store.YES));
writer.addDocument(doc);
doc = new Document();
doc.add(new TextField("name", "Cory Watney", Field.Store.YES));
writer.addDocument(doc);
writer.commit();
IndexReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser queryParser = new QueryParser("name", analyzer);
final Query query = queryParser.parse("name:Max AND name:Wat*");
final TopDocs topDocs = searcher.search(query, 10);
assertThat(topDocs.totalHits).isEqualTo(2);
for (int i = 0; i < topDocs.totalHits; i++) {
final Document found = searcher.doc(topDocs.scoreDocs[i].doc);
assertThat(found.get("name")).startsWith("Max");
}
reader.close();
writer.close();
}
use of org.apache.lucene.queryparser.classic.QueryParser in project orientdb by orientechnologies.
the class LuceneBooleanIndexTest method testMemoryIndex.
@Test
public void testMemoryIndex() throws ParseException {
// TODO To be used in evaluate Record
MemoryIndex index = new MemoryIndex();
Document doc = new Document();
doc.add(new StringField("text", "my text", Field.Store.YES));
StandardAnalyzer analyzer = new StandardAnalyzer();
for (IndexableField field : doc.getFields()) {
index.addField(field.name(), field.stringValue(), analyzer);
}
QueryParser parser = new QueryParser("text", analyzer);
float score = index.search(parser.parse("+text:my"));
}
use of org.apache.lucene.queryparser.classic.QueryParser in project neo4j by neo4j.
the class IndexType method query.
Query query(String keyOrNull, Object value, QueryContext contextOrNull) {
if (value instanceof Query) {
return (Query) value;
}
QueryParser parser = new QueryParser(keyOrNull, analyzer);
parser.setAllowLeadingWildcard(true);
parser.setLowercaseExpandedTerms(toLowerCase);
if (contextOrNull != null && contextOrNull.getDefaultOperator() != null) {
parser.setDefaultOperator(contextOrNull.getDefaultOperator());
}
try {
return parser.parse(value.toString());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
use of org.apache.lucene.queryparser.classic.QueryParser in project incubator-atlas by apache.
the class QueryFactory method create.
private QueryExpression create(Request request, ResourceDefinition resourceDefinition) throws InvalidQueryException {
String queryString;
if (request.getCardinality() == Request.Cardinality.INSTANCE) {
String idPropertyName = resourceDefinition.getIdPropertyName();
queryString = String.format("%s:%s", idPropertyName, request.<String>getProperty(idPropertyName));
} else {
queryString = request.getQueryString();
}
QueryExpression queryExpression;
if (queryString != null && !queryString.isEmpty()) {
QueryParser queryParser = new QueryParser(Version.LUCENE_48, "name", new KeywordAnalyzer());
queryParser.setLowercaseExpandedTerms(false);
queryParser.setAllowLeadingWildcard(true);
Query query;
try {
query = queryParser.parse((String) escape(queryString));
} catch (ParseException e) {
throw new InvalidQueryException(e.getMessage());
}
LOG.info("LuceneQuery: {}", query);
queryExpression = create(query, resourceDefinition);
} else {
queryExpression = new AlwaysQueryExpression();
}
// add query properties to request so that they are returned
request.addAdditionalSelectProperties(queryExpression.getProperties());
return queryExpression;
}
Aggregations