Example usage for org.apache.lucene.document DateTools dateToString

List of usage examples for org.apache.lucene.document DateTools dateToString

Introduction

In this page you can find the example usage for org.apache.lucene.document DateTools dateToString.

Prototype

public static String dateToString(Date date, Resolution resolution) 

Source Link

Document

Converts a Date to a string suitable for indexing.

Usage

From source file:action.indexing.Fragments.java

License:Apache License

public void dateMethod() {
    Document doc = new Document();
    doc.add(new Field("indexDate", DateTools.dateToString(new Date(), DateTools.Resolution.DAY),
            Field.Store.YES, Field.Index.NOT_ANALYZED));
}

From source file:action.indexing.Fragments.java

License:Apache License

public void numericField() throws Exception {
    Document doc = new Document();
    NumericField price = new NumericField("price");
    price.setDoubleValue(19.99);/*from w  w w.  j  a  v a  2s.c  om*/
    doc.add(price);

    NumericField timestamp = new NumericField("timestamp");
    timestamp.setLongValue(new Date().getTime());
    doc.add(timestamp);

    Date b = new Date();
    NumericField birthday = new NumericField("birthday");
    String v = DateTools.dateToString(b, DateTools.Resolution.DAY);
    birthday.setIntValue(Integer.parseInt(v));
    doc.add(birthday);
}

From source file:com.appeligo.amazon.AmazonIndexer.java

License:Apache License

public void addAmazonItem(AmazonItem item, String programId) {
    Document doc = new Document();
    doc.add(new Field("asin", item.getId(), Store.YES, Index.UN_TOKENIZED));
    if (item.getTitle() != null) {
        doc.add(new Field("title", item.getTitle(), Store.YES, Index.TOKENIZED));
    }/* w w w. j  av a2s.com*/
    if (item.getDetailsUrl() != null) {
        doc.add(new Field("detailsUrl", item.getDetailsUrl(), Store.YES, Index.NO));
    }
    if (item.getSmallImageUrl() != null) {
        doc.add(new Field("smallImageUrl", item.getSmallImageUrl(), Store.YES, Index.NO));
        doc.add(new Field("smallImageWidth", Integer.toString(item.getSmallImageWidth()), Store.YES, Index.NO));
        doc.add(new Field("smallImageHeight", Integer.toString(item.getSmallImageHeight()), Store.YES,
                Index.NO));
    }
    doc.add(new Field("programId", programId, Store.YES, Index.UN_TOKENIZED));
    doc.add(new Field("storeTime", DateTools.dateToString(new Date(), Resolution.MINUTE), Store.YES,
            Index.UN_TOKENIZED));

    queue.addDocument(doc);
}

From source file:com.appeligo.amazon.ProgramIndexer.java

License:Apache License

protected void deleteExpiredPrograms() throws SQLException, IOException {
    //do a range query to find all the documents we need to delete
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, -staleDays);
    String start = DateTools.dateToString(new Date(0), Resolution.DAY);
    String end = DateTools.dateToString(calendar.getTime(), Resolution.DAY);

    RangeQuery query = new RangeQuery(new Term("storeTime", start), new Term("storeTime", end), true);
    Hits hits = searcher.search(query);// www .j  a v  a  2 s . c om

    //build a term list of terms that match the programs we want to delete
    int length = hits.length();
    Term[] terms = new Term[length];
    String programId;
    Document doc;
    for (int i = 0; i < length; i++) {
        doc = hits.doc(i);
        programId = doc.get("programId");
        terms[i] = new Term("programId", programId);
    }
    writer.deleteDocuments(terms);
    if (log.isInfoEnabled()) {
        log.info("Deleted " + length + " stale documents from product index.");
    }
}

From source file:com.appeligo.amazon.ProgramIndexer.java

License:Apache License

/**
 * Adds a marker program so it won't query for this programId until the time expires.
 * @param programId the programId to add a marker for
 * @throws IOException//w w w.  j a va 2 s  .c o  m
 */
private void addMarkerProgram(String programId) throws IOException {
    Document doc = new Document();
    doc.add(new Field("type", "marker", Store.NO, Index.UN_TOKENIZED));
    doc.add(new Field("programId", programId, Store.YES, Index.UN_TOKENIZED));
    doc.add(new Field("storeTime", DateTools.dateToString(new Date(), Resolution.DAY), Store.YES,
            Index.UN_TOKENIZED));
    writer.addDocument(doc);
}

