Java Code Examples for org.apache.lucene.document.DateTools

The following examples show how to use org.apache.lucene.document.DateTools. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: StandardQueryConfigHandler.java    License: Apache License 2.0 6 votes vote down vote up
public StandardQueryConfigHandler() {
  // Add listener that will build the FieldConfig.
  addFieldConfigListener(new FieldBoostMapFCListener(this));
  addFieldConfigListener(new FieldDateResolutionFCListener(this));
  addFieldConfigListener(new PointsConfigListener(this));
  
  // Default Values
  set(ConfigurationKeys.ALLOW_LEADING_WILDCARD, false); // default in 2.9
  set(ConfigurationKeys.ANALYZER, null); //default value 2.4
  set(ConfigurationKeys.DEFAULT_OPERATOR, Operator.OR);
  set(ConfigurationKeys.PHRASE_SLOP, 0); //default value 2.4
  set(ConfigurationKeys.ENABLE_POSITION_INCREMENTS, false); //default value 2.4
  set(ConfigurationKeys.FIELD_BOOST_MAP, new LinkedHashMap<String, Float>());
  set(ConfigurationKeys.FUZZY_CONFIG, new FuzzyConfig());
  set(ConfigurationKeys.LOCALE, Locale.getDefault());
  set(ConfigurationKeys.MULTI_TERM_REWRITE_METHOD, MultiTermQuery.CONSTANT_SCORE_REWRITE);
  set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new HashMap<CharSequence, DateTools.Resolution>());
  
}
 
Example 2
Source Project: lucene-solr   Source File: FieldDateResolutionFCListener.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void buildFieldConfig(FieldConfig fieldConfig) {
  DateTools.Resolution dateRes = null;
  Map<CharSequence, DateTools.Resolution> dateResMap = this.config.get(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP);

  if (dateResMap != null) {
    dateRes = dateResMap.get(
        fieldConfig.getField());
  }

  if (dateRes == null) {
    dateRes = this.config.get(ConfigurationKeys.DATE_RESOLUTION);
  }

  if (dateRes != null) {
    fieldConfig.set(ConfigurationKeys.DATE_RESOLUTION, dateRes);
  }

}
 
Example 3
Source Project: lucene-solr   Source File: QueryParserBase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the date resolution that is used by RangeQueries for the given field.
 * Returns null, if no default or field specific date resolution has been set
 * for the given field.
 *
 */
public DateTools.Resolution getDateResolution(String fieldName) {
  if (fieldName == null) {
    throw new IllegalArgumentException("Field must not be null.");
  }

  if (fieldToDateResolution == null) {
    // no field specific date resolutions set; return default date resolution instead
    return this.dateResolution;
  }

  DateTools.Resolution resolution = fieldToDateResolution.get(fieldName);
  if (resolution == null) {
    // no date resolutions set for the given field; return default date resolution instead
    resolution = this.dateResolution;
  }

  return resolution;
}
 
Example 4
public static Document Document(File f)
     throws java.io.FileNotFoundException {
  Document doc = new Document();
  doc.add(new StoredField("path", f.getPath()));
  doc.add(new StoredField("modified",
                    DateTools.timeToString(f.lastModified(), DateTools.Resolution.MINUTE)));
  
  //create new FieldType to store term positions (TextField is not sufficiently configurable)
  FieldType ft = new FieldType();
  ft.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
  ft.setTokenized(true);
  ft.setStoreTermVectors(true);
  ft.setStoreTermVectorPositions(true);
  Field contentsField = new Field("contents", new FileReader(f), ft);

  doc.add(contentsField);
  return doc;
}
 
Example 5
public static Document Document(String inLine, int lineNumber) {
	  	
		Document doc = new Document();
	    doc.add(new StoredField("line_number", ""+lineNumber));
	    doc.add(new StoredField("modified",
	                      DateTools.timeToString(System.currentTimeMillis(), DateTools.Resolution.MINUTE)));
	    
	    //create new FieldType to store term positions (TextField is not sufficiently configurable)
	    FieldType ft = new FieldType();
	    ft.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
	    ft.setTokenized(true);
	    ft.setStoreTermVectors(true);
	    ft.setStoreTermVectorPositions(true);
	    Field contentsField = new Field("contents", inLine, ft);

	    doc.add(contentsField);
	    return doc;
}
 
