List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery
BooleanQuery
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
private void getTheChildren(Node parent, String relChildren, NodeManager nmgr, int mode, int layerInStorage) throws Exception { IndexSearcher searcher = null;//from w ww.jav a2 s . c o m BooleanQuery bq = null; try { searcher = this.getIndexSearcher(); String pid = parent.getID(); HashMap ids = new HashMap(); int length; Query query1 = new TermQuery(new Term(ISCHILD, "true")); Query query2 = new TermQuery(new Term(PARENTID, pid)); for (int i = LIVE_MODE; i <= mode; i++) { bq = new BooleanQuery(); bq.add(query1, BooleanClause.Occur.MUST); bq.add(query2, BooleanClause.Occur.MUST); bq.add(new TermQuery(new Term(LAYER_OF_SAVE, i + "")), BooleanClause.Occur.MUST); Hits hits = searcher.search(bq); length = hits.length(); /*if (app.debug()) app.logEvent("LuceneManager.getTheChildren(), parent = " + parent.getKey() + ", layer = " + mode + ", layerInStorage = " + layerInStorage + ", executed query " + bq + " which produced " + length + " results");*/ for (int j = 0; j < length; j++) { Document doc = hits.doc(j); ids.put(doc.getField(ID).stringValue(), doc.getField(PROTOTYPE).stringValue()); } } Collection<NodeHandle> subnodes = null; length = ids.size(); if (length > 0) { subnodes = parent.createSubnodeList(); Iterator iter = ids.keySet().iterator(); while (iter.hasNext()) { String id = (String) iter.next(); NodeHandle handle = makeNodeHandle(nmgr, id, (String) ids.get(id), mode); if (subnodes instanceof SubnodeList) { ((SubnodeList) subnodes).addSorted(handle); } else { subnodes.add(handle); } } } ids.clear(); ids = null; if (relChildren != null) { String[] charr = relChildren.split(NULL_DELIM); if (charr.length > 0 && charr.length % 2 == 0) { if (subnodes == null) { subnodes = parent.createSubnodeList(); } for (int i = 0; i < charr.length; i += 2) { if (subnodes instanceof SubnodeList) { ((SubnodeList) subnodes).addSorted(makeNodeHandle(nmgr, charr[i], charr[i + 1], mode)); } else { subnodes.add(makeNodeHandle(nmgr, charr[i], charr[i + 1], mode)); } } } } } catch (IOException ioe) { throw new Exception("Searcher failed when attempting to retrieve children of " + "id '" + parent.getID() + "', query = " + bq); } finally { this.releaseIndexSearcher(searcher); } }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public Key[] getTargetNodeIds(final String id, final int mode, ArrayList protos, BooleanQuery append, Sort sort) throws Exception { IndexSearcher searcher = null;//from ww w .ja v a 2 s.c o m Document doc = null; BooleanQuery query = null; try { searcher = this.getIndexSearcher(); String idvalue = id; Query id_query = new TermQuery(new Term(ID, idvalue)); for (int i = mode; i >= LIVE_MODE; i--) { if (i != LIVE_MODE && !isDraftNode(id, mode)) { continue; } query = new BooleanQuery(); query.add(id_query, BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(LAYER_OF_SAVE, i + "")), BooleanClause.Occur.MUST); Hits hits = searcher.search(query); /*if (app.debug()) app.logEvent("LuceneManager.getTargetNodeIds(): id=" + id + ",layer=" + mode + " executed query [" + query + "] which resulted in " + hits.length() + " hits");*/ if (hits.length() > 0) { doc = hits.doc(0); break; } } } catch (Exception ex) { app.logError( ErrorReporter.errorMsg(this.getClass(), "getSourceReferences") + "Could not retrieve document " + id + " from Lucene index with query = " + (query != null ? query : "null"), ex); throw ex; } finally { this.releaseIndexSearcher(searcher); } if (doc == null) { return new Key[0]; } Field[] fields = doc.getFields(REF_LIST_FIELD); int len; if ((fields == null) || ((len = fields.length) == 0)) { return new Key[0]; } ArrayList<Key> keys = new ArrayList<Key>(); doc = null; for (int i = 0; i < len; i++) { doc = null; query = new BooleanQuery(); String refid = getIdFromRefListField(fields[i]); query.add(new TermQuery(new Term(ID, refid)), BooleanClause.Occur.MUST); BooleanQuery proto_query = null; final int sizeOfProtos; if ((sizeOfProtos = protos.size()) > 0) { proto_query = new BooleanQuery(); for (int j = 0; j < sizeOfProtos; j++) { proto_query.add(new TermQuery(new Term(PROTOTYPE, (String) protos.get(j))), BooleanClause.Occur.SHOULD); } query.add(proto_query, BooleanClause.Occur.MUST); } if (append != null && append.getClauses().length > 0) { query.add(append, BooleanClause.Occur.MUST); } for (int j = mode; j >= LIVE_MODE; j--) { if (j != LIVE_MODE && !isDraftNode(refid, mode)) { continue; } query.add(new TermQuery(new Term(LAYER_OF_SAVE, j + "")), BooleanClause.Occur.MUST); Hits hits = searcher.search(query); /*if (app.debug()) app.logEvent("LuceneManager.getTargetNodeIds() [for retrieving target " + "keys]: id=" + id + ",layer=" + mode + " executed query [" + query + "] which resulted in " + hits.length() + " hits");*/ if (hits.length() > 0) { doc = hits.doc(0); break; } } if (doc != null) { keys.add(new DbKey(this.app.getDbMapping(doc.get(PROTOTYPE)), doc.get(ID), mode)); } } Key[] key_arr = new Key[keys.size()]; return keys.toArray(key_arr); }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public Key[] getSourceNodeIds(final String id, final int mode, ArrayList protos, BooleanQuery append, Sort sort) throws Exception { IndexSearcher searcher = null;// w ww .j av a 2 s . co m Hits hits = null; Key[] keys = null; BooleanQuery query = null; try { searcher = this.getIndexSearcher(); query = new BooleanQuery(); final int sizeOfProtos; if ((sizeOfProtos = protos.size()) > 0) { BooleanQuery proto_query = new BooleanQuery(); for (int i = 0; i < sizeOfProtos; i++) { proto_query.add(new TermQuery(new Term(PROTOTYPE, (String) protos.get(i))), BooleanClause.Occur.SHOULD); } query.add(proto_query, BooleanClause.Occur.MUST); } query.add(new TermQuery(new Term(REF_LIST_FIELD, id)), BooleanClause.Occur.MUST); if (append != null && append.getClauses().length > 0) { query.add(append, BooleanClause.Occur.MUST); } hits = searcher.search(query, sort); /*if (app.debug()) app.logEvent("LuceneManager.getSourceNodeIds(): id=" + id + ",layer=" + mode + " executed query [" + query + " which resulted in " + hits.length() + " hits");*/ int size = hits.length(); ArrayList<Key> list = new ArrayList<Key>(); for (int i = 0; i < size; i++) { Document doc = hits.doc(i); if (!isIdInDocumentRefs(doc, id)) { continue; } Field id_field = doc.getField(ID); Field proto_field = doc.getField(PROTOTYPE); Field layer_field = doc.getField(LAYER_OF_SAVE); if (layer_field != null) { try { if (mode < Integer.parseInt(layer_field.stringValue())) { continue; } } catch (Exception nfe) { } } if (id_field != null && proto_field != null) { list.add(new DbKey(this.app.getDbMapping(proto_field.stringValue()), id_field.stringValue(), mode)); } } keys = new Key[list.size()]; list.toArray(keys); } catch (Exception ex) { app.logError( ErrorReporter.errorMsg(this.getClass(), "getSourceNodeIds") + "Could not retrieve document " + id + " from Lucene index with query = " + (query != null ? query : "null"), ex); throw ex; } finally { this.releaseIndexSearcher(searcher); } return keys; }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public ArrayList getChildrenIds(INode node) throws Exception { ArrayList childrenIds = new ArrayList(); IndexSearcher searcher = null;/* w w w. ja v a 2s. c o m*/ BooleanQuery bq = new BooleanQuery(); try { searcher = this.getIndexSearcher(); String id = node.getID(); final Query query1 = new TermQuery(new Term(PARENTID, isSpecialNode(id) ? id : node.getID())); final Query query2 = new TermQuery(new Term(ISCHILD, "true")); bq.add(query1, BooleanClause.Occur.MUST); bq.add(query2, BooleanClause.Occur.MUST); final Hits hits = searcher.search(bq); /*if (app.debug()) app.logEvent("LuceneManager.getChildrenIds() executed query [" + bq + " which resulted in " + hits.length() + " hits");*/ final int length = hits.length(); for (int i = 0; i < length; i++) { Document doc = hits.doc(i); childrenIds.add(doc.getField(ID).stringValue()); } } catch (IOException ioe) { throw new Exception("Searcher failed when attempting to retrieve children of " + "id '" + node.getID() + "', query = " + bq); } finally { this.releaseIndexSearcher(searcher); } return childrenIds; }
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Object luceneHits(ArrayList prototypes, IFilter filter, SortObject sort, int maxResults, ArrayList opaths, IndexSearcher searcher, LuceneQueryParams params, int _layer) throws Exception { long start = System.currentTimeMillis(); BooleanQuery primary = new BooleanQuery(); final String PROTO = LuceneManager.PROTOTYPE; final BooleanClause.Occur SHOULD = BooleanClause.Occur.SHOULD; final BooleanClause.Occur MUST = BooleanClause.Occur.MUST; final TypeManager tmgr = this.app.typemgr; final ResourceProperties combined_props = new ResourceProperties(); Sort lsort = null;/* w w w .jav a 2 s . c om*/ final int length; if (prototypes != null && (length = prototypes.size()) > 0) { BooleanQuery proto_query = new BooleanQuery(); for (int i = 0; i < length; i++) { String prototype = (String) prototypes.get(i); proto_query.add(new TermQuery(new Term(PROTO, prototype)), SHOULD); Prototype proto = tmgr.getPrototype(prototype); Stack protos = new Stack(); while (proto != null) { protos.push(proto); proto = proto.getParentPrototype(); } final int protoChainSize = protos.size(); for (int j = 0; j < protoChainSize; j++) { proto = (Prototype) protos.pop(); combined_props.putAll(proto.getTypeProperties()); } } primary.add(proto_query, MUST); } else { ArrayList protoarr = app.getSearchablePrototypes(); BooleanQuery proto_query = new BooleanQuery(); for (int i = protoarr.size() - 1; i > -1; i--) { String protoName = (String) protoarr.get(i); proto_query.add(new TermQuery(new Term(PROTO, protoName)), SHOULD); Prototype proto = tmgr.getPrototype(protoName); Stack protos = new Stack(); while (proto != null) { protos.push(proto); proto = proto.getParentPrototype(); } final int protoChainSize = protos.size(); for (int j = 0; j < protoChainSize; j++) { proto = (Prototype) protos.pop(); combined_props.putAll(proto.getTypeProperties()); } } primary.add(proto_query, MUST); } parseFilterIntoQuery(filter, primary, combined_props); RequestEvaluator reqeval = this.app.getCurrentRequestEvaluator(); int layer = _layer; if (layer == -1) { layer = DbKey.LIVE_LAYER; if (reqeval != null) { layer = reqeval.getLayer(); } } BooleanQuery layerQuery = new BooleanQuery(); for (int i = 0; i <= layer; i++) { layerQuery.add(new TermQuery(new Term(LuceneManager.LAYER_OF_SAVE, i + "")), BooleanClause.Occur.SHOULD); } primary.add(layerQuery, BooleanClause.Occur.MUST); BooleanClause[] clauses = primary.getClauses(); if (clauses == null || clauses.length == 0) { throw new Exception("QueryBean.executeQuery(): The lucene query doesn't have any clauses!"); } if (filter.isCached()) { SimpleQueryFilter sqf = (SimpleQueryFilter) this.cache.get(primary); if (sqf == null) { sqf = new SimpleQueryFilter(primary); this.cache.put(primary, sqf); } } Object ret = null; int sizeOfResults = 0; try { if (app.debug()) { app.logEvent("running query " + primary + " with maxResults " + maxResults + " and sort " + (sort == null ? "null" : getLuceneSort(sort))); } if (sort != null && (lsort = getLuceneSort(sort)) != null) { if (maxResults == -1 || opaths.size() > 0) { Hits h = searcher.search(primary, lsort); sizeOfResults = h.length(); ret = h; } else { TopFieldDocs tfd = searcher.search(primary, null, maxResults, lsort); sizeOfResults = tfd.totalHits; ret = tfd; } } else { if (maxResults == -1 || opaths.size() > 0) { Hits h = searcher.search(primary); sizeOfResults = h.length(); ret = h; } else { TopDocs td = searcher.search(primary, null, maxResults); sizeOfResults = td.totalHits; ret = td; } } } catch (Exception ex) { app.logError(ErrorReporter.errorMsg(this.getClass(), "luceneHits") + "Occured on query = " + primary, ex); } if (ret == null) { ret = (maxResults == -1 || opaths.size() > 0) ? new Boolean(true) : new Boolean(false); } if (params != null) { params.query = primary; params.max_results = maxResults; params.sort = lsort; params.rprops = combined_props; } if (app.debug()) { long time = System.currentTimeMillis() - start; app.logEvent("... took " + (time / 1000.0) + " seconds\n ------"); } return ret; }
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Object luceneHits(ArrayList prototypes, IFilter ifilter, LuceneQueryParams params) throws Exception { long start = System.currentTimeMillis(); BooleanQuery primary = new BooleanQuery(); final String PROTO = LuceneManager.PROTOTYPE; final BooleanClause.Occur SHOULD = BooleanClause.Occur.SHOULD; final BooleanClause.Occur MUST = BooleanClause.Occur.MUST; final TypeManager tmgr = this.app.typemgr; final ResourceProperties combined_props = new ResourceProperties(); final int length; if (prototypes != null && (length = prototypes.size()) > 0) { BooleanQuery proto_query = new BooleanQuery(); for (int i = 0; i < length; i++) { String prototype = (String) prototypes.get(i); proto_query.add(new TermQuery(new Term(PROTO, prototype)), SHOULD); Prototype p = tmgr.getPrototype(prototype); if (p != null) { combined_props.putAll(p.getTypeProperties()); }//from ww w .j a v a2s . co m } primary.add(proto_query, MUST); } Query mergedQuery; BooleanClause[] clauses = primary.getClauses(); if (clauses.length == 0) { mergedQuery = params.query; } else { BooleanQuery tmpQuery = new BooleanQuery(); tmpQuery.add(params.query, MUST); tmpQuery.add(primary, MUST); mergedQuery = tmpQuery; } if (params.rprops != null) { combined_props.putAll(params.rprops); } Filter filter = this.getQueryFilter(ifilter, combined_props); SimpleQueryFilter sqf = (SimpleQueryFilter) this.cache.get(mergedQuery); if (sqf != null) { mergedQuery = new TermQuery(new Term("_d", "1")); IndexReader reader = params.searcher.getIndexReader(); filter = new SimpleQueryFilter(filter.bits(reader), sqf.bits(reader)); } Object ret = null; int sizeOfResults = 0; try { if (app.debug()) { app.logEvent("running query " + primary); } IndexSearcher searcher = params.searcher; if (params.sort != null) { if (params.max_results == -1) { Hits h = searcher.search(mergedQuery, filter, params.sort); sizeOfResults = h.length(); ret = h; } else { TopFieldDocs tfd = searcher.search(mergedQuery, filter, params.max_results, params.sort); sizeOfResults = tfd.totalHits; ret = tfd; } } else { if (params.max_results == -1) { Hits h = searcher.search(mergedQuery, filter); sizeOfResults = h.length(); ret = h; } else { TopDocs td = searcher.search(mergedQuery, filter, params.max_results); sizeOfResults = td.totalHits; ret = td; } } } catch (Exception ex) { app.logError(ErrorReporter.errorMsg(this.getClass(), "luceneHits") + "Occured on query = " + primary, ex); } if (app.debug()) { long time = System.currentTimeMillis() - start; app.logEvent("... took " + (time / 1000.0) + " seconds\n ------"); } return ret; }
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Query getFilterQuery(FilterObject fobj, ResourceProperties props) throws Exception { final LuceneManager lmgr = this.lmgr; final BooleanClause.Occur SHOULD = BooleanClause.Occur.SHOULD; final BooleanClause.Occur MUST = BooleanClause.Occur.MUST; Query query = null;//from w w w .j a v a 2 s . com try { BooleanQuery primary = null; Iterator<String> iter = fobj.getKeys().iterator(); while (iter.hasNext()) { if (primary == null) { primary = new BooleanQuery(); } BooleanQuery sub_query = new BooleanQuery(); String key = iter.next().toString(); Object[] values = (Object[]) fobj.getValueForKey(key); final int vallen = values.length; if (vallen > 1) { for (int i = 0; i < vallen; i++) { Query q = createLuceneQuery(key, values[i], lmgr, props); sub_query.add(q, SHOULD); } primary.add(sub_query, MUST); } else if (vallen == 1) { primary.add(createLuceneQuery(key, values[0], lmgr, props), MUST); } } query = primary; } catch (Exception ex) { return null; } return query; }
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Query getSearchFilterQuery(SearchFilterObject sfobj, ResourceProperties props) throws Exception { BooleanQuery primary = null;// w w w. j ava 2s. c om Query query = null; Analyzer analyzer = sfobj.getAnalyzer(); SearchProfile profile = sfobj.getSearchProfile(); Object filter = sfobj.getFilter(); String qstr = filter instanceof String ? (String) filter : new String(); BooleanClause.Occur OCCUR = BooleanClause.Occur.MUST; if (profile.operator == null) { OCCUR = BooleanClause.Occur.MUST; } else if (profile.operator.equalsIgnoreCase("and")) { OCCUR = BooleanClause.Occur.MUST; } else if (profile.operator.equalsIgnoreCase("or")) { OCCUR = BooleanClause.Occur.SHOULD; } else if (profile.operator.equalsIgnoreCase("not")) { OCCUR = BooleanClause.Occur.MUST_NOT; } try { if (analyzer != null) { if (primary == null) { primary = new BooleanQuery(); } QueryParser qp = new QueryParser(LuceneManager.ID, analyzer); if (profile != null && !qstr.isEmpty()) { BooleanQuery sub_query = new BooleanQuery(); for (int i = 0; i < profile.fields.length; i++) { Query q = qp.parse(profile.fields[i] + ":" + qstr); q.setBoost(profile.boosts[i]); sub_query.add(q, BooleanClause.Occur.SHOULD); } primary.add(sub_query, BooleanClause.Occur.MUST); sub_query = null; } String profileFilter = profile.filter; if (profileFilter != null && profileFilter.startsWith("{") && profileFilter.endsWith("}")) { profileFilter = "new Filter(" + profileFilter + ")"; } Context cx = Context.getCurrentContext(); Object result = null; if (profileFilter != null) { result = cx.evaluateString(RhinoEngine.getRhinoCore(app).getGlobal(), profileFilter, "eval", 1, null); } IFilter spfilter = null; if (result != null) { spfilter = QueryBean.getFilterFromObject(result); } Query q = null; if (spfilter instanceof FilterObject) { q = getFilterQuery((FilterObject) spfilter, props); } else if (spfilter instanceof NativeFilterObject) { q = getNativeQuery((NativeFilterObject) spfilter); } else if (spfilter instanceof OpFilterObject) { q = parseOpFilter((OpFilterObject) spfilter, props); } else if (spfilter instanceof RangeFilterObject) { q = getRangeQuery((RangeFilterObject) spfilter, props); } else if (spfilter instanceof SearchFilterObject) { q = getSearchFilterQuery((SearchFilterObject) spfilter, props); } if (q != null) { primary.add(q, OCCUR); } query = primary; qp = null; } else { synchronized (this.qparser) { query = this.qparser.parse(qstr); } } } catch (Exception ex) { app.logError(ErrorReporter.errorMsg(this.getClass(), "getSearchFilterQuery") + "Could not parse the input query.\n\t query = " + qstr, ex); throw ex; } return query; }
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Query parseOpFilter(OpFilterObject opf, ResourceProperties props) throws Exception { IFilter[] filters = opf.getFilters(); //want to return all the results if no filters are specified if (filters == null || filters.length == 0) { return null; }//from w w w . ja v a 2 s . com BooleanQuery bq = new BooleanQuery(); BooleanClause.Occur OCCUR = BooleanClause.Occur.MUST; // default is AND if (opf instanceof AndFilterObject) { OCCUR = BooleanClause.Occur.MUST; } else if (opf instanceof OrFilterObject) { OCCUR = BooleanClause.Occur.SHOULD; } else if (opf instanceof NotFilterObject) { OCCUR = BooleanClause.Occur.MUST_NOT; } final int length = filters.length; for (int i = 0; i < length; i++) { Query q = null; if (filters[i] instanceof FilterObject) { try { q = getFilterQuery((FilterObject) filters[i], props); } catch (Exception ex) { q = null; } } else if (filters[i] instanceof NativeFilterObject) { q = getNativeQuery((NativeFilterObject) filters[i]); } else if (filters[i] instanceof OpFilterObject) { try { q = parseOpFilter((OpFilterObject) filters[i], props); } catch (Exception e) { q = null; } } else if (filters[i] instanceof RangeFilterObject) { q = getRangeQuery((RangeFilterObject) filters[i], props); } else if (filters[i] instanceof SearchFilterObject) { q = getSearchFilterQuery((SearchFilterObject) filters[i], props); } if (q != null) { if (OCCUR == BooleanClause.Occur.MUST_NOT) { // every NOT query in lucene must be proceeded with a MUST have query, // since every document in the index will have IDENTITY set to 1, we // precede the NOT query with something we know every document will have, // that is, IDENTITY set to 1. (its a hack, i know) bq.add(new TermQuery(new Term(LuceneManager.IDENTITY, "1")), BooleanClause.Occur.MUST); } bq.add(q, OCCUR); } } return bq; }
From source file:axiom.scripting.rhino.LuceneQueryDispatcher.java
License:Open Source License
private Filter getQueryFilter(IFilter filter, ResourceProperties props) { BooleanQuery query = new BooleanQuery(); try {//from w w w . jav a 2 s. c o m this.parseFilterIntoQuery(filter, query, props); SimpleQueryFilter sqf = (SimpleQueryFilter) this.cache.get(query); if (sqf == null && filter.isCached()) { sqf = new SimpleQueryFilter(query); this.cache.put(query, sqf); } return sqf; } catch (Exception ex) { } return null; }