From source file:com.appeligo.amazon.ProgramIndexer.java

License:Apache License

protected Document createProductDocument(AmazonItem item, String programId) {
    Document doc = new Document();
    doc.add(new Field("type", "product", Store.NO, Index.UN_TOKENIZED));
    doc.add(new Field("asin", item.getId(), Store.YES, Index.UN_TOKENIZED));
    if (item.getTitle() != null) {
        doc.add(new Field("title", item.getTitle(), Store.YES, Index.TOKENIZED));
    }// ww  w.  ja  va 2s .com
    if (item.getDetailsUrl() != null) {
        doc.add(new Field("detailsUrl", item.getDetailsUrl(), Store.YES, Index.NO));
    }
    if (item.getSmallImageUrl() != null) {
        doc.add(new Field("smallImageUrl", item.getSmallImageUrl(), Store.YES, Index.NO));
        doc.add(new Field("smallImageWidth", Integer.toString(item.getSmallImageWidth()), Store.YES, Index.NO));
        doc.add(new Field("smallImageHeight", Integer.toString(item.getSmallImageHeight()), Store.YES,
                Index.NO));
    }
    doc.add(new Field("programId", programId, Store.YES, Index.UN_TOKENIZED));
    doc.add(new Field("storeTime", DateTools.dateToString(new Date(), Resolution.DAY), Store.YES,
            Index.UN_TOKENIZED));

    return doc;
}

From source file:com.appeligo.captions.DeleteOldProgramsThread.java

License:Apache License

@Override
public void run() {

    while (true) {
        LuceneIndexer liveIndex = LuceneIndexer.getInstance(liveIndexLocation);
        Calendar wayback = Calendar.getInstance();
        wayback.add(Calendar.MONTH, -3);
        Calendar tenminutesago = Calendar.getInstance();
        tenminutesago.add(Calendar.MINUTE, -10);
        log.info("Deleting old programs from live index, between " + wayback.getTime() + " and "
                + tenminutesago.getTime());
        String dateField = "lineup-" + liveLineup + "-endTime";
        ConstantScoreRangeQuery dateQuery = new ConstantScoreRangeQuery(dateField,
                DateTools.dateToString(wayback.getTime(), DateTools.Resolution.MINUTE),
                DateTools.dateToString(tenminutesago.getTime(), DateTools.Resolution.MINUTE), true, true);
        IndexSearcher searcher = null;// w  w  w .  j a  v a  2  s.  c  o m
        try {
            searcher = new IndexSearcher(liveIndexLocation);
            Hits hits = searcher.search(dateQuery);
            Set<Term> terms = new HashSet<Term>();
            if (hits.length() > 0) {
                for (int index = 0; index < hits.length(); index++) {
                    Document doc = hits.doc(index);
                    Term term = new Term(dateField, doc.get(dateField));
                    terms.add(term);
                }
            }
            liveIndex.deleteDocuments(terms.toArray(new Term[terms.size()]));
        } catch (IOException e) {
            log.error("Error deleting old programs from live index", e);
        } finally {
            if (searcher != null) {
                try {
                    searcher.close();
                } catch (IOException e) {
                    log.error("Error closing searcher when deleting old programs from live index", e);

                }
            }
        }

        Calendar cal = Calendar.getInstance();
        int minute = cal.get(Calendar.MINUTE);
        if (minute < 15) {
            cal.set(Calendar.MINUTE, 15);
        } else if (minute >= 45) {
            cal.set(Calendar.MINUTE, 15);
            cal.add(Calendar.HOUR, 1);
        } else {
            cal.set(Calendar.MINUTE, 45);
        }
        log.info("queued up that delete, now we're waiting until " + cal.getTime());
        Utils.sleepUntil(cal.getTimeInMillis());
    }
}

From source file:com.appeligo.lucene.DocumentUtil.java

License:Apache License

