+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.api.service.ITaxonService#findByFullText(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.taxon.Classification, java.util.List, boolean, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)\r
+ */\r
+ @Override\r
+ public Pager<SearchResult<TaxonBase>> findByFullText(\r
+ Class<? extends TaxonBase> clazz, String queryString,\r
+ Classification classification, List<Language> languages,\r
+ boolean highlightFragments, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) throws CorruptIndexException, IOException, ParseException {\r
+\r
+\r
+ LuceneSearch luceneSearch = prepareFindByFullTextSearch(clazz, queryString, classification, languages, highlightFragments, null);\r
+\r
+ // --- execute search\r
+ TopGroupsWithMaxScore topDocsResultSet = luceneSearch.executeSearch(pageSize, pageNumber);\r
+\r
+ Map<CdmBaseType, String> idFieldMap = new HashMap<CdmBaseType, String>();\r
+ idFieldMap.put(CdmBaseType.TAXON, "id");\r
+\r
+ // --- initialize taxa, thighlight matches ....\r
+ ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(luceneSearch, luceneSearch.getQuery());\r
+ List<SearchResult<TaxonBase>> searchResults = searchResultBuilder.createResultSet(\r
+ topDocsResultSet, luceneSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);\r
+\r
+ int totalHits = topDocsResultSet != null ? topDocsResultSet.topGroups.totalGroupCount : 0;\r
+ return new DefaultPagerImpl<SearchResult<TaxonBase>>(pageNumber, totalHits, pageSize, searchResults);\r
+ }\r
+\r
+ @Override\r
+ public Pager<SearchResult<TaxonBase>> findByDistribution(List<NamedArea> areaFilter, List<PresenceAbsenceTermBase<?>> statusFilter,\r
+ Classification classification,\r
+ Integer pageSize, Integer pageNumber,\r
+ List<OrderHint> orderHints, List<String> propertyPaths) throws IOException, ParseException {\r
+\r
+ LuceneSearch luceneSearch = prepareByDistributionSearch(areaFilter, statusFilter, classification);\r
+\r
+ // --- execute search\r
+ TopGroupsWithMaxScore topDocsResultSet = luceneSearch.executeSearch(pageSize, pageNumber);\r
+\r
+ Map<CdmBaseType, String> idFieldMap = new HashMap<CdmBaseType, String>();\r
+ idFieldMap.put(CdmBaseType.TAXON, "id");\r
+\r
+ // --- initialize taxa, thighlight matches ....\r
+ ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(luceneSearch, luceneSearch.getQuery());\r
+ List<SearchResult<TaxonBase>> searchResults = searchResultBuilder.createResultSet(\r
+ topDocsResultSet, luceneSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);\r
+\r
+ int totalHits = topDocsResultSet != null ? topDocsResultSet.topGroups.totalGroupCount : 0;\r
+ return new DefaultPagerImpl<SearchResult<TaxonBase>>(pageNumber, totalHits, pageSize, searchResults);\r
+ }\r
+\r
+ /**\r
+ * @param clazz\r
+ * @param queryString\r
+ * @param classification\r
+ * @param languages\r
+ * @param highlightFragments\r
+ * @param sortFields TODO\r
+ * @param directorySelectClass\r
+ * @return\r
+ */\r
+ protected LuceneSearch prepareFindByFullTextSearch(Class<? extends CdmBase> clazz, String queryString, Classification classification, List<Language> languages,\r
+ boolean highlightFragments, SortField[] sortFields) {\r
+ BooleanQuery finalQuery = new BooleanQuery();\r
+ BooleanQuery textQuery = new BooleanQuery();\r
+\r
+ LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, GroupByTaxonClassBridge.GROUPBY_TAXON_FIELD, TaxonBase.class);\r
+ QueryFactory taxonBaseQueryFactory = luceneIndexToolProvider.newQueryFactoryFor(TaxonBase.class);\r
+\r
+ if(sortFields == null){\r
+ sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};\r
+ }\r
+ luceneSearch.setSortFields(sortFields);\r
+\r
+ // ---- search criteria\r
+ luceneSearch.setCdmTypRestriction(clazz);\r
+\r
+ textQuery.add(taxonBaseQueryFactory.newTermQuery("titleCache", queryString), Occur.SHOULD);\r
+ textQuery.add(taxonBaseQueryFactory.newDefinedTermQuery("name.rank", queryString, languages), Occur.SHOULD);\r
+\r
+ finalQuery.add(textQuery, Occur.MUST);\r
+\r
+ if(classification != null){\r
+ finalQuery.add(taxonBaseQueryFactory.newEntityIdQuery("taxonNodes.classification.id", classification), Occur.MUST);\r
+ }\r
+ luceneSearch.setQuery(finalQuery);\r
+\r
+ if(highlightFragments){\r
+ luceneSearch.setHighlightFields(taxonBaseQueryFactory.getTextFieldNamesAsArray());\r
+ }\r
+ return luceneSearch;\r
+ }\r
+\r
+ /**\r
+ * Uses org.apache.lucene.search.join.JoinUtil for query time joining, alternatively\r
+ * the BlockJoinQuery could be used. The latter might be more memory save but has the\r
+ * drawback of requiring to do the join an indexing time.\r
+ * see http://dev.e-taxonomy.eu/trac/wiki/LuceneNotes#JoinsinLucene for more information on this.\r
+ *\r
+ * Joins TaxonRelationShip with Taxon depending on the direction of the given edge:\r
+ * <ul>\r
+ * <li>direct, everted: {@link Direction.relatedTo}: TaxonRelationShip.relatedTo.id --> Taxon.id </li>\r
+ * <li>inverse: {@link Direction.relatedFrom}: TaxonRelationShip.relatedFrom.id --> Taxon.id </li>\r
+ * <ul>\r
+ * @param queryString\r
+ * @param classification\r
+ * @param languages\r
+ * @param highlightFragments\r
+ * @param sortFields TODO\r
+ *\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ protected LuceneSearch prepareFindByTaxonRelationFullTextSearch(TaxonRelationshipEdge edge, String queryString, Classification classification, List<Language> languages,\r
+ boolean highlightFragments, SortField[] sortFields) throws IOException {\r
+\r
+ String fromField;\r
+ String queryTermField;\r
+ String toField = "id"; // TaxonBase.uuid\r
+\r
+ if(edge.isBidirectional()){\r
+ throw new RuntimeException("Bidirectional joining not supported!");\r
+ }\r
+ if(edge.isEvers()){\r
+ fromField = "relatedFrom.id";\r
+ queryTermField = "relatedFrom.titleCache";\r
+ } else if(edge.isInvers()) {\r
+ fromField = "relatedTo.id";\r
+ queryTermField = "relatedTo.titleCache";\r
+ } else {\r
+ throw new RuntimeException("Invalid direction: " + edge.getDirections());\r
+ }\r
+\r
+ BooleanQuery finalQuery = new BooleanQuery();\r
+\r
+ LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, GroupByTaxonClassBridge.GROUPBY_TAXON_FIELD, TaxonBase.class);\r
+ QueryFactory taxonBaseQueryFactory = luceneIndexToolProvider.newQueryFactoryFor(TaxonBase.class);\r
+\r
+ BooleanQuery joinFromQuery = new BooleanQuery();\r
+ joinFromQuery.add(taxonBaseQueryFactory.newTermQuery(queryTermField, queryString), Occur.MUST);\r
+ joinFromQuery.add(taxonBaseQueryFactory.newEntityIdQuery("type.id", edge.getTaxonRelationshipType()), Occur.MUST);\r
+ Query joinQuery = taxonBaseQueryFactory.newJoinQuery(fromField, toField, joinFromQuery, TaxonRelationship.class);\r
+\r
+ if(sortFields == null){\r
+ sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};\r
+ }\r
+ luceneSearch.setSortFields(sortFields);\r
+\r
+ finalQuery.add(joinQuery, Occur.MUST);\r
+\r
+ if(classification != null){\r
+ finalQuery.add(taxonBaseQueryFactory.newEntityIdQuery("taxonNodes.classification.id", classification), Occur.MUST);\r
+ }\r
+ luceneSearch.setQuery(finalQuery);\r
+\r
+ if(highlightFragments){\r
+ luceneSearch.setHighlightFields(taxonBaseQueryFactory.getTextFieldNamesAsArray());\r
+ }\r
+ return luceneSearch;\r
+ }\r
+\r
+\r
+\r
+\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.api.service.ITaxonService#findTaxaAndNamesByFullText(java.util.EnumSet, java.lang.String, eu.etaxonomy.cdm.model.taxon.Classification, java.util.Set, java.util.List, boolean, java.lang.Integer, java.lang.Integer, java.util.List, java.util.Map)\r
+ */\r
+ @Override\r
+ public Pager<SearchResult<TaxonBase>> findTaxaAndNamesByFullText(\r
+ EnumSet<TaxaAndNamesSearchMode> searchModes, String queryString, Classification classification,\r
+ Set<NamedArea> namedAreas, Set<PresenceAbsenceTermBase<?>> distributionStatus, List<Language> languages,\r
+ boolean highlightFragments, Integer pageSize,\r
+ Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)\r
+ throws CorruptIndexException, IOException, ParseException, LuceneMultiSearchException {\r
+\r
+ // FIXME: allow taxonomic ordering\r
+ // hql equivalent: order by t.name.genusOrUninomial, case when t.name.specificEpithet like '\"%\"' then 1 else 0 end, t.name.specificEpithet, t.name.rank desc, t.name.nameCache";\r
+ // this require building a special sort column by a special classBridge\r
+ if(highlightFragments){\r
+ logger.warn("findTaxaAndNamesByFullText() : fragment highlighting is " +\r
+ "currently not fully supported by this method and thus " +\r
+ "may not work with common names and misapplied names.");\r
+ }\r
+\r
+ // convert sets to lists\r
+ List<NamedArea> namedAreaList = null;\r
+ List<PresenceAbsenceTermBase<?>>distributionStatusList = null;\r
+ if(namedAreas != null){\r
+ namedAreaList = new ArrayList<NamedArea>(namedAreas.size());\r
+ namedAreaList.addAll(namedAreas);\r
+ }\r
+ if(distributionStatus != null){\r
+ distributionStatusList = new ArrayList<PresenceAbsenceTermBase<?>>(distributionStatus.size());\r
+ distributionStatusList.addAll(distributionStatus);\r
+ }\r
+\r
+ // set default if parameter is null\r
+ if(searchModes == null){\r
+ searchModes = EnumSet.of(TaxaAndNamesSearchMode.doTaxa);\r
+ }\r
+\r
+ // set sort order and thus override any sort orders which may have been\r
+ // defindes by prepare*Search methods\r
+ if(orderHints == null){\r
+ orderHints = OrderHint.NOMENCLATURAL_SORT_ORDER;\r
+ }\r
+ SortField[] sortFields = new SortField[orderHints.size()];\r
+ int i = 0;\r
+ for(OrderHint oh : orderHints){\r
+ sortFields[i++] = oh.toSortField();\r
+ }\r
+// SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("id", SortField.STRING, false)};\r
+// SortField[] sortFields = new SortField[]{new SortField(NomenclaturalSortOrderBrigde.NAME_SORT_FIELD_NAME, SortField.STRING, false)};\r
+\r
+\r
+ boolean addDistributionFilter = namedAreas != null && namedAreas.size() > 0;\r
+\r
+ List<LuceneSearch> luceneSearches = new ArrayList<LuceneSearch>();\r
+ Map<CdmBaseType, String> idFieldMap = new HashMap<CdmBaseType, String>();\r
+\r
+ /*\r
+ ======== filtering by distribution , HOWTO ========\r
+\r
+ - http://www.javaranch.com/journal/2009/02/filtering-a-lucene-search.html\r
+ - http://stackoverflow.com/questions/17709256/lucene-solr-using-complex-filters -> QueryWrapperFilter\r
+ add Filter to search as http://lucene.apache.org/core/3_6_0/api/all/org/apache/lucene/search/Filter.html\r
+ which will be put into a FilteredQuersy in the end ?\r
+\r
+\r
+ 3. how does it work in spatial?\r
+ see\r
+ - http://www.nsshutdown.com/projects/lucene/whitepaper/locallucene_v2.html\r
+ - http://www.infoq.com/articles/LuceneSpatialSupport\r
+ - http://www.mhaller.de/archives/156-Spatial-search-with-Lucene.html\r
+ ------------------------------------------------------------------------\r
+\r
+ filter strategies:\r
+ A) use a separate distribution filter per index sub-query/search:\r
+ - byTaxonSyonym (query TaxaonBase):\r
+ use a join area filter (Distribution -> TaxonBase)\r
+ - byCommonName (query DescriptionElementBase): use an area filter on\r
+ DescriptionElementBase !!! PROBLEM !!!\r
+ This cannot work since the distributions are different entities than the\r
+ common names and thus these are different lucene documents.\r
+ - byMisaplliedNames (join query TaxonRelationship -> TaxaonBase):\r
+ use a join area filter (Distribution -> TaxonBase)\r
+\r
+ B) use a common distribution filter for all index sub-query/searches:\r
+ - use a common join area filter (Distribution -> TaxonBase)\r
+ - also implement the byCommonName as join query (CommonName -> TaxonBase)\r
+ PROBLEM in this case: we are losing the fragment highlighting for the\r
+ common names, since the returned documents are always TaxonBases\r
+ */\r
+\r
+ /* The QueryFactory for creating filter queries on Distributions should\r
+ * The query factory used for the common names query cannot be reused\r
+ * for this case, since we want to only record the text fields which are\r
+ * actually used in the primary query\r
+ */\r
+ QueryFactory distributionFilterQueryFactory = luceneIndexToolProvider.newQueryFactoryFor(Distribution.class);\r
+\r
+ BooleanFilter multiIndexByAreaFilter = new BooleanFilter();\r
+\r
+\r
+ // search for taxa or synonyms\r
+ if(searchModes.contains(TaxaAndNamesSearchMode.doTaxa) || searchModes.contains(TaxaAndNamesSearchMode.doSynonyms)) {\r
+ Class taxonBaseSubclass = TaxonBase.class;\r
+ if(searchModes.contains(TaxaAndNamesSearchMode.doTaxa) && !searchModes.contains(TaxaAndNamesSearchMode.doSynonyms)){\r
+ taxonBaseSubclass = Taxon.class;\r
+ } else if (!searchModes.contains(TaxaAndNamesSearchMode.doTaxa) && searchModes.contains(TaxaAndNamesSearchMode.doSynonyms)) {\r
+ taxonBaseSubclass = Synonym.class;\r
+ }\r
+ luceneSearches.add(prepareFindByFullTextSearch(taxonBaseSubclass, queryString, classification, languages, highlightFragments, sortFields));\r
+ idFieldMap.put(CdmBaseType.TAXON, "id");\r
+ /* A) does not work!!!!\r
+ if(addDistributionFilter){\r
+ // in this case we need a filter which uses a join query\r
+ // to get the TaxonBase documents for the DescriptionElementBase documents\r
+ // which are matching the areas in question\r
+ Query taxonAreaJoinQuery = createByDistributionJoinQuery(\r
+ namedAreaList,\r
+ distributionStatusList,\r
+ distributionFilterQueryFactory\r
+ );\r
+ multiIndexByAreaFilter.add(new QueryWrapperFilter(taxonAreaJoinQuery), Occur.SHOULD);\r
+ }\r
+ */\r
+ if(addDistributionFilter && searchModes.contains(TaxaAndNamesSearchMode.doSynonyms)){\r
+ // add additional area filter for synonyms\r
+ String fromField = "inDescription.taxon.id"; // in DescriptionElementBase index\r
+ String toField = "accTaxon.id"; // id in TaxonBase index\r
+\r
+ BooleanQuery byDistributionQuery = createByDistributionQuery(namedAreaList, distributionStatusList, distributionFilterQueryFactory);\r
+\r
+ Query taxonAreaJoinQuery = distributionFilterQueryFactory.newJoinQuery(fromField, toField, byDistributionQuery, Distribution.class);\r
+ multiIndexByAreaFilter.add(new QueryWrapperFilter(taxonAreaJoinQuery), Occur.SHOULD);\r
+\r
+ }\r
+ }\r
+\r
+ // search by CommonTaxonName\r
+ if(searchModes.contains(TaxaAndNamesSearchMode.doTaxaByCommonNames)) {\r
+ // B)\r
+ QueryFactory descriptionElementQueryFactory = luceneIndexToolProvider.newQueryFactoryFor(DescriptionElementBase.class);\r
+ Query byCommonNameJoinQuery = descriptionElementQueryFactory.newJoinQuery(\r
+ "inDescription.taxon.id",\r
+ "id",\r
+ QueryFactory.addTypeRestriction(\r
+ createByDescriptionElementFullTextQuery(queryString, classification, null, languages, descriptionElementQueryFactory)\r
+ , CommonTaxonName.class\r
+ ),\r
+ CommonTaxonName.class);\r
+ logger.debug("byCommonNameJoinQuery: " + byCommonNameJoinQuery.toString());\r
+ LuceneSearch byCommonNameSearch = new LuceneSearch(luceneIndexToolProvider, GroupByTaxonClassBridge.GROUPBY_TAXON_FIELD, Taxon.class);\r
+ byCommonNameSearch.setCdmTypRestriction(Taxon.class);\r
+ byCommonNameSearch.setQuery(byCommonNameJoinQuery);\r
+ byCommonNameSearch.setSortFields(sortFields);\r
+ idFieldMap.put(CdmBaseType.TAXON, "id");\r
+\r
+ luceneSearches.add(byCommonNameSearch);\r
+\r
+ /* A) does not work!!!!\r
+ luceneSearches.add(\r
+ prepareByDescriptionElementFullTextSearch(CommonTaxonName.class,\r
+ queryString, classification, null, languages, highlightFragments)\r
+ );\r
+ idFieldMap.put(CdmBaseType.DESCRIPTION_ELEMENT, "inDescription.taxon.id");\r
+ if(addDistributionFilter){\r
+ // in this case we are able to use DescriptionElementBase documents\r
+ // which are matching the areas in question directly\r
+ BooleanQuery byDistributionQuery = createByDistributionQuery(\r
+ namedAreaList,\r
+ distributionStatusList,\r
+ distributionFilterQueryFactory\r
+ );\r
+ multiIndexByAreaFilter.add(new QueryWrapperFilter(byDistributionQuery), Occur.SHOULD);\r
+ } */\r
+ }\r
+\r
+ // search by misapplied names\r
+ if(searchModes.contains(TaxaAndNamesSearchMode.doMisappliedNames)) {\r
+ // NOTE:\r
+ // prepareFindByTaxonRelationFullTextSearch() is making use of JoinUtil.createJoinQuery()\r
+ // which allows doing query time joins\r
+ // finds the misapplied name (Taxon B) which is an misapplication for\r
+ // a related Taxon A.\r
+ //\r
+ luceneSearches.add(prepareFindByTaxonRelationFullTextSearch(\r
+ new TaxonRelationshipEdge(TaxonRelationshipType.MISAPPLIED_NAME_FOR(), Direction.relatedTo),\r
+ queryString, classification, languages, highlightFragments, sortFields));\r
+ idFieldMap.put(CdmBaseType.TAXON, "id");\r
+\r
+ if(addDistributionFilter){\r
+ String fromField = "inDescription.taxon.id"; // in DescriptionElementBase index\r
+\r
+ /*\r
+ * Here i was facing wired and nasty bug which took me bugging be really for hours until I found this solution.\r
+ * Maybe this is a but in java itself java.\r
+ *\r
+ * When the string toField is constructed by using the expression TaxonRelationshipType.MISAPPLIED_NAME_FOR().getUuid().toString()\r
+ * directly:\r
+ *\r
+ * String toField = "relation." + TaxonRelationshipType.MISAPPLIED_NAME_FOR().getUuid().toString() +".to.id";\r
+ *\r
+ * The byDistributionQuery fails, however when the uuid is first stored in another string variable the query\r
+ * will execute as expected:\r
+ *\r
+ * String misappliedNameForUuid = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getUuid().toString();\r
+ * String toField = "relation." + misappliedNameForUuid +".to.id";\r
+ *\r
+ * Comparing both strings by the String.equals method returns true, so both String are identical.\r
+ *\r
+ * The bug occurs when running eu.etaxonomy.cdm.api.service.TaxonServiceSearchTest in eclipse and in maven and seems to to be\r
+ * dependent from a specific jvm (openjdk6 6b27-1.12.6-1ubuntu0.13.04.2, openjdk7 7u25-2.3.10-1ubuntu0.13.04.2, oracle jdk1.7.0_25 tested)\r
+ * The bug is persistent after a reboot of the development computer.\r
+ */\r
+// String misappliedNameForUuid = TaxonRelationshipType.MISAPPLIED_NAME_FOR().getUuid().toString();\r
+// String toField = "relation." + misappliedNameForUuid +".to.id";\r
+ String toField = "relation.1ed87175-59dd-437e-959e-0d71583d8417.to.id";\r
+// System.out.println("relation.1ed87175-59dd-437e-959e-0d71583d8417.to.id".equals("relation." + misappliedNameForUuid +".to.id") ? " > identical" : " > different");\r
+// System.out.println("relation.1ed87175-59dd-437e-959e-0d71583d8417.to.id".equals("relation." + TaxonRelationshipType.MISAPPLIED_NAME_FOR().getUuid().toString() +".to.id") ? " > identical" : " > different");\r
+\r
+ BooleanQuery byDistributionQuery = createByDistributionQuery(namedAreaList, distributionStatusList, distributionFilterQueryFactory);\r
+ Query taxonAreaJoinQuery = distributionFilterQueryFactory.newJoinQuery(fromField, toField, byDistributionQuery, Distribution.class);\r
+ QueryWrapperFilter filter = new QueryWrapperFilter(taxonAreaJoinQuery);\r
+\r
+// debug code for bug described above\r
+ DocIdSet filterMatchSet = filter.getDocIdSet(luceneIndexToolProvider.getIndexReaderFor(Taxon.class));\r
+// System.err.println(DocIdBitSetPrinter.docsAsString(filterMatchSet, 100));\r
+\r
+ multiIndexByAreaFilter.add(filter, Occur.SHOULD);\r
+ }\r
+ }\r
+\r
+ LuceneMultiSearch multiSearch = new LuceneMultiSearch(luceneIndexToolProvider,\r
+ luceneSearches.toArray(new LuceneSearch[luceneSearches.size()]));\r
+\r
+\r
+ if(addDistributionFilter){\r
+\r
+ // B)\r
+ // in this case we need a filter which uses a join query\r
+ // to get the TaxonBase documents for the DescriptionElementBase documents\r
+ // which are matching the areas in question\r
+ //\r
+ // for toTaxa, doByCommonName\r
+ Query taxonAreaJoinQuery = createByDistributionJoinQuery(\r
+ namedAreaList,\r
+ distributionStatusList,\r
+ distributionFilterQueryFactory\r
+ );\r
+ multiIndexByAreaFilter.add(new QueryWrapperFilter(taxonAreaJoinQuery), Occur.SHOULD);\r
+ }\r
+\r
+ if (addDistributionFilter){\r
+ multiSearch.setFilter(multiIndexByAreaFilter);\r
+ }\r
+\r
+\r
+ // --- execute search\r
+ TopGroupsWithMaxScore topDocsResultSet = multiSearch.executeSearch(pageSize, pageNumber);\r
+\r
+ // --- initialize taxa, highlight matches ....\r
+ ISearchResultBuilder searchResultBuilder = new SearchResultBuilder(multiSearch, multiSearch.getQuery());\r
+\r
+\r
+ List<SearchResult<TaxonBase>> searchResults = searchResultBuilder.createResultSet(\r
+ topDocsResultSet, multiSearch.getHighlightFields(), dao, idFieldMap, propertyPaths);\r
+\r
+ int totalHits = topDocsResultSet != null ? topDocsResultSet.topGroups.totalGroupCount : 0;\r
+ return new DefaultPagerImpl<SearchResult<TaxonBase>>(pageNumber, totalHits, pageSize, searchResults);\r
+ }\r
+\r
+ /**\r
+ * @param namedAreaList at least one area must be in the list\r
+ * @param distributionStatusList optional\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ protected Query createByDistributionJoinQuery(\r
+ List<NamedArea> namedAreaList,\r
+ List<PresenceAbsenceTermBase<?>> distributionStatusList,\r
+ QueryFactory queryFactory\r
+ ) throws IOException {\r
+\r
+ String fromField = "inDescription.taxon.id"; // in DescriptionElementBase index\r
+ String toField = "id"; // id in TaxonBase index\r
+\r
+ BooleanQuery byDistributionQuery = createByDistributionQuery(namedAreaList, distributionStatusList, queryFactory);\r
+\r
+ Query taxonAreaJoinQuery = queryFactory.newJoinQuery(fromField, toField, byDistributionQuery, Distribution.class);\r
+\r
+ return taxonAreaJoinQuery;\r
+ }\r
+\r
+ /**\r
+ * @param namedAreaList\r
+ * @param distributionStatusList\r
+ * @param queryFactory\r
+ * @return\r
+ */\r
+ private BooleanQuery createByDistributionQuery(List<NamedArea> namedAreaList,\r
+ List<PresenceAbsenceTermBase<?>> distributionStatusList, QueryFactory queryFactory) {\r
+ BooleanQuery areaQuery = new BooleanQuery();\r
+ // area field from Distribution\r
+ areaQuery.add(queryFactory.newEntityIdsQuery("area.id", namedAreaList), Occur.MUST);\r
+\r
+ // status field from Distribution\r
+ if(distributionStatusList != null && distributionStatusList.size() > 0){\r
+ areaQuery.add(queryFactory.newEntityIdsQuery("status.id", distributionStatusList), Occur.MUST);\r
+ }\r
+\r
+ logger.debug("createByDistributionQuery() query: " + areaQuery.toString());\r
+ return areaQuery;\r
+ }\r
+\r
+ /**\r
+ * This method has been primarily created for testing the area join query but might\r
+ * also be useful in other situations\r
+ *\r
+ * @param namedAreaList\r
+ * @param distributionStatusList\r
+ * @param classification\r
+ * @param highlightFragments\r
+ * @return\r
+ * @throws IOException\r
+ */\r
+ protected LuceneSearch prepareByDistributionSearch(\r
+ List<NamedArea> namedAreaList, List<PresenceAbsenceTermBase<?>> distributionStatusList,\r
+ Classification classification) throws IOException {\r
+\r
+ BooleanQuery finalQuery = new BooleanQuery();\r
+\r
+ LuceneSearch luceneSearch = new LuceneSearch(luceneIndexToolProvider, GroupByTaxonClassBridge.GROUPBY_TAXON_FIELD, Taxon.class);\r
+\r
+ // FIXME is this query factory using the wrong type?\r
+ QueryFactory taxonQueryFactory = luceneIndexToolProvider.newQueryFactoryFor(Taxon.class);\r
+\r
+ SortField[] sortFields = new SortField[]{SortField.FIELD_SCORE, new SortField("titleCache__sort", SortField.STRING, false)};\r
+ luceneSearch.setSortFields(sortFields);\r
+\r
+\r
+ Query byAreaQuery = createByDistributionJoinQuery(namedAreaList, distributionStatusList, taxonQueryFactory);\r
+\r
+ finalQuery.add(byAreaQuery, Occur.MUST);\r
+\r
+ if(classification != null){\r
+ finalQuery.add(taxonQueryFactory.newEntityIdQuery("taxonNodes.classification.id", classification), Occur.MUST);\r
+ }\r
+\r
+ logger.info("prepareByAreaSearch() query: " + finalQuery.toString());\r
+ luceneSearch.setQuery(finalQuery);\r
+\r
+ return luceneSearch;\r
+ }\r
+\r
+\r
+\r
+ /* (non-Javadoc)\r
+ * @see eu.etaxonomy.cdm.api.service.ITaxonService#findByDescriptionElementFullText(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.taxon.Classification, java.util.List, java.util.List, boolean, java.lang.Integer, java.lang.Integer, java.util.List, java.util.List)\r
+ */\r