2 Replies Latest reply on Jan 7, 2013 12:38 PM by drathnow

    Query not working with Infinispan Cache

    fzipper

      I've been trying out the query capabilities of Inifinispan and was able to get the sample from the documention working but I seem to be having problems getting my own test program to work.  I have a ControlRequest class that is indexed by a SiteAddress field, which uses a StringBridge to convert it's value to a string.  However, the test code below doesn't seem to work.  I'm expecting it to return only 3 entries to me by it is returning all 5 in the cache. 

       

      Could some kind soul enlighten me as to what I'm doing wrong?

       

      Thanks!

       

      Footnote: If I change the address2 to  NuidSiteAddress(1234, 18) , the query to work???

       

      public class Test {

       

          private static final SiteAddress address1 = new IpSiteAddress("1.2.3.4", 17); // toString="1.2.3.4/17"

          private static final SiteAddress address2 = new NuidSiteAddress(1234, 17);    // toString="1234/17"

         

          public static void main(String[] args) {

              org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout("%m%n")));

              org.apache.log4j.Logger.getRootLogger().setLevel(Level.TRACE);

              SearchMapping mapping = new SearchMapping();

              mapping.entity(ControlRequest.class).indexed().providedId();

              

              Properties properties = new Properties();

              properties.put(org.hibernate.search.Environment.MODEL_MAPPING, mapping);

              

              Configuration infinispanConfiguration = new ConfigurationBuilder()

                    .indexing()

                       .enable()

                       .indexLocalOnly(true)

                       .withProperties(properties)

                    .build();       

              Cache<Long, ControlRequest> cache = new DefaultCacheManager(infinispanConfiguration).getCache();

             

              cache.put(1L, new ControlRequest(address1));

              cache.put(2L, new ControlRequest(address1));

              cache.put(3L, new ControlRequest(address1));

              cache.put(4L, new ControlRequest(address2));

              cache.put(5L, new ControlRequest(address2));

             

              SearchManager searchManager = org.infinispan.query.Search.getSearchManager(cache);

              QueryBuilder queryBuilder = searchManager.buildQueryBuilderForClass(ControlRequest.class).get();

              org.apache.lucene.search.Query luceneQuery = queryBuilder

                      .keyword()

                      .onField("siteAddress")

                      .matching(address1) // Also tried this with "address1.toString()"

                      .createQuery();

              CacheQuery query = searchManager.getQuery(luceneQuery, ControlRequest.class);

              List<Object> objectList = query.list();

              Assert.assertEquals(3, objectList.size());

          }

      }

       

      public class ControlRequest {

          @Field

          @FieldBridge(impl = SiteAddressFieldBridge.class)

          private SiteAddress siteAddress;

       

          public ControlRequest(SiteAddress siteAddress) {

              this.siteAddress = siteAddress;

          }

       

          public SiteAddress getSiteAddress() {

              return siteAddress;

          }

      }

       

      public class SiteAddressFieldBridge implements StringBridge {

       

          @Override

          public String objectToString(Object object) {

              return object.toString();

          }

      }

        • 1. Re: Query not working with Infinispan Cache
          fzipper

          No response so far so how about this one:

           

          I took the sample from the documentation page and wrote it using my own values:

           

           

           

          public class Test2 {

           

              public static class Author {

                  long id;

                  @Field String name;

                  @Field String surname;

           

                  public Author(long id, String name, String surname) {

                      this.id = id;

                      this.name = name;

                      this.surname = surname;

                  }

           

                  public long getId() {

                      return id;

                  }

              }

           

              public static void main(String[] args) {

                  org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout("%m%n")));

                  org.apache.log4j.Logger.getRootLogger().setLevel(Level.TRACE);

                  SearchMapping mapping = new SearchMapping();

                  mapping.entity(Author.class).indexed().providedId()

                        .property("name", ElementType.METHOD).field()

                        .property("surname", ElementType.METHOD).field();

           

                  Properties properties = new Properties();

                  properties.put(org.hibernate.search.Environment.MODEL_MAPPING, mapping);

                  properties.put("hibernate.search.[other options]", "[...]");

           

                  Configuration infinispanConfiguration = new ConfigurationBuilder()

                        .indexing()

                           .enable()

                           .indexLocalOnly(true)

                           .withProperties(properties)

                        .build();

           

                  DefaultCacheManager cacheManager = new DefaultCacheManager(infinispanConfiguration);

           

                  Cache<Long, Author> cache = cacheManager.getCache();

           

                  cache.put(1L, new Author(1, "1.2.3.4/17", "Surtani1"));

                  cache.put(2L, new Author(2, "1.2.3.4/17", "Surtani2"));

                  cache.put(3L, new Author(3, "4321/17", "Surtani3"));

                  cache.put(4L, new Author(4, "4321/17", "Surtani4"));

                  cache.put(5L, new Author(5, "Bob", "Surtani5"));

           

                  SearchManager sm = org.infinispan.query.Search.getSearchManager(cache);

                  QueryBuilder queryBuilder = sm.buildQueryBuilderForClass(Author.class).get();

                  org.apache.lucene.search.Query query = queryBuilder

                                                              .keyword()

                                                              .onField("name")

                                                              .matching("1234/17")

                                                              .createQuery();

                  CacheQuery cq = sm.getQuery(query, Author.class);

                  Assert.assertEquals(0, cq.getResultSize());

              }

           

          }

           

          This fails with

           

          Exception in thread "main" junit.framework.AssertionFailedError: expected:<0> but was:<4>

           

          Could anyone explain that one to me???

          • 2. Re: Query not working with Infinispan Cache
            drathnow

            I think you're running into a problem with Lucene Analyzers, which are responsible for breaking up index values into tokens.  The default analyzer treats non-alphabetic characters as token separators so it is breaking up your query string into multiple tokens (i.e. "1234/17" is broken up into "1234" and "17").  Try using "WhitespaceAnalyzer" instead. The easiest way to do this is to annotate your  ControlRequest and Author class with "@Analyzer(impl = WhitespaceAnalyzer.class)"

             

            Dave.