I'm responding to this old mail becouse I implemented something like this
similar to http://wiki.apache.org/solr/SolrSnmp . Maybe we could discuss if
this is a good solution.
I'm using Solr 1.4 on a JBoss 4.0.5 and Java 1.5.
In my particular case, what I'm trying to find out is how often the user uses
wildcards on his query.
I implemented a Servlet Filter that extracts the q parameter from the request
and log it (to a diferent log file, just for querys). After that, the filter
pases the query to the MBean, wich parses que query looking for wildcrds and
that's it.
This is not a complete solution but it might help.
What we are planning to do with this is to use de MBean to see live
information, and to parse the query.log for a more detailed analysis of usage.
I put the following code on a jar file on the server's lib directory
-QueryFilter--
public class QueryFilter implements Filter {
private static final Log log = LogFactory.getLog(QueryFilter.class);
private QueryStatsMBean mbean;
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
if(request != null) {
String query = request.getParameter(q);
if(query != null) {
log.info(Query: + query);
mbean.parseQuery(query);
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
log.info(init filter + this);
mbean = new QueryStats();
try {
mbean.create();
registerMBean(mbean.getName(), mbean);
} catch (Exception e) {
throw new
ServletException(e);
}
}
private void registerMBean(String objectName, Object bean) throws
ServletException {
MBeanServer mbs = MBeanServerLocator.locateJBoss();
try {
ObjectName on = new ObjectName(objectName);
mbs.registerMBean(bean, on);
log.info(MBean registered);
}
catch (NotCompliantMBeanException e) { throw new
ServletException(e); }
catch (MBeanRegistrationException e) { throw new
ServletException(e); }
catch (InstanceAlreadyExistsException e) { throw new
ServletException(e); }
catch (MalformedObjectNameException e) { throw new
ServletException(e); }
}
public void destroy() {
log.info(Destroying Filter + this);
mbean.destroy();
}
}
---QueryStatsMBean--
public interface QueryStatsMBean extends org.jboss.system.ServiceMBean {
public Integer
getTotalQueries();
public Integer getAsteriskQueries();
public Integer getQuestionQueries();
public Integer getDefaultQueries();
public void parseQuery(String query);
public void create() throws Exception;
public void start() throws Exception;
public void stop();
public void destroy();
public int fileReaded();
}
QueryStats
public class QueryStats implements QueryStatsMBean{
private Integer totalQueries;
private Integer asteriskQueries;
private Integer questionQueries;
private Integer defaultQueries;
public void create() throws Exception {
totalQueries = new Integer(0);
asteriskQueries = new Integer(0);
questionQueries = new Integer(0);
defaultQueries = new Integer(0);
}
public void parseQuery(String query) {
totalQueries =
new Integer(totalQueries.intValue() + 1);
if(isDefaultQuery(query)) {
defaultQueries = new Integer(defaultQueries.intValue()
+ 1);
}else {
if(hasAsterisk(query)) {
asteriskQueries = new
Integer(asteriskQueries.intValue() + 1);
}
if(hasQuestion(query)) {
questionQueries = new
Integer(questionQueries.intValue() + 1);
}
}
}
private boolean hasQuestion(String query) {
return query.indexOf(?) != -1;
}
private boolean hasAsterisk(String query) {
return query.indexOf(*) != -1;
}
private boolean isDefaultQuery(String defaultQuery) {
return *:*.equals(defaultQuery);
}
public Integer getTotalQueries() {
return totalQueries;
}