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;
        }

        public
 Integer getAsteriskQueries() {
                return asteriskQueries;
        }

        public Integer getQuestionQueries() {
                return questionQueries;
        }

        public Integer getDefaultQueries()
 {
                return defaultQueries;
        }

        public void destroy() {
                /*
                 * I don't need any kind of persistence, but this could be a 
good place
                 * to do it
                 */
        }

        public void start() throws Exception {
                /*
                 * I don't need any kind of persistence, but this could be a 
good place
                 * to do it
                 */

        }

        public void stop() {
                /*
                 * I don't need any kind of persistence, but this could be a 
good place
                 * to do it
                 */
        }

        public String getName() {
                return "Solr:service=QueryStats";
        }

        public int getState() {
                // TODO Auto-generated method stub
                return 0;
        }

        public String getStateString() {
                // TODO Auto-generated method stub
                return
 null;
        }

        public void jbossInternalLifecycle(String arg0) throws Exception {

        }

        public int fileReaded() {
                return 0;

        }

}


--------------------------------------
I also added the code

  <filter>
    <filter-name>MonitorQueryFilter</filter-name>
    <filter-class>ar.com.osde.solr.QueryFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>MonitorQueryFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

to de web.xml of solr.war


Tomás

________________________________

Encontra las mejores recetas con Yahoo! Cocina. 
http://ar.mujer.yahoo.com/cocina/


      Yahoo! Cocina

Encontra las mejores recetas con Yahoo! Cocina.


http://ar.mujer.yahoo.com/cocina/

Reply via email to