public static void populateDocument(Document doc, List<ScheduledProgram> programs, Date modified) {
    // Now add the details of the ScheduledProgram.
    for (ScheduledProgram program : programs) {
        if (program.getNetwork() != null) {
            Network network = program.getNetwork();
            if (network.getStationCallSign() != null) {
                doc.add(new Field("lineup-" + program.getLineupId() + "-stationCallSign",
                        network.getStationCallSign(), Field.Store.YES, Field.Index.UN_TOKENIZED));
            }//from   w  w w .  j  av a 2  s.  c  om
            if (network.getAffiliation() != null) {
                doc.add(new Field("lineup-" + program.getLineupId() + "-affiliation", network.getAffiliation(),
                        Field.Store.YES, Field.Index.TOKENIZED));

            }
            if (network.getStationName() != null) {
                doc.add(new Field("lineup-" + program.getLineupId() + "-stationName", network.getStationName(),
                        Field.Store.YES, Field.Index.TOKENIZED));
            }
        }
        if (program.getStartTime() != null) {
            doc.add(new Field("lineup-" + program.getLineupId() + "-startTime",
                    DateTools.dateToString(program.getStartTime(), DateTools.Resolution.MINUTE),
                    Field.Store.YES, Field.Index.UN_TOKENIZED));
        }

        if (program.getEndTime() != null) {
            doc.add(new Field("lineup-" + program.getLineupId() + "-endTime",
                    DateTools.dateToString(program.getEndTime(), DateTools.Resolution.MINUTE), Field.Store.YES,
                    Field.Index.UN_TOKENIZED));
        }
        doc.add(new Field("lineup-" + program.getLineupId(), "true", Field.Store.YES,
                Field.Index.UN_TOKENIZED));

    }
    ScheduledProgram program = programs != null && programs.size() > 0 ? programs.get(0) : null;
    if (program != null) {
        if (program.getDescriptionWithActors() != null) {
            doc.add(new Field("description", program.getDescriptionWithActors(), Field.Store.YES,
                    Field.Index.TOKENIZED));
        } else if (program.getDescription() != null) {
            doc.add(new Field("description", program.getDescription(), Field.Store.YES, Field.Index.TOKENIZED));
        }

        if (program.getProgramId() != null) {
            doc.add(new Field("programID", program.getProgramId(), Field.Store.YES, Field.Index.UN_TOKENIZED));
        }

        if (program.getProgramTitle() != null) {
            doc.add(new Field("programTitle", program.getProgramTitle(), Field.Store.YES,
                    Field.Index.TOKENIZED));
        }

        if (program.getEpisodeTitle() != null) {
            doc.add(new Field("episodeTitle", program.getEpisodeTitle(), Field.Store.YES,
                    Field.Index.TOKENIZED));
        }

        if (program.getReducedTitle40() != null) {
            doc.add(new Field("reducedTitle40", program.getReducedTitle40(), Field.Store.YES,
                    Field.Index.UN_TOKENIZED));
        }

        if (program.getLabel() != null) {
            doc.add(new Field("programLabel", program.getLabel(), Field.Store.YES, Field.Index.UN_TOKENIZED));
        }

        if (program.getWebPath() != null) {
            doc.add(new Field("webPath", program.getWebPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));
        }

        if (program.getProgramType() != null) {
            doc.add(new Field("programType", program.getProgramType().toString(), Field.Store.YES,
                    Field.Index.TOKENIZED));
        }

        doc.add(new Field("lastModified", DateTools.dateToString(modified, DateTools.Resolution.MINUTE),
                Field.Store.YES, Field.Index.UN_TOKENIZED));

        if (program.getTvRating() != null) {
            doc.add(new Field("tvRating", program.getTvRating(), Field.Store.YES, Field.Index.UN_TOKENIZED));
        }

        if (program.getStarRating() > 0) {
            double rating = program.getStarRating();
            doc.add(new Field("starRating", pad((int) (rating * 1000)), Field.Store.YES,
                    Field.Index.UN_TOKENIZED));
        }

        List<Credit> credits = program.getCredits();
        StringBuilder sb = new StringBuilder();
        for (Credit credit : credits) {
            String first = credit.getFirstName();
            if (first != null && first.trim().length() > 0) {
                sb.append(' ');
                sb.append(first);
            }
            String last = credit.getLastName();
            if (last != null && last.trim().length() > 0) {
                sb.append(' ');
                sb.append(last);
            }
        }
        doc.add(new Field("credits", sb.toString(), Field.Store.NO, Field.Index.TOKENIZED));
        doc.add(new Field("newEpisode", Boolean.toString(program.isNewEpisode()), Field.Store.YES,
                Field.Index.UN_TOKENIZED));
        doc.add(new Field("programType", program.getProgramType().toString(), Field.Store.YES,
                Field.Index.UN_TOKENIZED));
        if (program.getGenreDescription() == null) {
            doc.add(new Field("genre", "", Field.Store.NO, Field.Index.TOKENIZED));
        } else {
            doc.add(new Field("genre", program.getGenreDescription(), Field.Store.NO, Field.Index.TOKENIZED));
        }
    }
}