Example 6
Source Project: james-project   Source File: LuceneMessageSearchIndex.java    License: Apache License 2.0 6 votes vote down vote up
private Query createQuery(String field, DateOperator dop) throws UnsupportedSearchException {
    Date date = dop.getDate();
    DateResolution res = dop.getDateResultion();
    DateTools.Resolution dRes = toResolution(res);
    String value = DateTools.dateToString(date, dRes);
    switch (dop.getType()) {
    case ON:
        return new TermQuery(new Term(field, value));
    case BEFORE: 
        return new TermRangeQuery(field, DateTools.dateToString(MIN_DATE, dRes), value, true, false);
    case AFTER: 
        return new TermRangeQuery(field, value, DateTools.dateToString(MAX_DATE, dRes), false, true);
    default:
        throw new UnsupportedSearchException();
    }
}
 
Example 7
Source Project: james-project   Source File: LuceneMessageSearchIndex.java    License: Apache License 2.0 6 votes vote down vote up
private DateTools.Resolution toResolution(DateResolution res) {
    switch (res) {
    case Year:
        return DateTools.Resolution.YEAR;
    case Month:
        return DateTools.Resolution.MONTH;
    case Day:
        return DateTools.Resolution.DAY;
    case Hour:
        return DateTools.Resolution.HOUR;
    case Minute:
        return DateTools.Resolution.MINUTE;
    case Second:
        return DateTools.Resolution.SECOND;
    default:
        return DateTools.Resolution.MILLISECOND;
    }
}
 
Example 8
Source Project: uyuni   Source File: Server.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param checkinIn the checkin to set
 */
