Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package dhbw.clippinggorilla.external.solr; import dhbw.clippinggorilla.objects.interestprofile.InterestProfile; import dhbw.clippinggorilla.objects.article.Article; import dhbw.clippinggorilla.objects.clipping.Clipping; import dhbw.clippinggorilla.objects.group.GroupInterestProfileUtils; import dhbw.clippinggorilla.objects.group.GroupUtils; import dhbw.clippinggorilla.objects.interestprofile.InterestProfileUtils; import dhbw.clippinggorilla.objects.source.Source; import dhbw.clippinggorilla.objects.user.User; import dhbw.clippinggorilla.objects.user.UserUtils; import dhbw.clippinggorilla.utilities.log.Log; import java.io.IOException; import java.time.Clock; import java.time.LocalDateTime; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException; import org.apache.solr.client.solrj.impl.XMLResponseParser; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrInputDocument; /** * * @author frank */ public class SOLR { public static HttpSolrClient client = null; /** * Sets the url to the solr Server. Example: http://localhost:8983/solr/ */ public static void setServer() { String url = "http://gvtr.de:8983/solr/news"; HttpSolrClient.Builder clientBuilder = new HttpSolrClient.Builder(url); client = clientBuilder.build(); client.setParser(new XMLResponseParser()); } /** * adds documents to solr * * @param documents */ public static void addDocument(SolrInputDocument... documents) { Collection<SolrInputDocument> docs = new ArrayList<>(); if (client == null) { setServer(); } docs.addAll(Arrays.asList(documents)); try { client.add(docs); client.commit(); } catch (SolrServerException | IOException e) { Log.error("Solr: Can't add or commit", e); } } /** * adds one bean to solr * * @param obj */ public static void addBean(Object obj) { if (client == null) { setServer(); } try { client.addBean(obj); } catch (IOException | SolrServerException | RemoteSolrException e) { Log.error("Solr: Can't add or commit", e); } } /** * adds list of beans to solr * * @param beans */ public static void addBeans(List<?> beans) { if (client == null) { setServer(); } List<?> subBeans; for (int i = 0; i < beans.size(); i += 25) { if (beans.size() >= i + 25) { subBeans = beans.subList(i, i + 25); } else { subBeans = beans.subList(i, beans.size()); } try { client.addBeans(subBeans); } catch (SolrServerException | IOException | RemoteSolrException e) { Log.error("Solr: Can't add or commit", e); } } } public static Article getArticle(int id) { if (client == null) { setServer(); } String q = "id:\"" + Integer.toString(id) + "\""; SolrQuery query = new SolrQuery(); query.setQuery(q); try { QueryResponse queryResponse = client.query(query); List<Article> beans = queryResponse.getBeans(Article.class); if (!beans.isEmpty()) { return beans.get(0); } } catch (SolrServerException | IOException e) { Log.error("Solr: Can't Query", e); } return null; } public static LinkedHashSet<Article> getArticlesFromSolr(Set<String> includedTagsSet, Set<String> excludedTagsSet, Map<Source, Boolean> sourcesMap, LocalDateTime date) { SolrQuery query = new SolrQuery(); String include = ""; String exclude; String sources = "source:\""; List<String> includedTags = new ArrayList<>(includedTagsSet); if (includedTags.size() > 0) { include = "(" + replaceWhitespaces(includedTags.get(0), true) + ")"; for (int i = 1; i < includedTags.size(); i++) { include += " OR " + "(" + replaceWhitespaces(includedTags.get(i), true) + ")"; } } List<String> excludedTags = new ArrayList<>(excludedTagsSet); if (excludedTags.size() > 0) { exclude = "-(" + replaceWhitespaces(excludedTags.get(0), false) + ")"; for (int i = 1; i < excludedTags.size(); i++) { exclude += " AND " + "-(" + replaceWhitespaces(excludedTags.get(i), false) + ")"; } query.addFilterQuery(exclude); } for (Map.Entry<Source, Boolean> entry : sourcesMap.entrySet()) { if (entry.getValue()) { sources = sources.concat(entry.getKey().getId() + "\" OR \""); } } sources = sources.substring(0, sources.length() - 5); query.setQuery(include); query.set("qf", "title^3 description^2 body"); query.addFilterQuery("publishedAt:[" + date.toString() + " TO NOW]"); query.addFilterQuery(sources); if (client == null) { setServer(); } try { QueryResponse queryResponse = client.query(query); List<Article> response = queryResponse.getBeans(Article.class); return new LinkedHashSet<>(response); } catch (SolrServerException | IOException e) { Log.error("Solr: Can't Query", e); return new LinkedHashSet<>(); } } /** * Creates search string for solr * * @param string * @param and * @return */ public static String replaceWhitespaces(String string, boolean and) { String word; String result = ""; List<String> allMatches = new ArrayList<>(); if (and) { word = " AND "; } else { word = " OR "; } if (StringUtils.countMatches(string, '"') > 0 && ((StringUtils.countMatches(string, '"') & 1) == 1)) { string = string.concat("\""); } Matcher m = Pattern.compile("[^,]+").matcher(string); while (m.find()) { allMatches.add(m.group()); } for (String sub : allMatches) { sub = sub.trim(); result = result.concat("\"" + sub + "\"" + word); } if (and) { result = result.substring(0, result.length() - 5); } else { result = result.substring(0, result.length() - 4); } return result; } }