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 searchcode-server by boyter.
the class IndexService method deleteByCodeId.
/**
* Deletes a file from the index using the code id which seems to be
* the most reliable way of doing it. Code id being a hash of the file
* name and location.
* TODO Update the record and set the facets to a value we can ignore
*/
@Override
public synchronized void deleteByCodeId(String codeId) throws IOException {
Directory dir = FSDirectory.open(this.INDEX_WRITE_LOCATION);
Analyzer analyzer = new CodeAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter writer = null;
Query query;
try {
writer = new IndexWriter(dir, iwc);
QueryParser parser = new QueryParser(Values.CONTENTS, analyzer);
query = parser.parse(Values.CODEID + ":" + QueryParser.escape(codeId));
writer.deleteDocuments(query);
} catch (ParseException | NoSuchFileException ex) {
this.logger.severe(String.format("e9a71f33::error in class %s exception %s", ex.getClass(), ex.getMessage()));
}
this.helpers.closeQuietly(writer);
}
use of org.apache.lucene.queryparser.classic.QueryParser in project searchcode-server by boyter.
the class IndexService method getProjectFileTree.
/**
* Collects project stats for a repo given its name
*/
@Override
public SearchResult getProjectFileTree(String repoName) {
List<CodeResult> codeResults = new ArrayList<>();
if (this.helpers.isNullEmptyOrWhitespace(repoName)) {
return new SearchResult(0, 0, Values.EMPTYSTRING, codeResults, null, null, null, null, null);
}
IndexReader reader = null;
try {
reader = DirectoryReader.open(FSDirectory.open(this.INDEX_READ_LOCATION));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new CodeAnalyzer();
QueryParser parser = new QueryParser(Values.CONTENTS, analyzer);
Query query = parser.parse(Values.REPO_NAME_LITERAL + ":" + this.helpers.replaceForIndex(repoName));
TopDocs results = searcher.search(query, Integer.MAX_VALUE);
ScoreDoc[] hits = results.scoreDocs;
for (int i = 0; i < results.totalHits; i++) {
Document doc = searcher.doc(hits[i].doc);
CodeResult codeResult = this.createCodeResult(null, Values.EMPTYSTRING, doc, hits[i].doc, hits[i].score);
codeResults.add(codeResult);
if (i == 1_000) {
break;
}
}
} catch (Exception ex) {
this.logger.severe(String.format("1c90b7f9::error in class %s exception %s", ex.getClass(), ex.getMessage()));
} finally {
this.helpers.closeQuietly(reader);
}
codeResults.sort(Comparator.comparing(x -> x.displayLocation));
return new SearchResult(0, 0, Values.EMPTYSTRING, codeResults, null, null, null, null, null);
}
use of org.apache.lucene.queryparser.classic.QueryParser in project HongsCORE by ihongs.
the class LuceneRecord method padQry.
/**
* 组织查询条件
* 可覆盖此方法扩展查询条件
*
* @param qr
* @param rd
* @param r 递归层级
* @throws HongsException
*/
protected void padQry(BooleanQuery.Builder qr, Map rd, int r) throws HongsException {
// 条件数量, 否定数量, 计数规避全否定时查不到数据
int i = 0, j = 0;
Map<String, Map> fields = getFields();
Set<String> ks = new LinkedHashSet(fields.keySet());
ks.retainAll(rd.keySet());
for (String k : ks) {
Object v = rd.get(k);
Map m = fields.get(k);
if (m == null || v == null) {
continue;
}
// 自定义条件
if (!padQry(qr, rd, k, v)) {
continue;
}
IQuery qa;
String t = datatype(m);
if (t != null)
switch(t) {
case "int":
qa = new IntQuery();
break;
case "long":
case "date":
qa = new LongQuery();
break;
case "float":
qa = new FloatQuery();
break;
case "double":
case "number":
qa = new DoubleQuery();
break;
case "string":
case "search":
if (!srchable(m)) {
qa = new StringQuery();
} else {
SearchQuery qs;
qa = qs = new SearchQuery();
qs.analyser(getAnalyser(m));
qs.settings(m);
}
break;
default:
continue;
}
else {
continue;
}
// ** 常规查询 **/
Map vd;
if (v instanceof Map) {
vd = (Map) v;
} else if (v instanceof Collection || v instanceof Object[]) {
Set a = Synt.asSet(v);
a.remove("");
if (!a.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object b : a) {
qx.add(qa.whr(k, b), BooleanClause.Occur.SHOULD);
}
qr.add(qx.build(), BooleanClause.Occur.MUST);
i++;
}
continue;
} else {
if (!v.equals("")) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
i++;
}
continue;
}
// ** 条件关系 **/
v = vd.get(Cnst.OR_REL);
if (Cnst.OR_KEY.equals(v) || Cnst.NR_KEY.equals(v)) {
List r2 = new ArrayList(vd.size() - 1);
for (Object ot : vd.entrySet()) {
Map.Entry et = (Map.Entry) ot;
Object k2 = et.getKey();
if (!Cnst.OR_KEY.equals(k2)) {
Object v2 = et.getValue();
r2.add(Synt.mapOf(k, Synt.mapOf(k2, v2)));
}
}
if (!r2.isEmpty()) {
padQry(qr, Synt.mapOf(v, r2), r - 1);
}
continue;
}
// ** 空值查询 **/
v = vd.get(Cnst.IS_REL);
if (v != null) {
String b = qa instanceof SearchQuery ? "$" : "@";
String a = Synt.asString(v).toUpperCase();
Query p;
try {
p = new QueryParser(b + k, new StandardAnalyzer()).parse("[* TO *]");
} catch (ParseException e) {
throw new HongsExemption(e);
}
switch(a) {
case "WELL":
case "NOT-NULL":
qr.add(p, BooleanClause.Occur.MUST);
i++;
break;
case "NULL":
case "NOT-WELL":
qr.add(p, BooleanClause.Occur.MUST_NOT);
i++;
j++;
break;
default:
throw new UnsupportedOperationException("Unsupported `is`: " + v);
}
}
// ** 精确匹配 **/
v = vd.get(Cnst.EQ_REL);
if (v != null) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
i++;
}
v = vd.get(Cnst.NE_REL);
if (v != null) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
// ** 模糊匹配 **/
v = vd.get(Cnst.CQ_REL);
if (v != null && !"".equals(v)) {
qr.add(qa.wdr(k, v), BooleanClause.Occur.MUST);
i++;
}
v = vd.get(Cnst.NC_REL);
if (v != null && !"".equals(v)) {
qr.add(qa.wdr(k, v), BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
// ** 集合查询 **/
v = vd.get(Cnst.AI_REL);
if (v != null) {
Set vs = Synt.asSet(v);
if (!vs.isEmpty()) {
for (Object vv : vs) {
qr.add(qa.whr(k, vv), BooleanClause.Occur.MUST);
i++;
}
}
}
v = vd.get(Cnst.NI_REL);
if (v != null) {
Set vs = Synt.asSet(v);
if (!vs.isEmpty()) {
for (Object vv : vs) {
qr.add(qa.whr(k, vv), BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
}
}
v = vd.get(Cnst.IN_REL);
if (v != null) {
Set vs = Synt.asSet(v);
if (!vs.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object vv : vs) {
qx.add(qa.whr(k, vv), BooleanClause.Occur.SHOULD);
}
qr.add(qx.build(), BooleanClause.Occur.MUST);
i++;
}
}
v = vd.get(Cnst.ON_REL);
if (v != null) {
if (v instanceof Collection || v instanceof Object[]) {
Set a = Synt.asSet(v);
// 与 in 不同
a.remove("");
if (!a.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object b : a) {
qx.add(qa.whr(k, b), BooleanClause.Occur.SHOULD);
}
qr.add(qx.build(), BooleanClause.Occur.MUST);
i++;
}
} else {
if (!v.equals("")) {
qr.add(qa.whr(k, v), BooleanClause.Occur.MUST);
i++;
}
}
}
// ** 区间查询 **/
Object n, x;
boolean l, g;
n = vd.get(Cnst.GT_REL);
if (n != null) {
l = false;
} else {
n = vd.get(Cnst.GE_REL);
if (n != null) {
l = true;
} else {
n = null;
l = true;
}
}
x = vd.get(Cnst.LT_REL);
if (x != null) {
g = false;
} else {
x = vd.get(Cnst.LE_REL);
if (x != null) {
g = true;
} else {
x = null;
g = true;
}
}
if ((n != null && !"".equals(n)) || (x != null && !"".equals(x))) {
Query qu = qa.whr(k, n, x, l, g);
qr.add(qu, BooleanClause.Occur.MUST);
i++;
}
Set s = null;
v = vd.get(Cnst.RG_REL);
if (v != null) {
s = Synt.asSet(v);
}
if (s != null && !s.isEmpty()) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
for (Object o : s) {
Object[] a = Synt.toRange(o);
if (a == null) {
continue;
}
n = a[0];
l = (boolean) a[2];
x = a[1];
g = (boolean) a[3];
if (n == null && x == null) {
continue;
}
Query qu = qa.whr(k, n, x, l, g);
qx.add(qu, BooleanClause.Occur.SHOULD);
}
BooleanQuery qz = qx.build();
if (qz.clauses().size() > 0) {
qr.add(qz, BooleanClause.Occur.MUST);
i++;
}
}
}
Object v;
// ** 全局搜索 **/
v = rd.get(Cnst.WD_KEY);
if (v != null && !"".equals(v)) {
Set<String> fs = getRschable();
if (fs.size() > 0) {
BooleanQuery.Builder qx = new BooleanQuery.Builder();
SearchQuery qs = new SearchQuery();
for (String fn : fs) {
Map fc = fields.get(fn);
if (fc == null)
continue;
qs.settings(/*parser*/
fc);
qs.analyser(getAnalyser(fc));
qx.add(qs.wdr(fn, v), BooleanClause.Occur.SHOULD);
}
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
qr.add(qa, BooleanClause.Occur.MUST);
i++;
}
}
}
// ** 递归条件 **/
v = rd.get(Cnst.AR_KEY);
if (v != null) {
if (r > 2) {
throw new HongsException(400, "Key '" + Cnst.AR_KEY + "' can not exceed 2 layers");
}
Set<Map> set = Synt.asSet(v);
if (set != null && !set.isEmpty()) {
for (Map map : set) {
// 规避 NullPointerException
if (map == null)
continue;
BooleanQuery.Builder qx = new BooleanQuery.Builder();
padQry(qx, map, r + 1);
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
// 权重
Query qb = qa;
v = map.get(Cnst.WT_REL);
if (v != null && !"".equals(v)) {
qb = new BoostQuery(qa, Synt.declare(v, 1f));
}
qr.add(qb, BooleanClause.Occur.MUST);
i++;
}
}
}
}
v = rd.get(Cnst.NR_KEY);
if (v != null) {
if (r > 2) {
throw new HongsException(400, "Key '" + Cnst.NR_KEY + "' can not exceed 2 layers");
}
Set<Map> set = Synt.asSet(v);
if (set != null && !set.isEmpty()) {
for (Map map : set) {
// 规避 NullPointerException
if (map == null)
continue;
BooleanQuery.Builder qx = new BooleanQuery.Builder();
padQry(qx, map, r + 1);
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
// 权重
Query qb = qa;
v = map.get(Cnst.WT_REL);
if (v != null && !"".equals(v)) {
qb = new BoostQuery(qa, Synt.declare(v, 1f));
}
qr.add(qb, BooleanClause.Occur.MUST_NOT);
i++;
j++;
}
}
}
}
v = rd.get(Cnst.OR_KEY);
if (v != null) {
if (r > 2) {
throw new HongsException(400, "Key '" + Cnst.OR_KEY + "' can not exceed 2 layers");
}
Set<Map> set = Synt.asSet(v);
if (set != null && !set.isEmpty()) {
BooleanQuery.Builder qz = new BooleanQuery.Builder();
for (Map map : set) {
// 规避 NullPointerException
if (map == null)
continue;
BooleanQuery.Builder qx = new BooleanQuery.Builder();
padQry(qx, map, r + 1);
BooleanQuery qa = qx.build();
if (!qa.clauses().isEmpty()) {
// 权重
Query qb = qa;
v = map.get(Cnst.WT_REL);
if (v != null && !"".equals(v)) {
qb = new BoostQuery(qa, Synt.declare(v, 1f));
}
qz.add(qb, BooleanClause.Occur.SHOULD);
}
}
BooleanQuery qa = qz.build();
if (!qa.clauses().isEmpty()) {
qr.add(qa, BooleanClause.Occur.MUST);
i++;
}
}
}
/**
* 如果全部条件都是 MUST_NOT 会导致取不到数据
* 故有必要增加一个 MUST all 从而规避这个问题
*/
if (i > 0 & i == j) {
qr.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
}
}
use of org.apache.lucene.queryparser.classic.QueryParser in project tutorials by eugenp.
the class InMemoryLuceneIndex method searchIndex.
public List<Document> searchIndex(String inField, String queryString) {
try {
Query query = new QueryParser(inField, analyzer).parse(queryString);
IndexReader indexReader = DirectoryReader.open(memoryIndex);
IndexSearcher searcher = new IndexSearcher(indexReader);
TopDocs topDocs = searcher.search(query, 10);
List<Document> documents = new ArrayList<>();
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
documents.add(searcher.doc(scoreDoc.doc));
}
return documents;
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return null;
}
Aggregations