public void setCheckin(Date checkinIn) {
    if (checkinIn != null) {
        this.checkin = DateTools.dateToString(checkinIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.checkin = null;
    }
}
 
Example 9
Source Project: uyuni   Source File: Server.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param registeredIn the registered to set
 */
public void setRegistered(Date registeredIn) {
    if (registeredIn != null) {
        this.registered = DateTools.dateToString(registeredIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.registered = null;
    }
}
 
Example 10
Source Project: inception   Source File: MtasDocumentIndex.java    License: Apache License 2.0 5 votes vote down vote up
private void indexDocument(String aDocumentTitle, long aSourceDocumentId,
        long aAnnotationDocumentId, String aUser, byte[] aBinaryCas)
    throws IOException
{
    // Calculate timestamp that will be indexed
    String timestamp = DateTools.dateToString(new Date(), DateTools.Resolution.MILLISECOND);
    
    log.trace(
            "Indexing document in project [{}]({}). sourceId: {}, annotationId: {}, "
            + "user: {} timestamp: {}",
            project.getName(), project.getId(), aSourceDocumentId, aAnnotationDocumentId,
            aUser, timestamp);
    
    IndexWriter indexWriter = getIndexWriter();
    
    // Prepare bytearray with document content to be indexed
    String encodedCAS = new String(MtasUtils.bytesToChars(aBinaryCas));

    // Create new Lucene document
    Document doc = new Document();
    
    // Add indexed fields
    doc.add(new StringField(FIELD_ID, String.valueOf(aSourceDocumentId) + "/"
            + String.valueOf(aAnnotationDocumentId), Field.Store.YES));
    doc.add(new StringField(FIELD_SOURCE_DOCUMENT_ID, String.valueOf(aSourceDocumentId),
            Field.Store.YES));
    doc.add(new StringField(FIELD_ANNOTATION_DOCUMENT_ID,
            String.valueOf(aAnnotationDocumentId), Field.Store.YES));
    doc.add(new StringField(FIELD_TITLE, aDocumentTitle, Field.Store.YES));
    doc.add(new StringField(FIELD_USER, aUser, Field.Store.YES));
    doc.add(new StringField(FIELD_TIMESTAMP, timestamp, Field.Store.YES));
    doc.add(new TextField(FIELD_CONTENT, encodedCAS, Field.Store.NO));

    // Add document to the Lucene index
    indexWriter.addDocument(doc);

    scheduleCommit();
}
 
Example 11
Source Project: lucene-solr   Source File: DocData.java    License: Apache License 2.0 5 votes vote down vote up
public void setDate(Date date) {
  if (date != null) {
    setDate(DateTools.dateToString(date, DateTools.Resolution.SECOND));
  } else {
    this.date = null;
  }
}
 
Example 12
Source Project: lucene-solr   Source File: TrecContentSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
private void assertDocData(DocData dd, String expName, String expTitle,
                           String expBody, Date expDate)
    throws ParseException {
  assertNotNull(dd);
  assertEquals(expName, dd.getName());
  assertEquals(expTitle, dd.getTitle());
  assertTrue(dd.getBody().indexOf(expBody) != -1);
  Date date = dd.getDate() != null ? DateTools.stringToDate(dd.getDate()) : null;
  assertEquals(expDate, date);
}
 
Example 13
Source Project: lucene-solr   Source File: QueryParserPaneProvider.java    License: Apache License 2.0 5 votes vote down vote up
private JPanel initDateRangeQuerySettingsPanel() {
  JPanel panel = new JPanel();
  panel.setOpaque(false);
  panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

  JPanel header = new JPanel(new FlowLayout(FlowLayout.LEADING));
  header.setOpaque(false);
  header.add(new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.daterange_query")));
  panel.add(header);

  JPanel resolution = new JPanel(new FlowLayout(FlowLayout.LEADING));
  resolution.setOpaque(false);
  resolution.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
  JLabel resLabel = new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.date_res"));
  resolution.add(resLabel);
  Arrays.stream(DateTools.Resolution.values()).map(DateTools.Resolution::name).forEach(dateResCB::addItem);
  dateResCB.setSelectedItem(config.getDateResolution().name());
  dateResCB.setOpaque(false);
  resolution.add(dateResCB);
  panel.add(resolution);

  JPanel locale = new JPanel(new FlowLayout(FlowLayout.LEADING));
  locale.setOpaque(false);
  locale.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 0));
  JLabel locLabel = new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.locale"));
  locale.add(locLabel);
  locationTF.setColumns(10);
  locationTF.setText(config.getLocale().toLanguageTag());
  locale.add(locationTF);
  JLabel tzLabel = new JLabel(MessageUtils.getLocalizedMessage("search_parser.label.timezone"));
  locale.add(tzLabel);
  timezoneTF.setColumns(10);
  timezoneTF.setText(config.getTimeZone().getID());
  locale.add(timezoneTF);
  panel.add(locale);

  return panel;
}
 
Example 14
Source Project: lucene-solr   Source File: QueryParserBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sets the date resolution used by RangeQueries for a specific field.
 *
 * @param fieldName field for which the date resolution is to be set
 * @param dateResolution date resolution to set
 */
public void setDateResolution(String fieldName, DateTools.Resolution dateResolution) {
  if (fieldName == null) {
    throw new IllegalArgumentException("Field must not be null.");
  }

  if (fieldToDateResolution == null) {
    // lazily initialize HashMap
    fieldToDateResolution = new HashMap<>();
  }

  fieldToDateResolution.put(fieldName, dateResolution);
}
 
Example 15
Source Project: lucene-solr   Source File: TestPrecedenceQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1, false);
  String endDate = getLocalizedDate(2002, 1, 4, false);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  PrecedenceQueryParser qp = new PrecedenceQueryParser(new MockAnalyzer(random()));

  Map<CharSequence, DateTools.Resolution> fieldMap = new HashMap<>();
  // set a field specific date resolution
  fieldMap.put(monthField, DateTools.Resolution.MONTH);
  qp.setDateResolutionMap(fieldMap);

  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);

  // set second field specific date resolution
  fieldMap.put(hourField, DateTools.Resolution.HOUR);
  qp.setDateResolutionMap(fieldMap);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.HOUR);
}
 
Example 16
Source Project: lucene-solr   Source File: TestPrecedenceQueryParser.java    License: Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(PrecedenceQueryParser qp, String field,
    String startDate, String endDate, Date endDateInclusive,
    DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate)
      + " TO " + escapeDateString(endDate) + "]", "["
      + getDate(startDate, resolution) + " TO "
      + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate)
      + " TO " + escapeDateString(endDate) + "}", "{"
      + getDate(startDate, resolution) + " TO "
      + getDate(endDate, resolution) + "}");
}
 
Example 17
Source Project: lucene-solr   Source File: TestQPHelper.java    License: Apache License 2.0 5 votes vote down vote up
/** for testing DateTools support */
private String getDate(String s, DateTools.Resolution resolution)
    throws Exception {
  // we use the default Locale since LuceneTestCase randomizes it
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
  return getDate(df.parse(s), resolution);
}
 
Example 18
Source Project: lucene-solr   Source File: TestQPHelper.java    License: Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1);
  String endDate = getLocalizedDate(2002, 1, 4);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.clear();
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  StandardQueryParser qp = new StandardQueryParser();

  Map<CharSequence, DateTools.Resolution> dateRes =  new HashMap<>();
  
  // set a field specific date resolution    
  dateRes.put(monthField, DateTools.Resolution.MONTH);
  qp.setDateResolutionMap(dateRes);

  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);

  // set second field specific date resolution
  dateRes.put(hourField, DateTools.Resolution.HOUR);
  qp.setDateResolutionMap(dateRes);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two
  // fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate,
      endDateExpected.getTime(), DateTools.Resolution.HOUR);
}
 
