Package com.senseidb.util.JSONUtil

Examples of com.senseidb.util.JSONUtil.FastJSONObject


      {
        numHits = res.getNumHits();
        totalDocs = res.getTotalDocs();
      }

      JSONObject ret = new FastJSONObject();
      JSONObject obj = null;
      if (res != null && res.getSenseiHits() != null)
      {
        for (SenseiHit hit : res.getSenseiHits())
        {
          try
          {
            obj = new FastJSONObject(hit.getSrcData());
            ret.put(String.valueOf(hit.getUID()), obj);
          }
          catch(Exception ex)
          {
            logger.warn(ex.getMessage(), ex);
          }
        }
      }
      OutputStream ostream = resp.getOutputStream();
      ostream.write(ret.toString().getBytes("UTF-8"));
      ostream.flush();
    }
    catch (Exception e)
    {
      throw new ServletException(e.getMessage(),e);
View Full Code Here


    IndexWriter idxWriter = new IndexWriter(SimpleFSDirectory.open(idxDir),new StandardAnalyzer(Version.LUCENE_CURRENT),MaxFieldLength.UNLIMITED);
    while(true){
      String line = br.readLine();
      if (line==null) break;
     
      JSONObject obj = new FastJSONObject(line);
      ZoieIndexable indexable = defaultInterpreter.convertAndInterpret(obj);
      IndexingReq[] idxReqs = indexable.buildIndexingReqs();
      for (IndexingReq req : idxReqs){
        Document doc = req.getDocument();
        idxWriter.addDocument(doc);
View Full Code Here

    for (SenseiHit hit : hits)
    {
      Map<String, String[]> fieldMap = hit.getFieldValues();
      int fieldMapSize = fieldMap == null ? 0 : fieldMap.size();

      JSONObject hitObj = new FastJSONObject(20 + fieldMapSize);

      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_UID))
      {
        hitObj.put(PARAM_RESULT_HIT_UID, hit.getUID());
      }
      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_DOCID))
      {
        hitObj.put(PARAM_RESULT_HIT_DOCID, hit.getDocid());
      }
      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_SCORE))
      {
        hitObj.put(PARAM_RESULT_HIT_SCORE, formatScore(req.getScoreMeaningfulDigits(), hit.getScore()));
      }
      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_GROUPFIELD))
      {
        hitObj.put(PARAM_RESULT_HIT_GROUPFIELD, hit.getGroupField());
      }
      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_GROUPVALUE))
      {
        hitObj.put(PARAM_RESULT_HIT_GROUPVALUE, hit.getGroupValue());
      }
      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_GROUPHITSCOUNT))
      {
        hitObj.put(PARAM_RESULT_HIT_GROUPHITSCOUNT, hit.getGroupHitsCount());
      }
      if (hit.getGroupHits() != null && hit.getGroupHits().length > 0)
        hitObj.put(PARAM_RESULT_HIT_GROUPHITS, buildJSONHits(req, hit.getSenseiGroupHits()));

      // get fetchStored even if request does not have it because it could be set at the
      // federated broker level
      if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_SRC_DATA) ||
          req.isFetchStoredFields() || hit.getSrcData() != null)
      {
        hitObj.put(PARAM_RESULT_HIT_SRC_DATA, hit.getSrcData());
      }
      if (fieldMap != null)
      {
        Set<Entry<String, String[]>> entries = fieldMap.entrySet();
        for (Entry<String, String[]> entry : entries)
        {
          String key = entry.getKey();
          if (key.equals(PARAM_RESULT_HIT_UID))
          {
            // UID is already set.
            continue;
          }

          if (selectSet == null || selectSet.contains(key))
          {
            String[] vals = entry.getValue();

            JSONArray valArray;
            if (vals != null)
            {
              valArray = new FastJSONArray(vals.length);
              for (String val : vals)
              {
                valArray.put(val);
              }
            }
            else
            {
              valArray = new FastJSONArray();
            }
            hitObj.put(key, valArray);
          }
        }
      }

      Document doc = hit.getStoredFields();
      if (doc != null)
      {
        if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_STORED_FIELDS))
        {
          List<Fieldable> fields = doc.getFields();
          List<JSONObject> storedData = new ArrayList<JSONObject>(fields.size());
          for (Fieldable field : fields)
          {
            JSONObject data = new FastJSONObject(4);
            data.put(PARAM_RESULT_HIT_STORED_FIELDS_NAME, field.name());
            data.put(PARAM_RESULT_HIT_STORED_FIELDS_VALUE, field.stringValue());
            storedData.add(data);
          }

          hitObj.put(PARAM_RESULT_HIT_STORED_FIELDS, new FastJSONArray(storedData));
        }
      }

      Map<String,BrowseHit.TermFrequencyVector> tvMap = hit.getTermFreqMap();
      if (tvMap != null && tvMap.size() > 0){
        JSONObject tvObj = new FastJSONObject(2 * tvMap.entrySet().size());
        if (selectSet == null || selectSet.contains(PARAM_RESULT_HIT_TERMVECTORS))
        {
          hitObj.put(PARAM_RESULT_HIT_TERMVECTORS, tvObj);
        }

        Set<Entry<String,BrowseHit.TermFrequencyVector>> entries = tvMap.entrySet();
        for (Entry<String,BrowseHit.TermFrequencyVector> entry : entries){
          String field = entry.getKey();

          String[] terms = entry.getValue().terms;
          int[] freqs = entry.getValue().freqs;

          JSONArray tvArray = new FastJSONArray(terms.length);
          for (int i=0;i<terms.length;++i){
            JSONObject tv = new FastJSONObject(4);
            tv.put("term", terms[i]);
            tv.put("freq", freqs[i]);
            tvArray.put(tv);
          }

          tvObj.put(field, tvArray);
        }
View Full Code Here

  }

  public static JSONObject buildJSONResult(SenseiRequest req, SenseiResult res)
      throws Exception
  {
    JSONObject jsonObj = new FastJSONObject(15);
    jsonObj.put(PARAM_RESULT_TID, res.getTid());
    jsonObj.put(PARAM_RESULT_TOTALDOCS, res.getTotalDocs());
    jsonObj.put(PARAM_RESULT_NUMHITS, res.getNumHits());
    jsonObj.put(PARAM_RESULT_NUMGROUPS, res.getNumGroups());
    jsonObj.put(PARAM_RESULT_PARSEDQUERY, res.getParsedQuery());
    addErrors(jsonObj, res);
    SenseiHit[] hits = res.getSenseiHits();
    JSONArray hitArray = buildJSONHits(req, hits);
    jsonObj.put(PARAM_RESULT_HITS, hitArray);

    List<String> selectList = req.getSelectList();
    if (selectList != null)
    {
      JSONArray jsonSelectList = new FastJSONArray(selectList.size());
      for (String col: selectList)
      {
        jsonSelectList.put(col);
      }
      jsonObj.put(PARAM_RESULT_SELECT_LIST, jsonSelectList);
    }

    jsonObj.put(PARAM_RESULT_TIME, res.getTime());
    jsonObj.put(PARAM_RESULT_FACETS, convert(res.getFacetMap(), req));
    if (req.getMapReduceFunction() != null && res.getMapReduceResult() != null) {
      jsonObj.put(PARAM_RESULT_MAP_REDUCE, req.getMapReduceFunction().render(res.getMapReduceResult().getReduceResult()));
    }
  
    return jsonObj;
  }
