Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lucene Query Builder Unification #2802

Merged
merged 14 commits into from
Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public void test_buildLuceneQuery() throws InvalidQueryException, ParseException
String raw, expected;
List<String> fields = new ArrayList<String>();

// Invalid
raw = "%"; expected = "";
checkQuery(fields, raw, expected);

// No fields are provided

Expand Down
22 changes: 20 additions & 2 deletions components/server/src/ome/services/search/FullText.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import ome.conditions.ApiUsageException;
import ome.model.IAnnotated;
import ome.model.IObject;
import ome.model.core.Image;
import ome.system.ServiceFactory;
import ome.util.search.InvalidQueryException;
import ome.util.search.LuceneQueryBuilder;
Expand Down Expand Up @@ -132,11 +133,24 @@ public FullText(SearchValues values, String fields, String from,
throw new ApiUsageException(
"Invalid date format, dates must be in format YYYYMMDD.");
}


if (LuceneQueryBuilder.DATE_ACQUISITION.equals(dateType) &&
!values.onlyTypes.contains(Image.class)) {
// Ignore acquisition ranges for non-images
dFrom = null;
dTo = null;
}

try {
this.queryStr = LuceneQueryBuilder.buildLuceneQuery(fieldsArray, dFrom,
dTo, dateType, query);
log.info("Generated Lucene query: "+this.queryStr);
if (this.queryStr.isEmpty()) {
q = null;
log.info("Generated empty Lucene query");
return; // EARLY EXIT!
} else {
log.info("Generated Lucene query: "+this.queryStr);
}
} catch (InvalidQueryException e1) {
throw new ApiUsageException(
"Invalid query: "+e1.getMessage());
Expand Down Expand Up @@ -293,6 +307,10 @@ protected void initializeQuery(FullTextQuery ftQuery) {
@Transactional(readOnly = true)
public Object doWork(Session s, ServiceFactory sf) {

if (q == null) {
return null;
}

final Class<?> cls = values.onlyTypes.get(0);
FullTextSession session = Search.createFullTextSession(s);
Criteria criteria = criteria(session);
Expand Down
38 changes: 26 additions & 12 deletions components/tools/OmeroPy/src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,7 @@ def buildSearchQuery(self, text, fields=()):

fields = [str(f) for f in fields]
# # for each phrase or token, we strip out all non alpha-numeric
# except when inside double quotes.
# except when inside double quotes.
# To preserve quoted phrases we split by "
phrases = text.split('"')
tokens = []
Expand Down Expand Up @@ -3537,14 +3537,16 @@ def buildSearchQuery(self, text, fields=()):
return text, leadingWc


def searchObjects(self, obj_types, text, created=None, fields=(), batchSize=1000, page=0, searchGroup=None, ownedBy=None):
def searchObjects(self, obj_types, text, created=None, fields=(), batchSize=1000, page=0, searchGroup=None, ownedBy=None,
useAcquisitionDate=False):
"""
Search objects of type "Project", "Dataset", "Image", "Screen", "Plate"
Returns a list of results

@param obj_types: E.g. ["Dataset", "Image"]
@param text: The text to search for
@param created: L{omero.rtime} list or tuple (start, stop)
@param useAcquisitionDate if True, then use Image.acquisitionDate rather than import date for queries.
@return: List of Object wrappers. E.g. L{ImageWrapper}
"""
if not text:
Expand Down Expand Up @@ -3573,24 +3575,36 @@ def getWrapper(obj_type):
details.setOwner(omero.model.ExperimenterI(ownedBy, False))
search.onlyOwnedBy(details, ctx)

text, leadingWc = self.buildSearchQuery(text, fields)
if leadingWc:
search.setAllowLeadingWildcard(True, ctx)
# Matching OMEROGateway.search()
search.setAllowLeadingWildcard(True)
search.setCaseSentivice(False)

if len(text) == 0:
return []
def parse_time(c, i):
try:
t = c[i]
t = unwrap(t)
if t is not None:
t = time.localtime(t / 1000)
t = time.strftime("%Y%m%d", t)
return t
except:
pass
return None

logger.debug("Searching for: '%s'" % text);
d_from = parse_time(created, 0)
d_to = parse_time(created, 1)
d_type = useAcquisitionDate and "acquisitionDate" or "details.creationEvent.time"

try:
if created:
search.onlyCreatedBetween(created[0], created[1], ctx);
rv = []
for t in types:
def actualSearch ():
search.onlyType(t().OMERO_CLASS, ctx)
# search.bySomeMustNone(some, [], [])
search.byFullText(text, ctx)
search.byLuceneQueryBuilder(
",".join(fields),
d_from, d_to, d_type,
text, ctx)

timeit(actualSearch)()
# get results
def searchProcessing ():
Expand Down
15 changes: 10 additions & 5 deletions components/tools/OmeroPy/src/omero/plugins/hql.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def _configure(self, parser):
parser.add_argument(
"--all", help="Perform query on all groups", default=False,
action="store_true", dest="admin")
parser.add_argument(
"--ids-only",
action="store_true",
help="Show only the ids of returned objects")
parser.add_limit_arguments()
parser.add_style_argument()
parser.add_login_arguments()
Expand Down Expand Up @@ -64,7 +68,8 @@ def hql(self, args, loop=False):
p = ParametersI()
p.page(args.offset, args.limit)
rv = self.project(q, args.query, p, ice_map)
has_details = self.display(rv, style=args.style)
has_details = self.display(rv, style=args.style,
idsonly=args.ids_only)
if args.quiet or not sys.stdout.isatty():
return

Expand Down Expand Up @@ -93,9 +98,9 @@ def hql(self, args, loop=False):
self.ctx.dbg("\nCurrent page: offset=%s, limit=%s\n" %
(p.theFilter.offset.val, p.theFilter.limit.val))
rv = self.project(q, args.query, p, ice_map)
self.display(rv, style=args.style)
self.display(rv, style=args.style, idsonly=args.ids_only)
elif id.startswith("r"):
self.display(rv, style=args.style)
self.display(rv, style=args.style, idsonly=args.ids_only)
else:
try:
id = long(id)
Expand All @@ -122,7 +127,7 @@ def hql(self, args, loop=False):
self.ctx.out("%s = %s" % (key, value))
continue

def display(self, rv, cols=None, style=None):
def display(self, rv, cols=None, style=None, idsonly=False):
import omero.all
import omero.rtypes
from omero.util.text import TableBuilder
Expand All @@ -136,7 +141,7 @@ def display(self, rv, cols=None, style=None):
id = ""
values = {}
# Handling for simple lookup
if len(object_list) == 1 and \
if not idsonly and len(object_list) == 1 and \
isinstance(object_list[0], omero.rtypes.RObjectI):
has_details.append(idx)
o = object_list[0].val
Expand Down
84 changes: 75 additions & 9 deletions components/tools/OmeroPy/src/omero/plugins/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@


import sys
import time

from Ice import OperationNotExistException
from omero.cli import CLI
from omero.plugins.hql import HqlControl
from omero.rtypes import robject


HELP = """Search for object ids by string.
Expand Down Expand Up @@ -43,14 +46,46 @@ def _configure(self, parser):
parser.add_argument(
"--index", action="store_true", default=False,
help="Index an object as a administrator")
parser.add_argument(
"--no-parse",
action="store_true",
help="Pass the search string directly to Lucene with no parsing")
parser.add_argument(
"--field", nargs="*",
default=(),
help=("Fields which should be searched "
"(e.g. name, description, annotation)"))
parser.add_argument(
"--from",
dest="_from",
metavar="YYYY-MM-DD",
type=self.date,
help="Start date for limiting searches (YYYY-MM-DD)")
parser.add_argument(
"--to",
dest="_to",
metavar="YYYY-MM-DD",
type=self.date,
help="End date for limiting searches (YYYY-MM-DD")
parser.add_argument(
"--date-type",
default="acquisitionDate",
choices=("acquisitionDate", "import"),
help=("Which field to use for --from/--to "
"(default: acquisitionDate)"))
parser.add_argument(
"type",
help="Object type to search for, e.g. 'Image' or 'Well'")
parser.add_argument(
"search_string", nargs="?",
help="Lucene search string")
HqlControl._configure(self, parser)
parser.set_defaults(func=self.search)
parser.add_login_arguments()

def date(self, user_string):
try:
t = time.strptime(user_string, "%Y-%m-%d")
return time.strftime("%Y%m%d", t)
except Exception, e:
self.ctx.dbg(str(e))
raise

def search(self, args):
c = self.ctx.conn(args)
Expand All @@ -75,17 +110,48 @@ def search(self, args):
c.sf.getUpdateService().indexObject(obj)

else:
group = None
if args.admin:
group = "-1"
ctx = c.getContext(group)
search = c.sf.createSearchService()
try:
try:
# Matching OMEROGateway.search()
search.setAllowLeadingWildcard(True)
search.setCaseSentivice(False)
search.onlyType(args.type)
search.byFullText(args.search_string)
if not search.hasNext():

if args.no_parse:
if args._from or args._to or args.field:
self.ctx.err("Ignoring from/to/fields")
search.byFullText(args.query)
else:
try:
if args.date_type == "import":
args.date_type = "details.creationEvent.time"
search.byLuceneQueryBuilder(
",".join(args.field),
args._from, args._to, args.date_type,
args.query, ctx)
except OperationNotExistException:
self.ctx.err(
"Server does not support byLuceneQueryBuilder")
search.byFullText(args.query)

if not search.hasNext(ctx):
self.ctx.die(433, "No results found.")
while search.hasNext():
results = search.results()

self.ctx.set("search.results", [])
while search.hasNext(ctx):
results = search.results(ctx)
self.ctx.get("search.results").extend(results)
results = [[x] for x in results]
self.display(results)
if not args.ids_only:
results = [[robject(x[0])] for x in results]
self.display(results,
style=args.style,
idsonly=args.ids_only)
except omero.ApiUsageException, aue:
self.ctx.die(434, aue.message)

Expand Down