Example 19
Source Project: lucene-solr   Source File: TestQPHelper.java    License: Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(StandardQueryParser qp,
    String field, String startDate, String endDate, Date endDateInclusive,
    DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(qp, field, field + ":[" + escapeDateString(startDate) + " TO " + escapeDateString(endDate)
      + "]", "[" + getDate(startDate, resolution) + " TO "
      + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(qp, field, field + ":{" + escapeDateString(startDate) + " TO " + escapeDateString(endDate)
      + "}", "{" + getDate(startDate, resolution) + " TO "
      + getDate(endDate, resolution) + "}");
}
 
Example 20
Source Project: lucene-solr   Source File: QueryParserTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public void testDateRange() throws Exception {
  String startDate = getLocalizedDate(2002, 1, 1);
  String endDate = getLocalizedDate(2002, 1, 4);
  // we use the default Locale/TZ since LuceneTestCase randomizes it
  Calendar endDateExpected = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
  endDateExpected.clear();
  endDateExpected.set(2002, 1, 4, 23, 59, 59);
  endDateExpected.set(Calendar.MILLISECOND, 999);
  final String defaultField = "default";
  final String monthField = "month";
  final String hourField = "hour";
  Analyzer a = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true);
  CommonQueryParserConfiguration qp = getParserConfig(a);
  
  // set a field specific date resolution
  setDateResolution(qp, monthField, DateTools.Resolution.MONTH);
  
  // set default date resolution to MILLISECOND
  qp.setDateResolution(DateTools.Resolution.MILLISECOND);
  
  // set second field specific date resolution    
  setDateResolution(qp, hourField, DateTools.Resolution.HOUR);

  // for this field no field specific date resolution has been set,
  // so verify if the default resolution is used
  assertDateRangeQueryEquals(qp, defaultField, startDate, endDate, 
          endDateExpected.getTime(), DateTools.Resolution.MILLISECOND);

  // verify if field specific date resolutions are used for these two fields
  assertDateRangeQueryEquals(qp, monthField, startDate, endDate, 
          endDateExpected.getTime(), DateTools.Resolution.MONTH);

  assertDateRangeQueryEquals(qp, hourField, startDate, endDate, 
          endDateExpected.getTime(), DateTools.Resolution.HOUR);  
}
 
Example 21
Source Project: lucene-solr   Source File: QueryParserTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public void assertDateRangeQueryEquals(CommonQueryParserConfiguration cqpC, String field, String startDate, String endDate, 
                                       Date endDateInclusive, DateTools.Resolution resolution) throws Exception {
  assertQueryEquals(cqpC, field, field + ":[" + escapeDateString(startDate) + " TO " + escapeDateString(endDate) + "]",
             "[" + getDate(startDate, resolution) + " TO " + getDate(endDateInclusive, resolution) + "]");
  assertQueryEquals(cqpC, field, field + ":{" + escapeDateString(startDate) + " TO " + escapeDateString(endDate) + "}",
             "{" + getDate(startDate, resolution) + " TO " + getDate(endDate, resolution) + "}");
}
 
Example 22
Source Project: lucene-solr   Source File: TestDateSort.java    License: Apache License 2.0 5 votes vote down vote up
private Document createDocument(String text, long time) {
  Document document = new Document();

  // Add the text field.
  Field textField = newTextField(TEXT_FIELD, text, Field.Store.YES);
  document.add(textField);

  // Add the date/time field.
  String dateTimeString = DateTools.timeToString(time, DateTools.Resolution.SECOND);
  Field dateTimeField = newStringField(DATE_TIME_FIELD, dateTimeString, Field.Store.YES);
  document.add(dateTimeField);
  document.add(new SortedDocValuesField(DATE_TIME_FIELD, new BytesRef(dateTimeString)));

  return document;
}
 
Example 23
Source Project: webdsl   Source File: WebDSLDateBridge.java    License: Apache License 2.0 5 votes vote down vote up
public Object stringToObject(String stringValue) {
    if ( StringHelper.isEmpty( stringValue ) ) return null;
    try {
        return DateTools.stringToDate( stringValue );
    }
    catch (ParseException e) {
        throw new SearchException( "Unable to parse into date: " + stringValue, e );
    }
}
 
