public StandardQueryConfigHandler() { // Add listener that will build the FieldConfig. addFieldConfigListener(new FieldBoostMapFCListener(this)); addFieldConfigListener(new FieldDateResolutionFCListener(this)); addFieldConfigListener(new NumericFieldConfigListener(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.LOWERCASE_EXPANDED_TERMS, true); //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_AUTO_REWRITE_DEFAULT); set(ConfigurationKeys.FIELD_DATE_RESOLUTION_MAP, new HashMap<CharSequence, DateTools.Resolution>()); }
@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); } }
/** * Returns the Lucene {@link Query} represented by the specified * {@link Search} and key filter. * * @param expression * the expression * @param command * the command * @return a Lucene {@link Query} */ private Query query(String expression, ReadCommand command) { try { QueryParser queryParser = new QueryParser("query", this.indexOptions.search.defaultAnalyzer); queryParser.setDateResolution(Resolution.SECOND); Query searchQuery = queryParser.parse(expression); return searchQuery; } catch (ParseException e) { // TODO Auto-generated catch block throw new FhirIndexException(e); } // TODO: mejorar las busquedas por tipo // Optional<Query> maybeKeyRangeQuery = query(command); // if (maybeKeyRangeQuery.isPresent()) { // BooleanQuery.Builder builder = new BooleanQuery.Builder(); // builder.add(maybeKeyRangeQuery.get(), FILTER); // builder.add(searchQuery, MUST); // return builder.build(); // } else { // } }
/** * Transforms the user entered date string to lucene date string format * by trying to reconstruct the Date object either by local or by ISO (yyy-MM-dd) * DateTools calculates in GMT (comparing to the server TimeZone), so it should be adjusted * @param originalFieldValue * @param locale * @return */ private static String transformDateFields(String originalFieldValue, Locale locale) { String dateString = originalFieldValue; Date date; //DateTimeUtils dtu = new DateTimeUtils(locale); date = DateTimeUtils.getInstance().parseGUIDate(originalFieldValue, locale); if (date==null) { date = DateTimeUtils.getInstance().parseShortDate(originalFieldValue, locale); } //set the date according to offset from the GMT //see http://www.gossamer-threads.com/lists/lucene/java-user/39303?search_string=DateTools;#39303 Calendar cal = new GregorianCalendar(); int minutesOffset = (cal.get(Calendar.ZONE_OFFSET) + cal.get(Calendar.DST_OFFSET)) / (60 * 1000); if (date!=null) { cal.setTime(date); cal.add(Calendar.MINUTE, minutesOffset); return DateTools.dateToString(cal.getTime(), Resolution.DAY); } date = DateTimeUtils.getInstance().parseISODate(originalFieldValue); if (date!=null) { cal.setTime(date); cal.add(Calendar.MINUTE, minutesOffset); return DateTools.dateToString(cal.getTime(), Resolution.DAY); } return dateString; }
public void indexProtocolSearchableFieldsBean(ProtocolSearchableFieldsBean protocolFieldsBean) throws IOException { IndexWriter writer = getIndexWriter(false); Document doc = new Document(); doc.add(new StringField("protocolId", protocolFieldsBean.getProtocolId(), Field.Store.YES)); if(protocolFieldsBean.getProtocolFileDesc()!=null) doc.add(new StringField("protocolFileDesc", protocolFieldsBean.getProtocolFileDesc(), Field.Store.YES)); if(protocolFieldsBean.getProtocolFileId()!=null) doc.add(new StringField("protocolFileId", protocolFieldsBean.getProtocolFileId(), Field.Store.YES)); doc.add(new StringField("protocolName", protocolFieldsBean.getProtocolName(), Field.Store.YES)); if(protocolFieldsBean.getProtocolFileName()!=null) doc.add(new StringField("protocolFileName", protocolFieldsBean.getProtocolFileName(), Field.Store.YES)); if(protocolFieldsBean.getCreatedDate()!=null){ try{ doc.add(new Field("createdDate", DateTools.stringToDate(DateTools.dateToString(protocolFieldsBean.getCreatedDate(),Resolution.SECOND)).toString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); }catch(ParseException e){ e.printStackTrace(); } } String fullSearchableText = protocolFieldsBean.getProtocolName() + " " + protocolFieldsBean.getProtocolFileName(); doc.add(new TextField("content", fullSearchableText, Field.Store.NO)); writer.addDocument(doc); }
/** * Creates a Lucene document from an issue. * * @param issue * @return a Lucene document */ private Document createDocument(IssueModel issue) { Document doc = new Document(); doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.issue.name(), Store.YES, Field.Index.NOT_ANALYZED)); doc.add(new Field(FIELD_ISSUE, issue.id, Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_BRANCH, IssueUtils.GB_ISSUES, Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_DATE, DateTools.dateToString(issue.created, Resolution.MINUTE), Store.YES, Field.Index.NO)); doc.add(new Field(FIELD_AUTHOR, issue.reporter, Store.YES, Index.ANALYZED)); List<String> attachments = new ArrayList<String>(); for (Attachment attachment : issue.getAttachments()) { attachments.add(attachment.name.toLowerCase()); } doc.add(new Field(FIELD_ATTACHMENT, StringUtils.flattenStrings(attachments), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_SUMMARY, issue.summary, Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_CONTENT, issue.toString(), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_LABEL, StringUtils.flattenStrings(issue.getLabels()), Store.YES, Index.ANALYZED)); return doc; }
/** * Creates a Lucene document for a commit * * @param commit * @param tags * @return a Lucene document */ private Document createDocument(RevCommit commit, List<String> tags) { Document doc = new Document(); doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.commit.name(), Store.YES, Index.NOT_ANALYZED)); doc.add(new Field(FIELD_COMMIT, commit.getName(), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_DATE, DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE), Store.YES, Index.NO)); doc.add(new Field(FIELD_AUTHOR, getAuthor(commit), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_COMMITTER, getCommitter(commit), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_SUMMARY, commit.getShortMessage(), Store.YES, Index.ANALYZED)); doc.add(new Field(FIELD_CONTENT, commit.getFullMessage(), Store.YES, Index.ANALYZED)); if (!ArrayUtils.isEmpty(tags)) { doc.add(new Field(FIELD_TAG, StringUtils.flattenStrings(tags), Store.YES, Index.ANALYZED)); } return doc; }
/** * 获得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; }
@Override public void setDateResolution(CommonQueryParserConfiguration cqpC, CharSequence field, Resolution value) { assert (cqpC instanceof StandardQueryParser); StandardQueryParser qp = (StandardQueryParser) cqpC; qp.getDateResolutionMap().put(field, value); }
@Override public void setDateResolution(CommonQueryParserConfiguration cqpC, CharSequence field, Resolution value) { assert (cqpC instanceof QueryParser); QueryParser qp = (QueryParser) cqpC; qp.setDateResolution(field.toString(), value); }
/** * Creates a Lucene document for a commit * * @param commit * @param tags * @return a Lucene document */ private Document createDocument(RevCommit commit, List<String> tags) { Document doc = new Document(); doc.add(new Field(FIELD_OBJECT_TYPE, SearchObjectType.commit.name(), StringField.TYPE_STORED)); doc.add(new Field(FIELD_COMMIT, commit.getName(), TextField.TYPE_STORED)); doc.add(new Field(FIELD_DATE, DateTools.timeToString(commit.getCommitTime() * 1000L, Resolution.MINUTE), StringField.TYPE_STORED)); doc.add(new Field(FIELD_AUTHOR, getAuthor(commit), TextField.TYPE_STORED)); doc.add(new Field(FIELD_COMMITTER, getCommitter(commit), TextField.TYPE_STORED)); doc.add(new Field(FIELD_SUMMARY, commit.getShortMessage(), TextField.TYPE_STORED)); doc.add(new Field(FIELD_CONTENT, commit.getFullMessage(), TextField.TYPE_STORED)); if (!ArrayUtils.isEmpty(tags)) { doc.add(new Field(FIELD_TAG, StringUtils.flattenStrings(tags), TextField.TYPE_STORED)); } return doc; }
public void indexSampleSearchableFieldsBean(SampleSearchableFieldsBean sampleFieldsBean) throws IOException { IndexWriter writer = getIndexWriter(false); Document doc = new Document(); doc.add(new StringField("sampleId", sampleFieldsBean.getSampleId(), Field.Store.YES)); doc.add(new StringField("sampleName", sampleFieldsBean.getSampleName(), Field.Store.YES)); if(sampleFieldsBean.getSamplePocName()!=null) doc.add(new StringField("samplePocName", sampleFieldsBean.getSamplePocName(), Field.Store.YES)); if(sampleFieldsBean.getNanoEntityName()!=null) doc.add(new StringField("nanoEntityName", sampleFieldsBean.getNanoEntityName(), Field.Store.YES)); if(sampleFieldsBean.getNanoEntityDesc()!=null) doc.add(new StringField("nanoEntityDesc", sampleFieldsBean.getNanoEntityDesc(), Field.Store.YES)); if(sampleFieldsBean.getFuncEntityName()!=null) doc.add(new StringField("funcEntityName", sampleFieldsBean.getFuncEntityName(), Field.Store.YES)); if(sampleFieldsBean.getFunction()!=null) doc.add(new StringField("function", sampleFieldsBean.getFunction(), Field.Store.YES)); if(sampleFieldsBean.getCharacterization()!=null) doc.add(new StringField("characterization", sampleFieldsBean.getCharacterization(), Field.Store.YES)); if(sampleFieldsBean.getCreatedDate()!=null) try{ doc.add(new Field("createdDate", DateTools.stringToDate(DateTools.dateToString(sampleFieldsBean.getCreatedDate(),Resolution.SECOND)).toString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); }catch(ParseException e){ e.printStackTrace(); } if(sampleFieldsBean.getSampleKeywords().size()>0) for(int i = 0; i < sampleFieldsBean.getSampleKeywords().size();i++){ doc.add(new StringField("sampleKeywords", sampleFieldsBean.getSampleKeywords().get(i), Field.Store.YES)); } String fullSearchableText = sampleFieldsBean.getSampleName() + " " + sampleFieldsBean.getSamplePocName() + " " + sampleFieldsBean.getNanoEntityName() + " " + sampleFieldsBean.getNanoEntityDesc() + " " + sampleFieldsBean.getFuncEntityName() + " " + sampleFieldsBean.getFunction() + " " + sampleFieldsBean.getCharacterization(); String keywords = ""; for( int i = 0; i < sampleFieldsBean.getSampleKeywords().size(); i++){ String keyword = sampleFieldsBean.getSampleKeywords().get(i); keywords = keywords + " " + keyword; } fullSearchableText = fullSearchableText + " " + keywords; doc.add(new TextField("content", fullSearchableText, Field.Store.NO)); writer.addDocument(doc); }
/** * Initializes a new instance of Search with a query, * a set of facets (in {@link String} form with integer counts), an * analyzer and a custom sort. * @param qquery The query to be executed during the search. * @param ffacets The facets for which to search. * @param aanalyzer The analyzer with which to parse the * query. * @param ssort The custom sort order. * @throws ParseException A fatal exception occurred while * parsing the String query. */ public Search( final String qquery, final Map<String, Integer> ffacets, final Analyzer aanalyzer, final Sort ssort) throws ParseException { QueryParser parser = new SearchQueryParser( Lucene.LUCENE_VERSION, Lucene.DEFAULT_QUERY_FIELD, aanalyzer ); parser.setDateResolution(Resolution.HOUR); query = parser.parse(qquery); if (ffacets == null) { facets = null; } else { facets = new ArrayList<FacetRequest>(ffacets.size()); for (Entry<String, Integer> facet : ffacets.entrySet()) { final CategoryPath facetPath = new CategoryPath(facet.getKey(), '/'); final CountFacetRequest facetRequest = new CountFacetRequest(facetPath, facet.getValue()); facets.add(facetRequest); } } sort = ssort; }
public static String adjustIfDate(String texteLibre) { String result; try { Date date = new SimpleDateFormat("yyyy-MM-dd").parse(texteLibre.trim()); result = DateTools.dateToString(date, Resolution.DAY); } catch (Exception e) { result = texteLibre; } return result; }
public void addRecipe(String recipeName) throws SQLException, DBRecipe.RecipeNotFoundException, IOException { recipeName = recipeName.toLowerCase(Locale.GERMAN); if (checkIfRecipeExists(recipeName)) { removeRecipe(recipeName); } try (DBGetRecipe dbGetRecipe = new DBGetRecipe()) { Recipe recipe = dbGetRecipe.get(recipeName); int id = dbGetRecipe.getActiveIdfromRecipe(recipeName); String date = DateTools.dateToString(new Date(), Resolution.DAY); List<Step> steps = Steps.loadRecipeSteps(recipeName); StringBuilder stepText = new StringBuilder(); for (Step step : steps) { stepText.append(" ").append(step.getText()); } try (IndexWriter writer = new IndexWriter(index, createIndexWriterConfig())) { Document doc = new Document(); doc.add(new TextField("title", recipe.getName(), Field.Store.YES)); doc.add(new TextField("description", recipe.getDescription() == null ? "" : recipe .getDescription(), Field.Store.YES)); doc.add(new TextField("steps", stepText.toString(), Field.Store.YES)); doc.add(new IntField("version_id", id, Field.Store.YES)); doc.add(new TextField("date", date, Field.Store.YES)); writer.addDocument(doc); writer.commit(); logger.info("added " + recipeName + " to index"); } catch (CorruptIndexException | LockObtainFailedException e) { throw new IOException(e); } } }
public Document generateRecipeDoc(String recipeName) throws SQLException, DBRecipe.RecipeNotFoundException { try (DBGetRecipe dbGetRecipe = new DBGetRecipe()) { Recipe recipe = dbGetRecipe.get(recipeName); List<Step> steps = Steps.loadRecipeSteps(recipeName); StringBuilder stepText = new StringBuilder(); for (Step step : steps) { stepText.append(" ").append(step.getText()); } int id = dbGetRecipe.getActiveIdfromRecipe(recipeName); String date = DateTools.dateToString(new Date(), Resolution.DAY); Document doc = new Document(); doc.add(new TextField("title", recipe.getName(), Field.Store.YES)); doc.add(new TextField("description", recipe.getDescription() == null ? "" : recipe.getDescription(), Field.Store.YES)); doc.add(new TextField("steps", stepText.toString(), Field.Store.YES)); doc.add(new IntField("version_id", id, Field.Store.YES)); doc.add(new TextField("date", date, Field.Store.YES)); return doc; } }
@Override public void setDateResolution(QueryParser cqpC, CharSequence field, Resolution value) { assert (cqpC instanceof QueryParser); QueryParser qp = (QueryParser) cqpC; qp.setDateResolution(field.toString(), value); }
@Override public void setDateResolution(CommonQueryParserConfiguration cqpC, CharSequence field, Resolution value) { //no-op }
public void indexPublicationSearchableFieldsBean(PublicationSearchableFieldsBean pubFieldsBean) throws IOException { IndexWriter writer = getIndexWriter(false); Document doc = new Document(); doc.add(new StringField("publicationId", pubFieldsBean.getPublicationId(), Field.Store.YES)); if(pubFieldsBean.getPubTitle()!=null) doc.add(new StringField("pubTitle", pubFieldsBean.getPubTitle(), Field.Store.YES)); if(pubFieldsBean.getPubmedId()!=null) doc.add(new StringField("pubmedId", pubFieldsBean.getPubmedId(), Field.Store.YES)); if(pubFieldsBean.getDoiId()!=null) doc.add(new StringField("doiId", pubFieldsBean.getDoiId(), Field.Store.YES)); if(pubFieldsBean.getPubDesc()!=null) doc.add(new StringField("pubDesc", pubFieldsBean.getPubDesc(), Field.Store.YES)); if(pubFieldsBean.getCreatedDate()!=null) try{ doc.add(new Field("createdDate", DateTools.stringToDate(DateTools.dateToString(pubFieldsBean.getCreatedDate(),Resolution.SECOND)).toString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); }catch(ParseException e){ e.printStackTrace(); } String keywords = ""; String fullSearchableText = pubFieldsBean.getSampleName() + " " + pubFieldsBean.getPubDesc() + " " + pubFieldsBean.getPubTitle() + " " + pubFieldsBean.getPubmedId() + " " + pubFieldsBean.getDoiId(); if(pubFieldsBean.getPubKeywords()!=null){ for(int i = 0; i < pubFieldsBean.getPubKeywords().size(); i++){ String keyword = pubFieldsBean.getPubKeywords().get(i); keywords = keywords + " " + keyword; } } fullSearchableText = fullSearchableText + " " + keywords; String authors = ""; if(pubFieldsBean.getAuthors()!=null){ for ( int j = 0; j < pubFieldsBean.getAuthors().size(); j++){ String author = pubFieldsBean.getAuthors().get(j); authors = authors + " " + author; } } fullSearchableText = fullSearchableText + " " + authors; String sampleNames = ""; if(pubFieldsBean.getSampleName()!=null){ for(int k = 0; k < pubFieldsBean.getSampleName().size(); k++){ String sampleName = pubFieldsBean.getSampleName().get(k); sampleNames = sampleNames + " " + sampleName; } } fullSearchableText = fullSearchableText + " " + sampleNames; doc.add(new TextField("content", fullSearchableText, Field.Store.NO)); writer.addDocument(doc); }
/** * Sets the default {@link Resolution} used for certain field when * no {@link Resolution} is defined for this field. * * @param dateResolution the default {@link Resolution} */ public void setDateResolution(DateTools.Resolution dateResolution);