title: Hibernate/Lucene search: Wildcard query
body: <p>I am trying to implement a wildcard query, but I am stuck...  Can anyone help me?  </p>
  
  <p>If the user is search for instance for AB, I want to return all results matching something like the regular expression "A[/.-'+&amp;, ]{0,1}B[/.-'+&amp;, ]{0,1}".  I know regular expressions can't be used, but I just want to indicate the expected result.</p>
  
  <p>So, searching for "AB" should return results like: "ABC x", "abc x", "Abcdefg", "A.b.c.", "A-B-C", "A B C d", "Table".</p>
  
  <p>I created a model using this analyser:</p>
  
  <pre><code>@Indexed
  @AnalyzerDef(name = "abAnalyzer", tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class), filters =
  {
      @TokenFilterDef(factory = StandardFilterFactory.class),
      @TokenFilterDef(factory = LowerCaseFilterFactory.class),
      @TokenFilterDef(factory = StopFilterFactory.class, params =
      {
          @Parameter(name = "ignoreCase", value = "true")
      })
  })
  public class Foo
  {
      ...
      @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
      @Analyzer(definition = "abAnalyzer")
      private String name;
  
      ...
  }
  </code></pre>
  
  <p>I implemented a query like you can see below.  I get all expected results, except the the result like "A.b.c".  What am I doing wrong?  Where did I misunderstood things?</p>
  
  <pre><code>public List&lt;Foo&gt; getResults(final String searchName)
  {
      Session session = this.sessionFactory.openSession();
      FullTextSession fullTextSession = Search.getFullTextSession(session);
      Transaction tx = fullTextSession.beginTransaction();
  
      BooleanQuery bQuery = new BooleanQuery();
      Analyzer analyzer = fullTextSession.getSearchFactory().getAnalyzer("abAnalyzer");
  
      QueryParser qp = new QueryParser(Version.LUCENE_36, "name", analyzer);
      String cleanedText = qp.parse(searchName).toString("name");
      String[] tokenized = cleanedText.split(""); // split on each character
  
      QueryBuilder qBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Foo.class).get();
  
      org.apache.lucene.search.Query query = qBuilder.keyword().wildcard().onField("name").matching("*" + cleanedText + "*").createQuery();
      bQuery.add(query, BooleanClause.Occur.SHOULD);
  
      query = qBuilder.keyword().wildcard().onField("name").matching("*" + createSearchString(cleanedText) + "*").createQuery();
      bQuery.add(query, BooleanClause.Occur.SHOULD);
  
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(bQuery, Company.class);    
      return hibQuery.list();
  }
  
  ....
  
  private String createSearchString(final String name)
  {
      StringBuilder searchName = new StringBuilder("");
      for (int i = 0; i &lt; name.length(); i++)
      {
          if (searchName.length() &gt; 0)
          {
              searchName.append("?");
          }
          searchName.append(name.charAt(i));
      }
  
      return searchName.toString();
  }
  </code></pre>
  
  <p>I based my code on these resources:</p>
  
  <ul>
  <li><p><a href="http://docs.jboss.org/hibernate/search/4.2/reference/en-US/html_single/#search-query-querydsl" rel="nofollow noreferrer">http://docs.jboss.org/hibernate/search/4.2/reference/en-US/html_single/#search-query-querydsl</a></p></li>
  <li><p><a href="http://www.genericarticles.com/mediawiki/index.php?title=Setting_Up_Hibernate_Search_Project#Wildcard_queries" rel="nofollow noreferrer">http://www.genericarticles.com/mediawiki/index.php?title=Setting_Up_Hibernate_Search_Project#Wildcard_queries</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/15285117/how-to-search-fields-with-wildcard-and-spaces-in-hibernate-search">How to search fields with wildcard and spaces in Hibernate Search</a></p></li>
  </ul>