Example 24
/**
 * Convert a list of Lucene items into a list of generic search items
 * 
 * @param listSource The list of Lucene items
 * @return A list of generic search items
 */
private List<SearchResult> convertList( List<SearchItem> listSource )
{
    List<SearchResult> listDest = new ArrayList<>( );

    for ( SearchItem item : listSource )
    {
        SearchResult result = new SearchResult( );
        result.setId( item.getId( ) );

        try
        {
            result.setDate( DateTools.stringToDate( item.getDate( ) ) );
        }
        catch ( ParseException e )
        {
            AppLogService
                    .error( "Bad Date Format for indexed item \"" + item.getTitle( ) + "\" : " + e.getMessage( ) );
        }

        result.setUrl( item.getUrl( ) );
        result.setTitle( item.getTitle( ) );
        result.setSummary( item.getSummary( ) );
        result.setType( item.getType( ) );
        listDest.add( result );
    }

    return listDest;
}
 
Example 25
Source Project: spacewalk   Source File: Server.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param checkinIn the checkin to set
 */
public void setCheckin(Date checkinIn) {
    if (checkinIn != null) {
        this.checkin = DateTools.dateToString(checkinIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.checkin = null;
    }
}
 
Example 26
Source Project: spacewalk   Source File: Server.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param registeredIn the registered to set
 */
public void setRegistered(Date registeredIn) {
    if (registeredIn != null) {
        this.registered = DateTools.dateToString(registeredIn,
            DateTools.Resolution.MINUTE);
    }
    else {
        this.registered = null;
    }
}
 
Example 27
Source Project: cxf   Source File: LuceneQueryVisitor.java    License: Apache License 2.0 5 votes vote down vote up
private Query createRangeQuery(Class<?> cls, String name, Object value, ConditionType type) {

        boolean minInclusive = type == ConditionType.GREATER_OR_EQUALS || type == ConditionType.EQUALS;
        boolean maxInclusive = type == ConditionType.LESS_OR_EQUALS || type == ConditionType.EQUALS;

        if (String.class.isAssignableFrom(cls) || Number.class.isAssignableFrom(cls)) {
            Query query = null;

            if (Double.class.isAssignableFrom(cls)) {
                query = createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Float.class.isAssignableFrom(cls)) {
                query = createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Long.class.isAssignableFrom(cls)) {
                query = createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else {
                query = createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
            }

            return query;
        } else if (Date.class.isAssignableFrom(cls)) {
            final Date date = getValue(Date.class, getFieldTypeConverter(), value.toString());
            final String luceneDateValue = getString(Date.class, getFieldTypeConverter(), date);

            if (type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS) {
                return TermRangeQuery.newStringRange(name, null, luceneDateValue, minInclusive, maxInclusive);
            }
            return TermRangeQuery.newStringRange(name, luceneDateValue,
                DateTools.dateToString(new Date(), Resolution.MILLISECOND), minInclusive, maxInclusive);
        } else {
            return null;
        }
    }
 
Example 28
Source Project: Lottery   Source File: LuceneContent.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 获得Lucene格式的Document
 * 
 * @param c
 *            文章对象
 * @return
 */
public static Document createDocument(Content c) {
	Document doc = new Document();
	doc.add(new Field(ID, c.getId().toString(), Field.Store.YES,
			Field.Index.NOT_ANALYZED));
	doc.add(new Field(SITE_ID, c.getSite().getId().toString(),
			Field.Store.NO, Field.Index.NOT_ANALYZED));
	doc.add(new Field(RELEASE_DATE, DateTools.dateToString(c
			.getReleaseDate(), Resolution.DAY), Field.Store.NO,
			Field.Index.NOT_ANALYZED));
	Channel channel = c.getChannel();
	while (channel != null) {
		doc.add(new Field(CHANNEL_ID_ARRAY, channel.getId().toString(),
				Field.Store.NO, Field.Index.NOT_ANALYZED));
		channel = channel.getParent();
	}
	doc.add(new Field(TITLE, c.getTitle(), Field.Store.NO,
			Field.Index.ANALYZED));
	if (!StringUtils.isBlank(c.getTxt())) {
		doc.add(new Field(CONTENT, c.getTxt(), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	if(c.getAttr()!=null&&StringUtils.isNotBlank(c.getAttr().get("workplace"))){
		doc.add(new Field(WORKPLACE, c.getAttr().get("workplace"), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	if(c.getAttr()!=null&&StringUtils.isNotBlank(c.getAttr().get("category"))){
		doc.add(new Field(CATEGORY, c.getAttr().get("category"), Field.Store.NO,
				Field.Index.ANALYZED));
	}
	return doc;
}
 
Example 29
Source Project: dacapobench   Source File: Index.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Index either a file or a directory tree.
 * 
 * @param writer
 * @param file
 * @throws IOException
 */
void indexDocs(IndexWriter writer, File file) throws IOException {

  /* Strip the absolute part of the path name from file name output */
  int scratchP = scratch.getCanonicalPath().length() + 1;

  /* do not try to index files that cannot be read */
  if (file.canRead()) {
    if (file.isDirectory()) {
      String[] files = file.list();
      // an IO error could occur
      if (files != null) {
        Arrays.sort(files);
        for (int i = 0; i < files.length; i++) {
          indexDocs(writer, new File(file, files[i]));
        }
      }
    } else {
      System.out.println("adding " + file.getCanonicalPath().substring(scratchP));
      try {
        Document doc = new Document();
        FieldType docFT = new FieldType();
        docFT.setTokenized (false);
        docFT.setStored (true);
        docFT.setIndexOptions (IndexOptions.DOCS);

        // Add the path of the file as a field named "path".  Use a field that is
        // indexed (i.e. searchable), but don't tokenize the field into words.
        doc.add(new Field("path", file.getPath(), docFT));

        // Add the last modified date of the file a field named "modified".  Use
        // a field that is indexed (i.e. searchable), but don't tokenize the field
        // into words.
        doc.add(new Field("modified",
                DateTools.timeToString(file.lastModified(), DateTools.Resolution.MINUTE),
                docFT));

        // Add the contents of the file to a field named "contents".  Specify a Reader,
        // so that the text of the file is tokenized and indexed, but not stored.
        // Note that FileReader expects the file to be in the system's default encoding.
        // If that's not the case searching for special characters will fail.
        docFT.setTokenized (true);
        docFT.setStored (false);
        doc.add(new Field("contents", new FileReader(file), docFT));
        writer.addDocument(doc);
      }
      // at least on windows, some temporary files raise this exception with
      // an "access denied" message
      // checking if the file can be read doesn't help
      catch (FileNotFoundException fnfe) { }
    }
  }
}
 
Example 30
Source Project: lucene-solr   Source File: QueryParserPaneProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public QueryParserConfig getConfig() {
  int phraseSlop = (int) slopFTF.getValue();
  float fuzzyMinSimFloat = (float) minSimFTF.getValue();
  int fuzzyPrefLenInt = (int) prefLenFTF.getValue();

  Map<String, Class<? extends Number>> typeMap = new HashMap<>();
  for (int row = 0; row < pointRangeQueryTable.getModel().getRowCount(); row++) {
    String field = (String) pointRangeQueryTable.getValueAt(row, PointTypesTableModel.Column.FIELD.getIndex());
    String type = (String) pointRangeQueryTable.getValueAt(row, PointTypesTableModel.Column.TYPE.getIndex());
    switch (PointTypesTableModel.NumType.valueOf(type)) {
      case INT:
        typeMap.put(field, Integer.class);
        break;
      case LONG:
        typeMap.put(field, Long.class);
        break;
      case FLOAT:
        typeMap.put(field, Float.class);
        break;
      case DOUBLE:
        typeMap.put(field, Double.class);
        break;
      default:
        break;
    }
  }

  return new QueryParserConfig.Builder()
      .useClassicParser(classicRB.isSelected())
      .defaultOperator(QueryParserConfig.Operator.valueOf((String) defOpCombo.getSelectedItem()))
      .enablePositionIncrements(posIncCB.isSelected())
      .allowLeadingWildcard(wildCardCB.isSelected())
      .splitOnWhitespace(splitWSCB.isSelected())
      .autoGeneratePhraseQueries(genPhraseQueryCB.isSelected())
      .autoGenerateMultiTermSynonymsPhraseQuery(genMultiTermSynonymsPhraseQueryCB.isSelected())
      .phraseSlop(phraseSlop)
      .fuzzyMinSim(fuzzyMinSimFloat)
      .fuzzyPrefixLength(fuzzyPrefLenInt)
      .dateResolution(DateTools.Resolution.valueOf((String) dateResCB.getSelectedItem()))
      .locale(new Locale(locationTF.getText()))
      .timeZone(TimeZone.getTimeZone(timezoneTF.getText()))
      .typeMap(typeMap)
      .build();
}