public FacetFieldList(
     List<FacetField> facetFields,
     HashSet<String> hierarchicalNames,
     HashMap<String, String> fieldNameMappings) {
   if (facetFields == null) return;
   for (FacetField facetField : facetFields) {
     String fieldName = fieldNameMappings.get(facetField.getName());
     if (facetField.getValueCount() > 0) {
       this.add(
           new FacetFieldObject(fieldName, facetField, hierarchicalNames.contains(fieldName)));
     }
   }
 }
  @Test
  public void createInstance() {
    FacetField.Count count = mock(FacetField.Count.class);
    when(count.getCount()).thenReturn(1L);
    when(count.getName()).thenReturn("1,uuid:a");
    List<FacetField.Count> countList = Arrays.asList(count);

    FacetField facetField = mock(FacetField.class);
    when(facetField.getValues()).thenReturn(countList);

    FacetFieldFactory facetFieldFactory = new FacetFieldFactory();
    SearchSettings searchSettings = mock(SearchSettings.class);
    Map<String, Class<?>> facetClasses = new HashMap<String, Class<?>>();
    facetClasses.put("ANCESTOR_PATH", CutoffFacet.class);
    when(searchSettings.getFacetClasses()).thenReturn(facetClasses);
    facetFieldFactory.setSearchSettings(searchSettings);

    FacetFieldObject ffo = facetFieldFactory.createFacetFieldObject("ANCESTOR_PATH", facetField);
    assertTrue(ffo.getValues().get(0) instanceof CutoffFacet);
    assertEquals("1,uuid:a", ffo.getValues().get(0).getSearchValue());
    assertEquals("1,uuid:a", ffo.getValues().get(0).getValue());
    assertEquals(1, ffo.getValues().get(0).getCount());
  }
