Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomFieldBridge implements FieldBridge {
- private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
- private final static TimeZone GMT = TimeZone.getTimeZone("GMT");
- @Override
- public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
- Date date = (Date) value;
- Calendar cal = GregorianCalendar.getInstance(GMT);
- cal.setTime(date);
- int year = cal.get(Calendar.YEAR);
- int month = cal.get(Calendar.MONTH) + 1;
- int day = cal.get(Calendar.DAY_OF_MONTH);
- // set year
- addStringField(name + ".year", String.valueOf(year), document, luceneOptions);
- // set month and pad it if needed
- addStringField(name + ".month", month < 10 ? "0" : "" + String.valueOf(month), document, luceneOptions);
- // set day and pad it if needed
- addStringField(name + ".day", day < 10 ? "0" : "" + String.valueOf(day), document, luceneOptions);
- }
- private void addStringField(String fieldName, String fieldValue, Document document, LuceneOptions luceneOptions) {
- Field field = new Field(fieldName, fieldValue, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
- field.setBoost(luceneOptions.getBoost());
- try {
- field.setTokenStream(new StandardAnalyzer().tokenStream(fieldName, new StringReader(fieldValue)));
- } catch (IOException ex) {
- LOGGER.error(null, ex);
- }
- System.out.println("Field : " + field);
- document.add(field);
- }
- }
- @Basic
- @Column(name = "time")
- @Temporal(javax.persistence.TemporalType.TIMESTAMP)
- @Audited
- @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
- @FieldBridge(impl = CustomFieldBridge.class)
- private Date time;
- SearchFactory searchFactory = fullTextSession.getSearchFactory();
- QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(Meeting.class).get();
- org.apache.lucene.search.Query dateQuery = queryBuilder.keyword()
- .onField("time")
- .matching("2014").createQuery();
- org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(dateQuery).setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE);
- List result = fullTextQuery.list();
- .onField("time.year")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement