@Override public void processMultiple(Collection<IndexMaster> indexes, ContentRequest req, XMLWriter xml) throws BerliozException, IOException { String facets = req.getParameter("facets", ""); int maxNumber = req.getIntParameter("max-facets", 20); // get first catalog as they should all have the same one SearchQuery query = buildQuery(req, indexes.iterator().next().getCatalog()); if (query == null) { xml.emptyElement("index-search"); return; } SearchPaging paging = buildPaging(req); ArrayList<Index> theIndexes = new ArrayList<Index>(); for (IndexMaster index : indexes) { theIndexes.add(index.getIndex()); } try { SearchResults results = LuceneIndexQueries.query(theIndexes, query, paging); List<FieldFacet> facetsList = Facets.getFacets( Arrays.asList(facets.split(",")), maxNumber, query.toQuery(), theIndexes); outputResults(query, results, facetsList, xml); } catch (IndexException ex) { LOGGER.warn( "Fail to retrieve search result using query: {}", (Object) query.toString(), (Object) ex); } }
private SearchQuery buildQuery(ContentRequest req, String catalog) { String field = req.getParameter("field", "fulltext"); String typed = req.getParameter("term"); if (typed == null) return null; // compute parameters List<SearchParameter> params = new ArrayList<SearchParameter>(); String with = req.getParameter("with"); if (with != null) { for (String w : with.split(",")) { String[] both = w.split(":"); if (both.length == 2) { LOGGER.debug("Adding facet with field {} and term {}", both[0], both[1]); params.add(new TermParameter(both[0], both[1])); } else { LOGGER.warn("Ignoring invalid facet {}", w); } } } // check if we should tokenize Catalog theCatalog = Catalogs.getCatalog(catalog); boolean tokenize = theCatalog != null && theCatalog.isTokenized(field); BasicQuery<?> query; if (tokenize) { query = BasicQuery.newBasicQuery(new PhraseParameter(field, typed), params); LOGGER.debug("Search in field {} for phrase {}", field, typed); } else { query = BasicQuery.newBasicQuery(new TermParameter(field, typed), params); LOGGER.debug("Search in field {} for term {}", field, typed); } Sort sort = buildSort(req); if (sort != null) query.setSort(sort); return query; }
private SearchPaging buildPaging(ContentRequest req) { SearchPaging paging = new SearchPaging(); int page = req.getIntParameter("page", 1); int results = req.getIntParameter("results", 100); paging.setPage(page); paging.setHitsPerPage(results); return paging; }
@Override public void processSingle(IndexMaster index, ContentRequest req, XMLWriter xml) throws BerliozException, IOException { String facets = req.getParameter("facets", ""); int maxNumber = req.getIntParameter("max-facets", 20); SearchQuery query = buildQuery(req, index.getCatalog()); if (query == null) { xml.emptyElement("index-search"); return; } SearchPaging paging = buildPaging(req); try { SearchResults results = index.query(query, paging); List<FieldFacet> facetsList = Facets.getFacets( Arrays.asList(facets.split(",")), maxNumber, query.toQuery(), index.getIndex()); outputResults(query, results, facetsList, xml); } catch (IndexException ex) { LOGGER.warn( "Fail to retrieve search result using query: {}", (Object) query.toString(), (Object) ex); } }
private Sort buildSort(ContentRequest req) { // field name String field = req.getParameter("sort"); if (field == null) return null; // reverse order boolean reverse = field.startsWith("-"); if (reverse) field = field.substring(1); // sort type SortField sfield = null; String type = req.getParameter("sort-type", "score"); if ("int".equalsIgnoreCase(type)) sfield = new SortedNumericSortField(field, Type.INT, reverse); else if ("double".equalsIgnoreCase(type)) sfield = new SortedNumericSortField(field, Type.DOUBLE, reverse); else if ("float".equalsIgnoreCase(type)) sfield = new SortedNumericSortField(field, Type.FLOAT, reverse); else if ("long".equalsIgnoreCase(type)) sfield = new SortedNumericSortField(field, Type.LONG, reverse); else if ("document".equalsIgnoreCase(type)) sfield = SortField.FIELD_DOC; else if ("set".equalsIgnoreCase(type)) sfield = new SortedSetSortField(field, reverse); else if ("string".equalsIgnoreCase(type)) sfield = new SortField(field, Type.STRING, reverse); else sfield = SortField.FIELD_SCORE; // build sort field return new Sort(sfield); }