Hibernate Search + DbUnit Indexing
Asked Answered
H

0

1

I am writing some JUnits for my hibernate search implementation.

I use a HSSQL in memory database. I use DBUnit to populate this DB (an XML file). It definitely works as other non-search tests work with the same data. The search code definitely works as I've tried it in the web-app and it returns the correct records.

I assume that Hibernate Search will only index database entries that have been inserted using Hibernate. I tried to index the db manually using : -

fullTextEntityManager.createIndexer().startAndWait();

I have put this in a bean that runs after Spring initialises

public class SearchIndexer {

  @Autowired
  private EntityManagerFactory entityManagerFactory;

  public SearchIndexer(){
  }

  @PostConstruct
  public void doIndexing(){
    EntityManager em = entityManagerFactory.createEntityManager();
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
    try {
      fullTextEntityManager.createIndexer().startAndWait();
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

I also Autowired it into my JUnit class and ran the doIndexing method manually (to be sure it was being picked up correctly AFTER the data was loaded).

@Before
public void setup() throws Exception{  
  dbUnitAdapter.setup("ClubDaoTest.xml");
  searchIndexer.doIndexing();
  super.before();
}  

The dbUnitAdapter simply takes an XML file and inserts it in the db using DBUnit.

The entity is annotated like so: -

  @Field
  private String name;

  @NotBlank
  private String type;

  @Field
  private String address1;

  @Field
  private String county;

  @Field
  private String address2;
  @Field
  private String town;
  @Field
  private String country;
  @Field
  private String postcode;
  private String telephone;
  private String mobile;
  private String fax;
  @Field
  private String email;

  @Field
  @NumericField
  private long lft;
  @Field
  @NumericField
  private long rgt;

I tried also tried inserting the data using hibernate (creating a Club entity), this didn't work either confusingly. I changed the the search index location from RAM to filesystem and used luke to read it. I could see the data that I'd tried inserting using hibernate, but no other data that I could see, although it was my first time using Luke, so I may have made a mistake.

Hubbard answered 20/8, 2012 at 13:0 Comment(4)
If you insert the data via DBUnit you indeed bypass the automatic indexing. In this case building the index using the mass indexer (after the data is inserted and commited!) is the way to go. This all should probably happen in some sort of setup harness. Long story short, you are having the right idea. To answer the question on why it does not work we would need more information. How do the annotated entities look like, how do you insert and index the data (actual code). Other than that you can enable trace/debug logs to see what is happening in Search. Use Luke to check the index.Birdie
Thanks, updated the question with more infoHubbard
Where are your transactions? What I mean is, is the data visible (aka commited) for the mass indexer after the dbUnitAdapter.setup("ClubDaoTest.xml"); call? The log file should help you to determine what's going on. How does your search code look like? Are you sure you are searching for terms which exists in the index?Birdie
I just have @Transactional around my Club entity. I assumed the dbUnit would insert and commit all the data into the db. After I insert the data I can do hibernate queries on it successfully (there are other non-search tests in the JUnit class). Do I need to explicitly create a transaction and commit? Thanks again for your help!Hubbard

© 2022 - 2024 — McMap. All rights reserved.