Esempio n. 3
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType(XML_RESPONSE_HEADER); // Talkback happens in XML form.
    response.setCharacterEncoding("UTF-8"); // Unicode++
    request.setCharacterEncoding("UTF-8");

    PrintWriter out = null; // The talkback buffer.

    // handle startrecord
    Integer startRecord = 0;

    if (!(request.getParameter("startRecord") == null)) {
      try {
        startRecord = Integer.parseInt(request.getParameter("startRecord")) - 1;
      } catch (NumberFormatException e) {
        startRecord = 0;
      }
    }

    // maximumrecords
    Integer maximumRecords = Integer.parseInt(this.config.getProperty("default_maximumRecords"));
    if (!(request.getParameter("maximumRecords") == null)) {
      maximumRecords = Integer.parseInt(request.getParameter("maximumRecords"));
    }

    // operation
    String operation = request.getParameter("operation");

    // x_collection
    String x_collection = request.getParameter("x-collection");
    if (x_collection == null) x_collection = this.config.getProperty("default_x_collection");
    if (x_collection == null) operation = null;

    // sortkeys
    String sortKeys = request.getParameter("sortKeys");

    // sortorder
    String sortOrder = request.getParameter("sortOrder");

    // recordschema
    String recordSchema = request.getParameter("recordSchema");
    if (recordSchema == null) recordSchema = "dc";

    if (recordSchema.equalsIgnoreCase("dcx")) {
      recordSchema = "dcx";
    }

    if (recordSchema.equalsIgnoreCase("solr")) {
      recordSchema = "solr";
    }

    // query request
    String query = request.getParameter("query");
    String q = request.getParameter("q");

    // who is requestor ?
    String remote_ip = request.getHeader("X-FORWARDED-FOR");

    if (remote_ip == null) {
      remote_ip = request.getRemoteAddr().trim();
    } else {
      remote_ip = request.getHeader("X-FORWARDED-FOR");
    }

    // handle debug
    Boolean debug = Boolean.parseBoolean(request.getParameter("debug"));
    if (!debug) {
      out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
    }

    // handle query
    if ((query == null) && (q != null)) {
      query = q;
    } else {
      if ((query != null) && (q == null)) {
        q = query;
      } else {
        operation = null;
      }
    }

    // handle operation
    if (operation == null) {
      if (query != null) {
        operation = "searchRetrieve";
      } else {
        operation = "explain";
      }
    }

    //  searchRetrieve
    if (operation.equalsIgnoreCase("searchRetrieve")) {
      if (query == null) {
        operation = "explain";
        log.debug(operation + ":" + query);
      }
    }

    // start talking back.
    String[] sq = {""};
    String solrquery = "";

    // facet

    String facet = null;
    List<FacetField> fct = null;

    if (request.getParameter("facet") != null) {
      facet = request.getParameter("facet");
      log.debug("facet : " + facet);
    }

    if (operation == null) {
      operation = "searchretrieve";
    } else { // explain response
      if (operation.equalsIgnoreCase("explain")) {
        log.debug("operation = explain");
        out.write("<srw:explainResponse xmlns:srw=\"http://www.loc.gov/zing/srw/\">");
        out.write("</srw:explainResponse>");
      } else { // DEBUG routine
        operation = "searchretrieve";

        String triplequery = null;

        if (query.matches(".*?\\[.+?\\].*?")) { // New symantic syntax
          triplequery = symantic_query(query);
          query = query.split("\\[")[0] + " " + triplequery;
          log.fatal(triplequery);

          solrquery = CQLtoLucene.translate(query, log, config);
        } else {
          solrquery = CQLtoLucene.translate(query, log, config);
        }
        log.debug(solrquery);

        if (debug == true) {
          response.setContentType(HTML_RESPONSE_HEADER);
          out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
          out.write("<html><body>\n\n");
          out.write("'" + remote_ip + "'<br>\n");
          out.write("<form action='http://www.kbresearch.nl/kbSRU'>");
          out.write("<input type=text name=q value='" + query + "' size=120>");
          out.write("<input type=hidden name=debug value=True>");
          out.write("<input type=submit>");
          out.write("<table border=1><tr><td>");
          out.write("q</td><td>" + query + "</td></tr><tr>");
          out.write("<td>query out</td><td>" + URLDecoder.decode(solrquery) + "</td></tr>");
          out.write(
              "<tr><td>SOLR_URL</td><td> <a href='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/?q="
                  + solrquery
                  + "'>"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/select/?q="
                  + solrquery
                  + "</a><br>"
                  + this.config.getProperty("solr_url")
                  + solrquery
                  + "</td></tr>");
          out.write(
              "<b>SOLR_QUERY</b> : <BR> <iframe width=900 height=400 src='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/../?q="
                  + solrquery
                  + "'></iframe><BR>");
          out.write(
              "<b>SRU_QUERY</b> : <BR> <a href="
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "'>"
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "</a><br><iframe width=901 height=400 src='http://www.kbresearch.nl/kbSRU/?q="
                  + query
                  + "'></iframe><BR>");
          out.write(
              "<br><b>JSRU_QUERY</b> : <BR><a href='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection="
                  + x_collection
                  + "'>http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC</a><br><iframe width=900 height=400 src='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC'></iframe>");

        } else { // XML SearchRetrieve response
          String url =
              this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl");
          String buffer = "";
          CommonsHttpSolrServer server = null;
          server = new CommonsHttpSolrServer(url);
          log.fatal("URSING " + url);
          server.setParser(new XMLResponseParser());
          int numfound = 0;
          try {
            SolrQuery do_query = new SolrQuery();
            do_query.setQuery(solrquery);
            do_query.setRows(maximumRecords);
            do_query.setStart(startRecord);

            if ((sortKeys != null) && (sortKeys.length() > 1)) {
              if (sortOrder != null) {
                if (sortOrder.equals("asc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.asc);
                }
                if (sortOrder.equals("desc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.desc);
                }
              } else {
                for (String str : sortKeys.trim().split(",")) {
                  str = str.trim();
                  if (str.length() > 1) {
                    if (str.equals("date")) {
                      do_query.setSortField("date_date", SolrQuery.ORDER.desc);
                      log.debug("SORTORDERDEBUG | DATE! " + str + " | ");
                      break;
                    } else {
                      do_query.setSortField(str + "_str", SolrQuery.ORDER.asc);
                      log.debug("SORTORDERDEBUG | " + str + " | ");
                      break;
                    }
                  }
                }
              }
            }

            if (facet != null) {
              if (facet.indexOf(",") > 1) {
                for (String str : facet.split(",")) {
                  if (str.indexOf("date") > 1) {
                    do_query.addFacetField(str);
                  } else {
                    do_query.addFacetField(str);
                  }
                  // do_query.setParam("facet.method", "enum");
                }
                // q.setFacetSort(false);
              } else {
                do_query.addFacetField(facet);
              }
              do_query.setFacet(true);
              do_query.setFacetMinCount(1);
              do_query.setFacetLimit(-1);
            }

            log.fatal(solrquery);

            QueryResponse rsp = null;
            boolean do_err = false;
            boolean do_sugg = false;
            SolrDocumentList sdl = null;
            String diag = "";
            StringBuffer suggest = new StringBuffer("");

            String content = "1";

            SolrQuery spellq = do_query;
            try {
              rsp = server.query(do_query);
            } catch (SolrServerException e) {
              String header = this.SRW_HEADER.replaceAll("\\$numberOfRecords", "0");
              out.write(header);
              diag = this.SRW_DIAG.replaceAll("\\$error", e.getMessage());
              do_err = true;
              rsp = null;
            }

            log.fatal("query done..");
            if (!(do_err)) { // XML dc response

              SolrDocumentList docs = rsp.getResults();
              numfound = (int) docs.getNumFound();
              int count = startRecord;
              String header =
                  this.SRW_HEADER.replaceAll("\\$numberOfRecords", Integer.toString(numfound));
              out.write(header);
              out.write("<srw:records>");

              Iterator<SolrDocument> iter = rsp.getResults().iterator();

              while (iter.hasNext()) {
                count += 1;
                if (recordSchema.equalsIgnoreCase("dc")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" xmlns:facets=\"info:srw/extension/4/facets\" >");
                  StringBuffer result = new StringBuffer("");

                  construct_lucene_dc(result, resultDoc);

                  out.write(result.toString());
                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("solr")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/solr</srw:recordSchema>");
                  out.write("<srw:recordData xmlns:expand=\"http://www.kbresearch.nl/expand\">");
                  StringBuffer result = new StringBuffer("");
                  construct_lucene_solr(result, resultDoc);
                  out.write(result.toString());

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("dcx")) { // XML dcx response
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData><srw_dc:dc xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/marc.relators/\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");

                  String dcx_data =
                      helpers.getOAIdcx(
                          "http://services.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                          log);
                  if (x_collection.equalsIgnoreCase("ggc-thes")) {
                    dcx_data =
                        helpers.getOAIdcx(
                            "http://serviceso.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                            log);
                  }

                  if (!(dcx_data.length() == 0)) {
                    out.write(dcx_data);
                  } else {
                    // Should not do this!!

                    out.write("<srw:record>");
                    out.write("<srw:recordPacking>xml</srw:recordPacking>");
                    out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                    out.write(
                        "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" >");
                    StringBuffer result = new StringBuffer("");

                    construct_lucene_dc(result, resultDoc);

                    out.write(result.toString());
                    out.write("</srw:recordData>");
                    out.write(
                        "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                    out.write("</srw:record>");
                  }

                  out.write("</srw_dc:dc>");

                  StringBuffer expand_data;
                  boolean expand = false;

                  if (content.startsWith("GGC-THES:AC:")) {
                    String tmp_content = "";
                    tmp_content = content.replaceFirst("GGC-THES:AC:", "");
                    log.fatal("calling get");
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/general/lod_new/get/"
                                    + tmp_content
                                    + "?format=rdf",
                                log));
                    log.fatal("get finini");

                    if (expand_data.toString().length() > 4) {

                      out.write(
                          "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                      out.write(expand_data.toString());
                      expand = true;
                    }
                  } else {
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/ANP.cgi?q=" + content, log));
                    if (expand_data.toString().length() > 0) {
                      if (!expand) {
                        out.write(
                            "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                        expand = true;
                      }
                      out.write(expand_data.toString());
                    }
                  }
                  if (expand) {
                    out.write("</srw_dc:expand>");
                  }

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }
              }
            }

            if ((do_err) || (numfound == 0)) {
              log.fatal("I haz suggestions");

              try {
                spellq.setParam("spellcheck", true);
                spellq.setQueryType("/spell");
                server = new CommonsHttpSolrServer(url);
                rsp = server.query(spellq);
                sdl = rsp.getResults();
                SpellCheckResponse spell;
                spell = rsp.getSpellCheckResponse();
                List<SpellCheckResponse.Suggestion> suggestions = spell.getSuggestions();
                if (suggestions.isEmpty() == false) {
                  suggest.append("<srw:extraResponseData>");
                  suggest.append("<suggestions>");

                  for (SpellCheckResponse.Suggestion sugg : suggestions) {
                    suggest.append("<suggestionfor>" + sugg.getToken() + "</suggestionfor>");
                    for (String item : sugg.getSuggestions()) {
                      suggest.append("<suggestion>" + item + "</suggestion>");
                    }
                    suggest.append("</suggestions>");
                    suggest.append("</srw:extraResponseData>");
                  }
                  do_sugg = true;
                }
              } catch (Exception e) {
                rsp = null;
                // log.fatal(e.toString());
              }
              ;
            }
            ;

            if (!do_err) {
              if (facet != null) {

                try {
                  fct = rsp.getFacetFields();
                  out.write("<srw:facets>");

                  for (String str : facet.split(",")) {
                    out.write("<srw:facet>");
                    out.write("<srw:facetType>");
                    out.write(str);
                    out.write("</srw:facetType>");

                    for (FacetField f : fct) {
                      log.debug(f.getName());
                      // if (f.getName().equals(str+"_str") || (f.getName().equals(str+"_date")) ) {
                      List<FacetField.Count> facetEnties = f.getValues();
                      for (FacetField.Count fcount : facetEnties) {
                        out.write("<srw:facetValue>");
                        out.write("<srw:valueString>");
                        out.write(helpers.xmlEncode(fcount.getName()));
                        out.write("</srw:valueString>");
                        out.write("<srw:count>");
                        out.write(Double.toString(fcount.getCount()));
                        out.write("</srw:count>");
                        out.write("</srw:facetValue>");
                        //   }
                      }
                    }
                    out.write("</srw:facet>");
                  }
                  out.write("</srw:facets>");
                  startRecord += 1;
                } catch (Exception e) {
                }

                // log.fatal(e.toString()); }
              }
            } else {
              out.write(diag);
            }
            out.write("</srw:records>"); // SearchRetrieve response footer
            String footer = this.SRW_FOOTER.replaceAll("\\$query", helpers.xmlEncode(query));
            footer = footer.replaceAll("\\$startRecord", (startRecord).toString());
            footer = footer.replaceAll("\\$maximumRecords", maximumRecords.toString());
            footer = footer.replaceAll("\\$recordSchema", recordSchema);
            if (do_sugg) {
              out.write(suggest.toString());
            }
            out.write(footer);
          } catch (MalformedURLException e) {
            out.write(e.getMessage());
          } catch (IOException e) {
            out.write("TO ERR is Human");
          }
        }
      }
    }
    out.close();
  }
Esempio n. 4
0
  /**
   * Returns a SolrDocumentList for a given set of search parameters. Search parameters are parsed
   * and mapped into solr (solrj) query syntax, and solr query is performed.
   *
   * @param queryParams query parameters to map to a solr query
   * @return the SolrDocumentList for this query
   * @see SolrDocumentList
   */
  private SolrDocumentList doQuery(MultivaluedMap<String, String> queryParams) {
    SolrDocumentList docs = null;
    HttpSolrServer server = null;
    ArrayList<String> queryList = new ArrayList<String>();
    server = SolrServer.getSolrConnection();
    SolrQuery query = new SolrQuery();
    for (String key : queryParams.keySet()) {
      String value = queryParams.getFirst(key);
      System.out.println(key + " : " + queryParams.getFirst(key) + "\n");
      if (key.equals("start")) {
        int startNo = Integer.parseInt(value);
        if (startNo < 0) startNo = 0;
        query.setStart(startNo);
      } else if (key.equals("limit")) {
        limit = Integer.parseInt(value);
        query.setRows(limit);
      } else if (key.equals("sort.asc") || key.equals("sort")) query.setSort(value, ORDER.asc);
      else if (key.equals("sort.desc")) query.setSort(value, ORDER.desc);
      else if (key.startsWith("facet")) {
        query.setFacet(true);
        String[] facetArray = value.split(",");
        for (String f : facetArray) {
          query.addFacetField(f);
        }
      } else {
        if (key.endsWith("_exact")) queryList.add(key.replace("_exact", "") + ":\"" + value + "\"");
        else {
          if (value.contains(" ")) value = "( " + value.replaceAll(" ", " AND ") + ")";
          if (key.equals("q")) queryList.add("keyword:" + value);
          else queryList.add(key + "_keyword:" + value);
        }
      }
    }

    Iterator<String> it = queryList.iterator();
    String queryStr = "";
    while (it.hasNext()) {
      String qTerm = (String) it.next();
      System.out.print("QT: " + qTerm + "\n");
      queryStr += qTerm;
      System.out.print("QS: " + queryStr + "\n");
      if (it.hasNext()) queryStr += " AND ";
    }
    System.out.print("queryStr: " + queryStr);
    query.setQuery(queryStr);
    QueryResponse response = null;
    try {
      response = server.query(query);
    } catch (SolrServerException se) {
      log.error(se.getMessage());
      throw new BadParameterException(se.getMessage());
    } catch (RemoteSolrException rse) {
      if (rse.getMessage().contains("SyntaxError")) {
        log.error("solr syntax error");
        throw new BadParameterException("Incorrect query syntax");
      } else {
        String msg = rse.getMessage().replace("_keyword", "");
        log.error(msg);
        throw new BadParameterException("Incorrect query syntax:" + msg);
      }
    }
    List<FacetField> facets = response.getFacetFields();
    facet = null;
    if (facets != null) {
      facet = new Facet();
      List<FacetType> facetTypes = new ArrayList<FacetType>();
      for (FacetField facetField : facets) {
        List<FacetTerm> facetTerms = new ArrayList<FacetTerm>();
        FacetType facetType = new FacetType();
        facetType.setFacetName(facetField.getName());
        List<FacetField.Count> facetEntries = facetField.getValues();
        for (FacetField.Count fcount : facetEntries) {
          if (fcount.getCount() > 0) {
            FacetTerm facetTerm = new FacetTerm();
            facetTerm.setTermName(fcount.getName());
            facetTerm.setTermCount(fcount.getCount());
            // System.out.println(fcount.getName() + ": " + fcount.getCount());
            facetTerms.add(facetTerm);
          }
        }
        facetType.setFacetTerms(facetTerms);
        facetTypes.add(facetType);
      }
      facet.setFacetTypes(facetTypes);
    }
    docs = response.getResults();
    return docs;
  }
Esempio n. 5
0
  /** Simply selects the home view to render by returning its name. */
  @RequestMapping(value = "/charts", method = RequestMethod.GET)
  public ModelAndView home(Locale locale, Model model) {

    logger.info("Welcome Charts! The client locale is {}.", locale);

    // model = new object.. set values and set to MAV..

    SolrQuery query = new SolrQuery("id:*");
    query.set("facet", true);
    query.addFacetField("tweet_category");
    query.addSort("tweet_category", SolrQuery.ORDER.asc);
    query.setRows(0);
    ChartBean bean = new ChartBean();
    try {
      QueryResponse response = homeservice.getServiceResponse(query);
      Collection<JSONObject> productsList = new ArrayList<JSONObject>();
      Collection<JSONObject> tabletsAndSmartPhones = new ArrayList<JSONObject>();
      if (response != null) {
        FacetField categoryFacet = response.getFacetField("tweet_category");
        List<Count> list = categoryFacet.getValues();
        Iterator itr = list.iterator();
        Collection<String> productKeys = new ArrayList<String>();
        Collection<String> smartPhoneKeys = new ArrayList<String>();
        while (itr.hasNext()) {
          Count categoryObj = (Count) itr.next();
          if (categoryObj != null) {
            String categoryName = categoryObj.getName();
            if ((categoryName.contains("Products") || categoryName.contains("Product"))
                && categoryName.contains("_")) {
              // System.out.println(categoryName.split("_")[2]);
              String subCatName = categoryName.split("_")[2];
              JSONObject tempJSON = new JSONObject();
              if (!productKeys.contains(subCatName)) {
                productKeys.add(subCatName);
                tempJSON.put("label", subCatName);
                tempJSON.put("data", categoryObj.getCount());
                productsList.add(tempJSON);
              }

            } else if ((categoryName.contains("Smartphones") || categoryName.contains("Tablets"))
                && categoryName.contains("_")) {
              String subCatName = categoryName.split("_")[2];
              JSONObject tempJSON = new JSONObject();
              if (!smartPhoneKeys.contains(subCatName)) {
                smartPhoneKeys.add(subCatName);
                tempJSON.put("label", subCatName);
                tempJSON.put("data", categoryObj.getCount());
                tabletsAndSmartPhones.add(tempJSON);
              }
            }
          }
        }
      }
      JSONObject productsJSON = new JSONObject();
      productsJSON.put("productList", productsList);
      JSONObject smartPhoneJSON = new JSONObject();
      smartPhoneJSON.put("phonesAndTablets", tabletsAndSmartPhones);
      bean.setProductsJSON(productsJSON);
      bean.setSmartphoneAndTablets(smartPhoneJSON);

      query.addFacetPivotField("tweetPostedTime,sentimentScore");
      query.addSort("tweetPostedTime", SolrQuery.ORDER.asc);
      query.setRows(0);
      DateFormat dateFormat1 = new SimpleDateFormat("E MMM d hh:mm:ss zzz yyyy");
      SimpleDateFormat newFormat = new SimpleDateFormat("MM-dd-yyyy");
      Collection<JSONObject> sentimentObjList = new ArrayList<JSONObject>();
      JSONObject tempObj = new JSONObject();
      JSONObject finalObj = new JSONObject();
      response = homeservice.getServiceResponse(query);
      List<Date> myList = new ArrayList<Date>();
      if (response != null) {
        System.out.println(response);
        NamedList<List<PivotField>> pivotNamedList = response.getFacetPivot();
        System.out.println(pivotNamedList.size());
        for (int i = 0; i < pivotNamedList.size(); i++) {
          List<PivotField> pvotList = pivotNamedList.getVal(i);
          for (PivotField pf : pvotList) {
            if (pf != null) {
              System.out.println(pf.getValue());
              Date beforeParse = dateFormat1.parse(pf.getValue().toString());
              myList.add(beforeParse);
              JSONObject sentimentObj = new JSONObject();

              sentimentObj.put("period", newFormat.format(beforeParse));
              // System.out.println(pf.getField());
              List<PivotField> sentimentList = pf.getPivot();
              int negativeTotal = 0;
              int neutralTotal = 0;
              int positiveTotal = 0;
              for (PivotField sentimentpivot : sentimentList) {

                if (sentimentpivot != null) {
                  if (sentimentpivot.getValue() != null) {
                    int fullCatName = (Integer) sentimentpivot.getValue();
                    if (fullCatName == 0) {
                      negativeTotal = negativeTotal + sentimentpivot.getCount();
                    } else if (fullCatName == 1 || fullCatName == 2) {
                      neutralTotal = neutralTotal + sentimentpivot.getCount();
                    } else if (fullCatName == 3 || fullCatName == 4) {
                      positiveTotal = positiveTotal + sentimentpivot.getCount();
                    }
                  }
                }
              }
              sentimentObj.put("negative", negativeTotal);
              sentimentObj.put("positive", positiveTotal);
              sentimentObj.put("neutral", neutralTotal);
              tempObj.put(newFormat.format(beforeParse), sentimentObj);
            }
          }
        }
      }
      Collections.sort(myList);
      Iterator<Date> itr = myList.iterator();
      while (itr.hasNext()) {

        Date inputDate = dateFormat1.parse(itr.next().toString());

        String tempKey = newFormat.format(inputDate);
        System.out.println(tempKey);
        JSONObject newObj = (JSONObject) tempObj.get(tempKey);
        sentimentObjList.add(newObj);
      }
      finalObj.put("sentimentData", sentimentObjList);
      bean.setSentimentDataObject(finalObj);
    } catch (Exception e) {
      e.printStackTrace();
    }
    ModelAndView mav = new ModelAndView("charts", "model", bean);
    logger.info("bye now");
    return mav;
  }
Esempio n. 6
0
  @RequestMapping(value = "/searchtweetresponse", method = RequestMethod.GET)
  @ResponseBody
  public String getTweets(
      @RequestParam("search") String searchKey,
      @RequestParam("start") int start,
      @RequestParam("end") int end) {

    logger.info("Welcome to Tweets");
    JSONObject finalObject = new JSONObject();

    int startRow = 0;
    int endRow = 10;
    // String tweetType="*";

    if (start > startRow) {
      startRow = start;
    }
    if (end > endRow) {
      endRow = end;
    }
    SolrQuery query = new SolrQuery("tweet_content:*" + searchKey + "*");
    query.setStart(startRow);
    query.setRows(endRow);
    HomeBean bean = new HomeBean();
    Collection<JSONObject> objectList = new ArrayList<JSONObject>();
    try {
      QueryResponse response = homeservice.getServiceResponse(query);
      if (response != null) {
        SolrDocumentList responseList = response.getResults();
        long totalCount = responseList.getNumFound();
        if (totalCount > 0) {
          bean.setResultCount(totalCount);
        }
        finalObject.put("total", totalCount);
        for (SolrDocument document : responseList) {
          JSONObject json = new JSONObject();
          json.put("id", document.getFieldValue("id"));
          json.put("tweet", document.getFieldValue("tweet_content"));
          objectList.add(json);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    finalObject.put("tweetList", objectList);

    query.set("facet", true);
    query.addFacetField("tweetPostedTime");
    query.addSort("tweetPostedTime", SolrQuery.ORDER.asc);
    query.setRows(0);
    QueryResponse response = homeservice.getServiceResponse(query);
    // 2015-07-30T12:10:31Z
    DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat newFormat = new SimpleDateFormat("MM-dd-yyyy");
    DateFormat dateFormat2 = new SimpleDateFormat("E MMM d hh:mm:ss zzz yyyy");
    List<Date> myList = new ArrayList<Date>();
    // Collection<JSONObject> tempList = new ArrayList<JSONObject>();
    Collection<JSONObject> dateCountList = new ArrayList<JSONObject>();
    JSONObject tempObj = new JSONObject();
    try {
      if (response != null) {
        FacetField timeFacet = response.getFacetField("tweetPostedTime");
        List<Count> list = timeFacet.getValues();
        Iterator itr = list.iterator();
        // Collection<String> productKeys = new ArrayList<String>();
        // Collection<String> smartPhoneKeys = new ArrayList<String>();
        while (itr.hasNext()) {
          Count timeObj = (Count) itr.next();
          if (timeObj != null) {
            String timeName = timeObj.getName();
            String originalTime = timeName.split("T")[0];
            Date beforeParse = dateFormat1.parse(originalTime);
            myList.add(beforeParse);
            JSONObject timeObject = new JSONObject();
            timeObject.put("date", newFormat.format(beforeParse));
            timeObject.put("count", timeObj.getCount());
            // tempList.add(timeObject);
            tempObj.put(newFormat.format(beforeParse), timeObject);
          }
        }
      }
      Collections.sort(myList);
      Iterator<Date> itr = myList.iterator();
      while (itr.hasNext()) {

        Date inputDate = dateFormat2.parse(itr.next().toString());

        String tempKey = newFormat.format(inputDate);
        System.out.println(tempKey);
        JSONObject newObj = (JSONObject) tempObj.get(tempKey);
        dateCountList.add(newObj);
      }
      finalObject.put("dateList", dateCountList);

    } catch (Exception e) {
      logger.error(e.getMessage());
      e.printStackTrace();
    }
    logger.info("bye Tweets");
    return finalObject.toString();
  }