View Full Code Here

  }

  private static void addErrors(JSONObject jsonResult, SenseiResult res) throws JSONException {
    JSONArray errorsJson = new FastJSONArray(res.getErrors().size());
    for (SenseiError error: res.getErrors()) {
      errorsJson.put(new FastJSONObject(5).put(PARAM_RESULT_ERROR_MESSAGE, error.getMessage())
                                         .put(PARAM_RESULT_ERROR_TYPE, error.getErrorType().name())
                                         .put(PARAM_RESULT_ERROR_CODE, error.getErrorCode()));
    }
    jsonResult.put(PARAM_RESULT_ERRORS, errorsJson);
    if (res.getErrors().size() > 0) {
View Full Code Here

  private static SenseiQuery buildSenseiQuery(DataConfiguration params)
  {
    SenseiQuery sq;
    String query = params.getString(PARAM_QUERY, null);

    JSONObject qjson = new FastJSONObject(30);
    if (query != null && query.length() > 0)
    {
      try
      {
        qjson.put("query", query);
      }
      catch (Exception e)
      {
        logger.error(e.getMessage(), e);
      }
    }

    try
    {
      String[] qparams = params.getStringArray(PARAM_QUERY_PARAM);
      for (String qparam : qparams)
      {
        qparam = qparam.trim();
        if (qparam.length() == 0)
          continue;
        String[] parts = qparam.split(":", 2);
        if (parts.length == 2)
        {
          qjson.put(parts[0], parts[1]);
        }
      }
    }
    catch (JSONException jse)
    {
View Full Code Here

    }
  }

  @Override
  protected String buildResultString(HttpServletRequest httpReq, SenseiSystemInfo info) throws Exception {
    JSONObject jsonObj = new FastJSONObject(8);
    jsonObj.put(PARAM_SYSINFO_NUMDOCS, info.getNumDocs());
    jsonObj.put(PARAM_SYSINFO_LASTMODIFIED, info.getLastModified());
    jsonObj.put(PARAM_SYSINFO_VERSION, info.getVersion());

    if (info.getSchema() != null && info.getSchema().length() != 0)
    {
      jsonObj.put(PARAM_SYSINFO_SCHEMA, new FastJSONObject(info.getSchema()));
    }

    Set<SenseiSystemInfo.SenseiFacetInfo> facets = info.getFacetInfos();
    JSONArray jsonArray = new FastJSONArray(facets == null ? 0 : facets.size());
    if (facets != null) {
        for (SenseiSystemInfo.SenseiFacetInfo facet : facets) {
          JSONObject facetObj = new FastJSONObject(6);
          facetObj.put(PARAM_SYSINFO_FACETS_NAME, facet.getName());
          facetObj.put(PARAM_SYSINFO_FACETS_RUNTIME, facet.isRunTime());
          facetObj.put(PARAM_SYSINFO_FACETS_PROPS, facet.getProps());
          jsonArray.put(facetObj);
        }
    }
    jsonObj.put(PARAM_SYSINFO_FACETS, jsonArray);

    List<SenseiSystemInfo.SenseiNodeInfo> clusterInfo = info.getClusterInfo();
    jsonArray = new FastJSONArray(clusterInfo == null ? 0 : clusterInfo.size());
    if (clusterInfo != null)
    {
      for (SenseiSystemInfo.SenseiNodeInfo nodeInfo : clusterInfo)
      {
        JSONObject nodeObj = new FastJSONObject(7);
        nodeObj.put(PARAM_SYSINFO_CLUSTERINFO_ID, nodeInfo.getId());
        nodeObj.put(PARAM_SYSINFO_CLUSTERINFO_PARTITIONS, new FastJSONArray(Arrays.asList(nodeInfo.getPartitions())));
        nodeObj.put(PARAM_SYSINFO_CLUSTERINFO_NODELINK, nodeInfo.getNodeLink());
        nodeObj.put(PARAM_SYSINFO_CLUSTERINFO_ADMINLINK, nodeInfo.getAdminLink());
        jsonArray.put(nodeObj);
      }
    }
    jsonObj.put(PARAM_SYSINFO_CLUSTERINFO, jsonArray);
View Full Code Here

    JSONObject expected = new JSONObject("{\"facetInit\":{\"member\":{\"age\":{\"values\":\"$user_age\",\"type\":\"int\"}}},\"meta\":{\"select_list\":[\"*\"],\"variables\":[\"user_age\"]}}");
    assertTrue(_comp.isEquals(json, expected));

    JsonTemplateProcessor jsonProc = new JsonTemplateProcessor();
    json.put(JsonTemplateProcessor.TEMPLATE_MAPPING_PARAM,
             new FastJSONObject().put("user_age",
                                  new FastJSONArray().put(25)));
    JSONObject newJson = jsonProc.substituteTemplates(json);
    JSONObject expected2 = new JSONObject("{\"facetInit\":{\"member\":{\"age\":{\"values\":[25],\"type\":\"int\"}}},\"templateMapping\":{\"user_age\":[25]},\"meta\":{\"select_list\":[\"*\"],\"variables\":[\"user_age\"]}}");
    assertTrue(_comp.isEquals(newJson, expected2));
  }
View Full Code Here

  public static final String QUERY_TYPE = "ids";

  @Override
  public Query doConstructQuery(JSONObject jsonQuery) throws JSONException
  {
    JSONObject filterJson = new FastJSONObject();
    filterJson.put(QUERY_TYPE, jsonQuery);

    Filter filter = null;
    try
    {
      filter = FilterConstructor.constructFilter(filterJson, null/* QueryParser is not used by this filter */);
 
View Full Code Here

        query = null;
      else if (obj instanceof JSONObject)
        query = (JSONObject)obj;
      else if (obj instanceof String)
      {
        query = new FastJSONObject();
        JSONObject tmp = new FastJSONObject();
        try
        {
          tmp.put("query", (String)obj);
          query.put("query_string", tmp);
        }
        catch (JSONException jse)
        {
          // Should never happen.
View Full Code Here

TOP

Related Classes of com.senseidb.util.JSONUtil.FastJSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.