Archive for April 2008
problem with lucene number range filter
today i was almost stucked with lucene RangeFilter. here is description -
i have used this query – “price:[0 TO 2000]” to find all items which are bellow 2000.
but my search result was also including 12000 any clue ?
good news is i got my solution though it took bit longer to find my actual reason.
the quick answer is i have to use NumberTools.longToString(…) to index my number fields and to perform range search i have to convert my range value using NumberTools.
you can see my code here -
source view
just digging through lucene source code here is what i found -
to perform range query (which is later rewrite to range filter), it tooks lower token for example price: [0 TO 1000] here ‘0′ is the lower token. it asks TermInfosReader to return a list of available terms for a specific field (such as price here).
RangeFilter iterate through each suggested term and seek for the token position in a random access segment file.
the problem is, the suggested terms are not sorted. also i didn’t find any way to sort them before filtering out.
though i index my content after sorting them properly. but it didn’t affect any result.
since lucene treats everything as string, and it uses String::compare to determine the range position, i think NumberTools, DateTools are the only way out there.
anyway, i am satisfy with what i got.
best wishes,
“…i do what i love to do…”
create new url protocol to load spring configuration resource from the relative path
i had a problem with spring framework, as you know while we are defining dependency over the xml documents, it becomes less changeable unless you know about my recent post about ext-util namespace handler and if you know any other alternatives.
before digging inside the problem better i clarify my context -
i have been working with one of my projects, where i have a several configurations which suppose to be different in different deployment environment, for example – to make my service avail, user has to define his database, his content index directory, server port, ip address and few more.
all these configurations were kept within the spring bean declaration scope. though few of them were moved to the properties file through ext-util namespace handler.
previously to make those configuration changeable i kept those files under WEB-INF/classes so it overrides the default bundled resource.
but still it wasn’t user friendly, since my user has to change one xml document to add more index services also he has to edit different configurations from java properties file.
again when i started bundling jetty with my whole application i found it is tricker to keep those xml outside of application, since i don’t wanna make my user confused with lot of xml files. and i have to keep few xml out of my classpath context, so spring has to load my configuration from my “config/index-configuration/” directory.
though by default spring can do it over “file:///absolute/path/to/file.xml” since spring is using java URL class to locate this resource. but in my case i can’t put any aboslute path.
so i found a simple way out there to support relative path reference.
previously i had the following xml document -
now i have replaced them with the following change -
if you observe closely you will find “extfile:///” protocol reference, which is not standard url protocol. so to make it work i used the following bunch of code and configuration -
1. i have created a class “Handler” under the “com.ideabase.repository.core.protocols.extfile” package.
this package and class name is conventional, though you can change them lets sun explain about it.
2. i wrote the following code -
public class Handler extends URLStreamHandler {
protected URLConnection openConnection(final URL pURL) throws IOException {
final String path = pURL.getPath();
final URL resourceUrl;
if (path.startsWith(“/./”)) {
resourceUrl = new File(path.substring(1, path.length())).toURL();
} else {
throw new RuntimeException(“Unsupported url schema. currently you can ” +
“use only extfile:///./path/to/somewhere/in”);
}
return resourceUrl.openConnection();
}
}
3. i registered my protocol package through the system property -
System.setProperty(“java.protocol.handler.pkgs”, “com.ideabase.repository.core.protocols”);
now have a look on the main applicationContext.xml
if you observe closely again you could find the difference, on the normal case i have used relative path reference. so it must be from classloader or if it is loaded through file system based resource locator it will be located from the file system.
but now i can use cross context (relative file system and class loader path reference) resource file path reference. you can see i have imported “indexer-beans.xml” from the application relative path “config/index-configuration/…” where “data-access-layer-beans.xml” is loaded from the class loader.
now have a quick look on my configuration directory, to get an idea how i have improved configuration ability.

hope this will help you to solve similar problem.
sqabd – lightning talks: what’s preventing me to write nearly accurate code
wow, i didn’t thought that moment would be too short. thanks goes to sajjad bhai and whole volunteering team, without them we were not suppose to get that WoWsome evening.
those who bored by my annoying, prolonging and noisy session, i am apologized. good suggestion might help me to understand how i can improve
here is my presentation slide, hope this will help you guys to check through about “what is preventing me” and “how to write nearly accurate code”
best of luck