From source file:com.appeligo.search.actions.SearchResults.java

License:Apache License

private Query generateLuceneQuery(String givenQuery, IndexSearcher searcher) throws ParseException {
    if (givenQuery == null) {
        givenQuery = getQuery();//  w  ww  . j av  a 2s .  co  m
    }
    HashMap<String, Float> boost = new HashMap<String, Float>();
    boost.put("programTitle", 8.0f);
    boost.put("episodeTitle", 3.0f);
    boost.put("description", 2.0f);
    MultiFieldQueryParser parser = new MultiFieldQueryParser(
            new String[] { "text", "description", "programTitle", "episodeTitle", "credits", "genre" },
            analyzer, boost);
    parser.setDefaultOperator(Operator.AND);

    Query luceneQuery = null;
    try {
        luceneQuery = parser.parse(givenQuery);
    } catch (ParseException e) {
        log.error("Error parsing query for : " + givenQuery);
        if (log.isDebugEnabled()) {
            log.debug("Error parsing query for : " + givenQuery, e);
        }
        givenQuery = QueryParser.escape(givenQuery);
        luceneQuery = parser.parse(givenQuery);
    }
    BooleanQuery combinedQuery = new BooleanQuery();
    combinedQuery.add(luceneQuery, Occur.MUST);

    //This will move into a setting on the user.
    Query tvma = new TermQuery(new Term("tvRating", "TVMA"));
    combinedQuery.add(tvma, Occur.MUST_NOT);

    if (lineup != null) {
        //Only find programs that were on networks that are in the lineup
        TermQuery lineupQuery = new TermQuery(new Term("lineup-" + lineup, "true"));
        combinedQuery.add(lineupQuery, Occur.MUST);
    }

    if (searchType != null) {
        if (lineup == null) {
            throw new ParseException(
                    "Lineup cannot be null if searching based on date.  searchType=" + searchType);
        }
        switch (searchType) {
        case FUTURE: {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DAY_OF_YEAR, 14);
            Date future = cal.getTime();
            Date now = new Date();
            ConstantScoreRangeQuery dateQuery = new ConstantScoreRangeQuery("lineup-" + lineup + "-endTime",
                    DateTools.dateToString(now, DateTools.Resolution.MINUTE),
                    DateTools.dateToString(future, DateTools.Resolution.MINUTE), true, true);
            combinedQuery.add(dateQuery, Occur.MUST);
            break;
        }
        case TODAY: {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
            Date future = cal.getTime();
            Date now = new Date();
            ConstantScoreRangeQuery dateQuery = new ConstantScoreRangeQuery("lineup-" + lineup + "-endTime",
                    DateTools.dateToString(now, DateTools.Resolution.MINUTE),
                    DateTools.dateToString(future, DateTools.Resolution.MINUTE), true, true);
            combinedQuery.add(dateQuery, Occur.MUST);
            break;
        }
        }
    }
    if (modifiedSince != null) {
        Date now = new Date();
        ConstantScoreRangeQuery dateQuery = new ConstantScoreRangeQuery("lastModified",
                DateTools.dateToString(modifiedSince, DateTools.Resolution.MINUTE),
                DateTools.dateToString(now, DateTools.Resolution.MINUTE), true, true);
        combinedQuery.add(dateQuery, Occur.MUST);
    }
    return combinedQuery;
}

From source file:com.aurel.track.lucene.LuceneUtil.java

License:Open Source License

/**
 * Typically the value itself converted to string
 * But there are some exceptions where the toString()
 * doesn't work as expected. // w  ww .ja  v a 2s.c o m
 * It should be implemented
 * specific to the lucene requirement to be indexable
 * set the date according to offset from the GMT
 * see http://www.gossamer-threads.com/lists/lucene/java-user/39303?search_string=DateTools;#39303
 * @param value
 * @return
 */
public static String getLuceneDateValue(Object value) {
    Calendar cal = new GregorianCalendar();
    int minutesOffset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000);
    if (value != null) {
        Date dateValue = null;
        try {
            dateValue = (Date) value;
        } catch (Exception e) {
            LOGGER.error("The type of the lucene value is " + value.getClass().getName()
                    + ". Casting it to Date failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        if (dateValue != null) {
            cal.setTime(dateValue);
            cal.add(Calendar.MINUTE, minutesOffset);
            return DateTools.dateToString(cal.getTime(), DateTools.Resolution.DAY);
        }
    }
    return null;
}