/**
  * conversion from a SolrQueryResponse (which is a solr-internal data format) to SolrDocumentList
  * (which is a solrj-format) The conversion is done inside the solrj api using the
  * BinaryResponseWriter and a very complex unfolding process via
  * org.apache.solr.common.util.JavaBinCodec.marshal.
  *
  * @param request
  * @param sqr
  * @return
  */
 public SolrDocumentList SolrQueryResponse2SolrDocumentList(
     final SolrQueryRequest req, final SolrQueryResponse rsp) {
   SolrDocumentList sdl = new SolrDocumentList();
   NamedList<?> nl = rsp.getValues();
   ResultContext resultContext = (ResultContext) nl.get("response");
   DocList response =
       resultContext == null
           ? new DocSlice(0, 0, new int[0], new float[0], 0, 0.0f)
           : resultContext.docs;
   sdl.setNumFound(response == null ? 0 : response.matches());
   sdl.setStart(response == null ? 0 : response.offset());
   String originalName = Thread.currentThread().getName();
   if (response != null) {
     try {
       SolrIndexSearcher searcher = req.getSearcher();
       final int responseCount = response.size();
       DocIterator iterator = response.iterator();
       for (int i = 0; i < responseCount; i++) {
         int docid = iterator.nextDoc();
         Thread.currentThread()
             .setName("EmbeddedSolrConnector.SolrQueryResponse2SolrDocumentList: " + docid);
         Document responsedoc = searcher.doc(docid, (Set<String>) null);
         SolrDocument sordoc = doc2SolrDoc(responsedoc);
         sdl.add(sordoc);
       }
     } catch (IOException e) {
       ConcurrentLog.logException(e);
     }
   }
   Thread.currentThread().setName(originalName);
   return sdl;
 }
Example #2
0
 /**
  * Returns SearchResultsSlim for a given SolrDocumentList. A "slimmer" results object without the
  * "items" wrapper element is created for better transform to json.
  *
  * @param doc solr document list to build results
  * @return the SearchResultsSlim object for this solr result
  * @see SearchResultsSlim
  */
 private SearchResultsSlim buildSlimResults(SolrDocumentList docs) {
   SearchResultsSlim results = new SearchResultsSlim();
   Pagination pagination = new Pagination();
   pagination.setNumFound(docs.getNumFound());
   pagination.setStart(docs.getStart());
   pagination.setRows(limit);
   // List<ModsType> modsTypes = new ArrayList<ModsType>();
   List<Item> items = new ArrayList<Item>();
   for (final SolrDocument doc : docs) {
     Item item = new Item();
     ModsType modsType = null;
     try {
       modsType = (new ItemDAO()).getModsType(doc);
     } catch (JAXBException je) {
       log.error(je.getMessage());
       je.printStackTrace();
     }
     item.setModsType(modsType);
     items.add(item);
   }
   results.setItems(items);
   results.setPagination(pagination);
   if (facet != null) results.setFacet(facet);
   return results;
 }
  public SearchResponse search(SearchRequest request) {
    SolrQuery query = new SolrQuery();
    query.setRows(request.getLimit());
    query.setStart(request.getOffset() * request.getLimit());
    BooleanQuery aggregate = new BooleanQuery();
    if (!StringUtils.isBlank(request.getText())) {
      TermQuery termQuery = new TermQuery(new Term("", request.getText()));
      aggregate.add(termQuery, BooleanClause.Occur.SHOULD);
    }
    if (!StringUtils.isBlank(request.getEventId())) {
      aggregate.add(
          new TermQuery(new Term("eventid", request.getEventId())), BooleanClause.Occur.MUST);
    }
    query.setQuery(aggregate.toString());

    log.info("QUERY IS: " + query.toString());
    try {
      QueryResponse queryResponse = solrServer.query(query);
      log.info("RESPONSE FROM QUERY WAS: " + queryResponse);
      SolrDocumentList results = queryResponse.getResults();
      log.info("RESULTS WAS: " + results);
      ArrayList<SearchResponse.Hit> hits = new ArrayList<SearchResponse.Hit>();
      for (SolrDocument result : results) {
        hits.add(
            new SearchResponse.Hit(
                ObjectType.session, String.valueOf(result.getFieldValue("id")), null));
      }
      return new SearchResponse(results.getNumFound(), queryResponse.getElapsedTime(), hits);
    } catch (SolrServerException e) {
      e.printStackTrace();
    }
    return null;
  }
  /** {@inheritDoc} */
  @Override
  public void transform(
      Map<String, ?> result, ResponseBuilder rb, SolrDocumentSource solrDocumentSource) {
    Object value = result.get(rb.getGroupingSpec().getFields()[0]);
    if (TopGroups.class.isInstance(value)) {
      @SuppressWarnings("unchecked")
      TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
      SolrDocumentList docList = new SolrDocumentList();
      docList.setStart(rb.getGroupingSpec().getOffset());
      docList.setNumFound(rb.totalHitCount);

      Float maxScore = Float.NEGATIVE_INFINITY;
      for (GroupDocs<BytesRef> group : topGroups.groups) {
        for (ScoreDoc scoreDoc : group.scoreDocs) {
          if (maxScore < scoreDoc.score) {
            maxScore = scoreDoc.score;
          }
          docList.add(solrDocumentSource.retrieve(scoreDoc));
        }
      }
      if (maxScore != Float.NEGATIVE_INFINITY) {
        docList.setMaxScore(maxScore);
      }
      rb.rsp.addResponse(docList);
    }
  }
Example #5
0
    protected List<Vector> processQueryResponse(QueryResponse resp) {
      NamedList<Object> response = resp.getResponse();

      NamedList<Object> termVectorsNL = (NamedList<Object>) response.get("termVectors");
      if (termVectorsNL == null)
        throw new RuntimeException(
            "No termVectors in response! "
                + "Please check your query to make sure it is requesting term vector information from Solr correctly.");

      List<Vector> termVectors = new ArrayList<Vector>(termVectorsNL.size());
      Iterator<Map.Entry<String, Object>> iter = termVectorsNL.iterator();
      while (iter.hasNext()) {
        Map.Entry<String, Object> next = iter.next();
        String nextKey = next.getKey();
        Object nextValue = next.getValue();
        if (nextValue instanceof NamedList) {
          NamedList nextList = (NamedList) nextValue;
          Object fieldTerms = nextList.get(field);
          if (fieldTerms != null && fieldTerms instanceof NamedList) {
            termVectors.add(
                SolrTermVector.newInstance(nextKey, hashingTF, (NamedList<Object>) fieldTerms));
          }
        }
      }

      SolrDocumentList docs = resp.getResults();
      totalDocs = docs.getNumFound();

      return termVectors;
    }
 @Test(dependsOnMethods = "testAddDocument")
 public void testBasicQuery() throws SolrServerException {
   SolrServer server = solrServerFactory.getServer();
   SolrParams params = new SolrQuery("*:*");
   SolrDocumentList results = server.query(params).getResults();
   assertTrue(results.getNumFound() > 0, "didn't return any results");
 }
 @Test(dependsOnMethods = "testBasicQuery", dataProvider = "documentMap")
 public void testIdQuery(Map<String, String[]> document) throws SolrServerException {
   String id = document.get("id")[0];
   SolrServer server = solrServerFactory.getServer();
   SolrParams params = new SolrQuery("id:" + id);
   SolrDocumentList results = server.query(params).getResults();
   assertEquals(results.getNumFound(), 1, "didn't find article by id");
 }
 /** @return list of post ids */
 public ArrayList<String> getListId() {
   ArrayList<String> result = new ArrayList<String>();
   SolrDocumentList listResponses = response.getResults();
   for (int index = 0; index < listResponses.size(); index++) {
     result.add((String) listResponses.get(index).get(PostFields.INDEX_KEY));
   }
   return result;
 }
  @Override
  public RecommendResponse recommend(RecommendQuery query, Integer maxReuslts) {
    productsToFilter = new ArrayList<String>();

    String queryString = "*:*";
    String sortCriteria = "user_count_purchased desc";
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    QueryResponse response = null;
    RecommendResponse searchResponse = new RecommendResponse();

    try {
      String filterQueryString =
          RecommendationQueryUtils.buildFilterForContentBasedFiltering(contentFilter);

      if (query.getProductIds() != null && query.getProductIds().size() > 0) {
        productsToFilter.addAll(query.getProductIds());
      }
      if (query.getUser() != null) {
        if (alreadyBoughtProducts != null) {
          productsToFilter.addAll(alreadyBoughtProducts);
        }
      }

      if (productsToFilter != null && productsToFilter.size() > 0) {
        if (filterQueryString.length() > 0) {
          filterQueryString += " OR ";
        }
        filterQueryString +=
            RecommendationQueryUtils.buildFilterForAlreadyBoughtProducts(productsToFilter);
      }
      solrParams.set("q", queryString);
      solrParams.set("fq", filterQueryString);
      solrParams.set("sort", sortCriteria);
      solrParams.set("rows", maxReuslts);

      response =
          SolrServiceContainer.getInstance()
              .getRecommendService()
              .getSolrServer()
              .query(solrParams);
      // fill response object
      List<String> extractedRecommendations =
          RecommendationQueryUtils.extractRecommendationIds(
              response.getBeans(CustomerAction.class));
      searchResponse.setResultItems(extractedRecommendations);
      searchResponse.setElapsedTime(response.getElapsedTime());
      SolrDocumentList docResults = response.getResults();
      searchResponse.setNumFound(docResults.getNumFound());
    } catch (SolrServerException e) {
      e.printStackTrace();
      searchResponse.setNumFound(0);
      searchResponse.setResultItems(new ArrayList<String>());
      searchResponse.setElapsedTime(-1);
    }

    return searchResponse;
  }
Example #10
0
 public void writeSolrDocumentList(SolrDocumentList docs) throws IOException {
   writeTag(SOLRDOCLST);
   List<Number> l = new ArrayList<>(3);
   l.add(docs.getNumFound());
   l.add(docs.getStart());
   l.add(docs.getMaxScore());
   writeArray(l);
   writeArray(docs);
 }
  @Override
  public void writeSolrDocumentList(
      String name, SolrDocumentList docs, Set<String> fields, Map otherFields) throws IOException {
    boolean includeScore = false;
    if (fields != null) {
      includeScore = fields.contains("score");
      if (fields.size() == 0 || (fields.size() == 1 && includeScore) || fields.contains("*")) {
        fields = null; // null means return all stored fields
      }
    }

    int sz = docs.size();

    writeMapOpener(includeScore ? 4 : 3);
    incLevel();
    writeKey("numFound", false);
    writeLong(null, docs.getNumFound());
    writeMapSeparator();
    writeKey("start", false);
    writeLong(null, docs.getStart());

    if (includeScore && docs.getMaxScore() != null) {
      writeMapSeparator();
      writeKey("maxScore", false);
      writeFloat(null, docs.getMaxScore());
    }
    writeMapSeparator();
    // indent();
    writeKey("docs", false);
    writeArrayOpener(sz);

    incLevel();
    boolean first = true;

    SolrIndexSearcher searcher = req.getSearcher();
    for (SolrDocument doc : docs) {

      if (first) {
        first = false;
      } else {
        writeArraySeparator();
      }
      indent();
      writeSolrDocument(null, doc, fields, otherFields);
    }
    decLevel();
    writeArrayCloser();

    if (otherFields != null) {
      writeMap(null, otherFields, true, false);
    }

    decLevel();
    indent();
    writeMapCloser();
  }
Example #12
0
 private void assertOrder(QueryResponse rsp, String... docs) throws Exception {
   SolrDocumentList list = rsp.getResults();
   for (int i = 0; i < docs.length; i++) {
     SolrDocument doc = list.get(i);
     Object o = doc.getFieldValue("id");
     if (!docs[i].equals(o)) {
       throw new Exception("Order is not correct:" + o + "!=" + docs[i]);
     }
   }
 }
  @Override
  public <T> ScoredPage<T> queryForPage(Query query, Class<T> clazz) {
    Assert.notNull(query, "Query must not be 'null'.");
    Assert.notNull(clazz, "Target class must not be 'null'.");

    QueryResponse response = query(query);
    List<T> beans = convertQueryResponseToBeans(response, clazz);
    SolrDocumentList results = response.getResults();
    return new SolrResultPage<T>(
        beans, query.getPageRequest(), results.getNumFound(), results.getMaxScore());
  }
Example #14
0
  public SolrDocumentList readSolrDocumentList(DataInputInputStream dis) throws IOException {
    SolrDocumentList solrDocs = new SolrDocumentList();
    List list = (List) readVal(dis);
    solrDocs.setNumFound((Long) list.get(0));
    solrDocs.setStart((Long) list.get(1));
    solrDocs.setMaxScore((Float) list.get(2));

    @SuppressWarnings("unchecked")
    List<SolrDocument> l = (List<SolrDocument>) readVal(dis);
    solrDocs.addAll(l);
    return solrDocs;
  }
Example #15
0
 protected void assertSolrQuery(Object result, String fld, String query) throws Exception {
   // check the field value of a result returned in the usual Solr way
   SolrQuery q = new SolrQuery(query);
   QueryResponse rsp = solr.query(q, METHOD.POST);
   NamedList<Object> response = rsp.getResponse();
   SolrDocumentList docs = (SolrDocumentList) response.get("response");
   if (result == null) {
     assertEquals(0, docs.size());
   } else {
     assertNotNull("no docs returned", docs);
     assertEquals("unexpected result count", 1, docs.size());
     assertEquals(result, docs.get(0).get(fld));
   }
 }
Example #16
0
  protected void execute() throws Exception {

    SolrServer server = new HttpSolrServer("http://localhost:8983/solr");
    SolrQuery query = new SolrQuery("text:ipod");
    QueryResponse response = server.query(query);

    SolrDocumentList docList = response.getResults();
    System.out.println("件数:" + docList.size());
    for (SolrDocument doc : docList) {
      System.out.println("■");
      for (String fieldName : doc.getFieldNames()) {
        System.out.println(fieldName.toString() + ":" + doc.getFieldValue(fieldName.toString()));
      }
    }
  }
 @SuppressWarnings("unused")
 private void printHits(SolrQuery q) throws SolrServerException {
   System.out.println("--- solr contents " + q.toString() + " ---");
   SolrDocumentList results = getSolr().query(q).getResults();
   if (results.size() == 0) {
     System.out.println("empty");
   }
   for (SolrDocument d : results) {
     for (String f : d.getFieldNames()) {
       String v = "" + d.get(f);
       System.out.print(", " + f + ": " + v);
     }
     System.out.println("");
   }
 }
  // Build an FL_Link result from a list of SolrDocuments returned from a group command in a query
  protected FL_Link buildResultFromGroupedDocuments(SolrDocumentList dl) {

    // Build the initial result from the first document
    FL_Link link = buildResultFromDocument(dl.get(0));

    // Get the nodetype
    String targetField =
        PropertyDescriptorHelper.mapKey(
            FL_RequiredPropertyKey.TO.name(),
            _applicationConfiguration.getLinkDescriptors().getProperties(),
            link.getType());

    // Add the remaining document properties to the entity
    // Currently only the TO field is aggregated from grouping of one-to-many links
    for (int i = 1; i < dl.size(); i++) {
      SolrDocument sd = dl.get(i);
      String target = (String) sd.getFieldValue(targetField);

      FL_Property property = null;
      List<FL_Property> properties = link.getProperties();
      for (FL_Property prop : link.getProperties()) {
        if (prop.getKey().equals(FL_RequiredPropertyKey.TO.name())) {
          property = prop;
        }
      }

      if (property != null) {
        Object range = property.getRange();

        if (range instanceof FL_ListRange) {
          List<Object> values = ((FL_ListRange) range).getValues();
          values.add(target);
        } else if (range instanceof FL_SingletonRange) {
          List<Object> values = new ArrayList<Object>();
          values.add(((FL_SingletonRange) range).getValue());
          values.add(target);
          property.setRange(
              FL_ListRange.newBuilder().setType(FL_PropertyType.STRING).setValues(values).build());
        }

        link.setProperties(properties);
      }

      link.setTarget(link.getTarget() + "," + getTarget(sd));
    }

    return link;
  }
Example #19
0
  // 查询
  @Test
  public void testSolrForSearch() throws Exception {
    SolrQuery params = new SolrQuery();
    params.set("q", "*:*");
    QueryResponse response = solrServer.query(params);
    // 结果集
    SolrDocumentList docs = response.getResults();
    long count = docs.getNumFound();

    System.out.println("结果集有 :" + count);
    for (SolrDocument doc : docs) {
      String id = (String) doc.get("id");
      String name = (String) doc.get("name");
      System.out.println("id :" + id + "---" + "name :" + name);
    }
  }
  @Override
  public <T> HighlightPage<T> queryForHighlightPage(HighlightQuery query, Class<T> clazz) {
    Assert.notNull(query, "Query must not be 'null'.");
    Assert.notNull(clazz, "Target class must not be 'null'.");

    QueryResponse response = query(query);

    List<T> beans = convertQueryResponseToBeans(response, clazz);
    SolrDocumentList results = response.getResults();
    SolrResultPage<T> page =
        new SolrResultPage<T>(
            beans, query.getPageRequest(), results.getNumFound(), results.getMaxScore());
    ResultHelper.convertAndAddHighlightQueryResponseToResultPage(response, page);

    return page;
  }
Example #21
0
  public JSONArray Search(String query, String sort) {
    SolrServer server;
    SolrDocumentList docs = null;
    String Date_format = null; // for Date格式轉換
    JSONArray jsonArray = new JSONArray();
    try {
      server = new HttpSolrServer(url);
      // add

      SolrQuery solrQuery = new SolrQuery();
      solrQuery.setQuery(query);
      solrQuery.setStart(_start);
      solrQuery.setRows(_nbDocuments);
      solrQuery.setRequestHandler("query");
      solrQuery.set("fl", "*,score"); // 設定fl參數,指定要傳回哪個field的資料,這裡設定所有field與score
      if (_fq1 != "") {
        solrQuery.addFilterQuery("ProductName:" + _fq1);
      }
      if (_fq2 != "") {
        solrQuery.addFilterQuery("publishedDate:" + _fq2);
      }
      if (sort != null) {
        solrQuery.addSortField(sort, ORDER.asc);
      }
      solrQuery.setRequestHandler("/browse");
      response = server.query(solrQuery);
      docs = response.getResults();
      if (docs != null) {
        System.out.println(
            docs.getNumFound() + " documents found, " + docs.size() + " returned : ");
        setResultNumber(docs.getNumFound(), docs.size()); // 設定目前回傳幾筆資料給前端
        for (int i = 0; i < docs.size(); i++) {
          SolrDocument doc = docs.get(i);
          JSONObject jsonObject = new JSONObject();
          for (Iterator<Entry<String, Object>> it2 = doc.iterator(); it2.hasNext(); ) {
            Entry<String, Object> entry = it2.next();
            if (entry.getKey().equals("publishedDate")) { // 將傳回的date格式轉為純字串,方便前端呈現
              Date_format = entry.getValue().toString();
              jsonObject.put(entry.getKey(), Date_format);
            } else {
              // 一般情況
              jsonObject.put(entry.getKey(), entry.getValue());
            }
          }
          System.out.print("\n");
          // 將總共找到幾筆資料存在jsonarray的最後面傳給前端
          jsonObject.put("TotalResultFound", docs.getNumFound());
          jsonArray.add(jsonObject);
        }
      }

    } catch (SolrServerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return jsonArray;
  }
Example #22
0
 /**
  * Returns a MODS record for a given recordIdentifier.
  *
  * @param id a recordIdentifier for a solr document
  * @return the ModsType for this recordidentifier
  * @see ModsType
  */
 public ModsType getMods(String id) throws JAXBException {
   SolrDocumentList docs;
   SolrDocument doc;
   ModsType modsType = new ModsType();
   HttpSolrServer server = null;
   try {
     server = SolrServer.getSolrConnection();
     SolrQuery query = new SolrQuery("recordIdentifier:" + id);
     QueryResponse response = server.query(query);
     docs = response.getResults();
     if (docs.size() == 0) throw new ResourceNotFoundException("Item " + id + " not found");
     else {
       doc = docs.get(0);
       modsType = getModsType(doc);
     }
   } catch (SolrServerException se) {
     se.printStackTrace();
     log.error(se.getMessage());
   }
   return modsType;
 }
  @Override
  public <T> FacetPage<T> queryForFacetPage(FacetQuery query, Class<T> clazz) {
    Assert.notNull(query, "Query must not be 'null'.");
    Assert.notNull(clazz, "Target class must not be 'null'.");

    QueryResponse response = query(query);

    List<T> beans = convertQueryResponseToBeans(response, clazz);
    SolrDocumentList results = response.getResults();
    SolrResultPage<T> page =
        new SolrResultPage<T>(
            beans, query.getPageRequest(), results.getNumFound(), results.getMaxScore());
    page.addAllFacetFieldResultPages(
        ResultHelper.convertFacetQueryResponseToFacetPageMap(query, response));
    page.addAllFacetPivotFieldResult(
        ResultHelper.convertFacetQueryResponseToFacetPivotMap(query, response));
    page.setFacetQueryResultPage(
        ResultHelper.convertFacetQueryResponseToFacetQueryResult(query, response));

    return page;
  }
Example #24
0
 // deprecated - use getModsType instead, rewrite and use this if we decide to wrap
 // the mods document in an <item> tag
 public Item getItem(String id) {
   SolrDocumentList docs;
   SolrDocument doc;
   Item item = new Item();
   HttpSolrServer server = null;
   try {
     server = SolrServer.getSolrConnection();
     SolrQuery query = new SolrQuery("recordIdentifier:" + id);
     QueryResponse response = server.query(query);
     docs = response.getResults();
     if (docs.size() == 0) item = null;
     else {
       doc = docs.get(0);
       item = setItem(doc);
     }
   } catch (SolrServerException se) {
     log.error(se.getMessage());
     se.printStackTrace();
   }
   return item;
 }
  /**
   * Process a query string to search using Solr.
   *
   * @param request
   * @param query
   * @param asAnon
   * @param rs
   * @return
   * @throws SolrSearchException
   */
  private SolrSearchResultSet processSolrQuery(
      SlingHttpServletRequest request, Query query, boolean asAnon)
      throws StorageClientException, AccessDeniedException, SolrServerException {
    String queryString = query.getQueryString();
    // apply readers restrictions.
    if (asAnon) {
      queryString = "(" + queryString + ")  AND readers:" + User.ANON_USER;
    } else {
      Session session =
          StorageClientUtils.adaptToSession(
              request.getResourceResolver().adaptTo(javax.jcr.Session.class));
      if (!User.ADMIN_USER.equals(session.getUserId())) {
        AuthorizableManager am = session.getAuthorizableManager();
        Authorizable user = am.findAuthorizable(session.getUserId());
        Set<String> readers = Sets.newHashSet();
        for (Iterator<Group> gi = user.memberOf(am); gi.hasNext(); ) {
          readers.add(gi.next().getId());
        }
        readers.add(session.getUserId());
        queryString =
            "(" + queryString + ") AND readers:(" + StringUtils.join(readers, " OR ") + ")";
      }
    }

    SolrQuery solrQuery = buildQuery(request, queryString, query.getOptions());

    SolrServer solrServer = solrSearchService.getServer();
    try {
      LOGGER.info("Performing Query {} ", URLDecoder.decode(solrQuery.toString(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
    }
    QueryResponse response = solrServer.query(solrQuery);
    SolrDocumentList resultList = response.getResults();
    LOGGER.info("Got {} hits in {} ms", resultList.size(), response.getElapsedTime());
    return new SolrSearchResultSetImpl(response);
  }
Example #26
0
 private List<Item> parseSearchResults(final SolrDocumentList results) {
   final Iterator<SolrDocument> it = results.iterator();
   final List<Item> items = new ArrayList<Item>();
   while (it.hasNext()) {
     final SolrDocument document = it.next();
     final Map<String, Object> clonedValueMap = new HashMap<String, Object>();
     final Collection<String> fieldNames = document.getFieldNames();
     for (final String fieldName : fieldNames) {
       if (!ignorableFields.contains(fieldName)) {
         clonedValueMap.put(fieldName, document.getFieldValue(fieldName));
       }
     }
     final String json = JsonSerializer.serialize(clonedValueMap);
     if (json != null) {
       items.add(ItemParser.parseItem(json));
     }
   }
   return items;
 }
Example #27
0
  @Test
  public void testGetIdsWithParams() throws Exception {
    SolrDocumentList rsp =
        getSolrClient().getById(Arrays.asList("0", "1", "2"), params(CommonParams.FL, "id"));
    assertEquals(2, rsp.getNumFound());

    assertEquals("1", rsp.get(0).get("id"));
    assertNull("This field should have been removed from the response.", rsp.get(0).get("term_s"));
    assertNull("This field should have been removed from the response.", rsp.get(0).get("term2_s"));

    assertEquals("2", rsp.get(1).get("id"));
    assertNull("This field should have been removed from the response.", rsp.get(1).get("term_s"));
    assertNull("This field should have been removed from the response.", rsp.get(1).get("term2_s"));
  }
Example #28
0
  protected SolrDocumentList readDocuments(XMLStreamReader parser) throws XMLStreamException {
    SolrDocumentList docs = new SolrDocumentList();

    // Parse the attributes
    for (int i = 0; i < parser.getAttributeCount(); i++) {
      String n = parser.getAttributeLocalName(i);
      String v = parser.getAttributeValue(i);
      if ("numFound".equals(n)) {
        docs.setNumFound(Long.parseLong(v));
      } else if ("start".equals(n)) {
        docs.setStart(Long.parseLong(v));
      } else if ("maxScore".equals(n)) {
        docs.setMaxScore(Float.parseFloat(v));
      } else if ("sum".equals(n)) {
        docs.setSum(Double.parseDouble(v));
      } else if ("max".equals(n)) {
        docs.setMax(Double.parseDouble(v));
      } else if ("min".equals(n)) {
        docs.setMin(Double.parseDouble(v));
      }
    }

    // Read through each document
    int event;
    while (true) {
      event = parser.next();
      if (XMLStreamConstants.START_ELEMENT == event) {
        if (!"doc".equals(parser.getLocalName())) {
          throw new RuntimeException(
              "shoudl be doc! " + parser.getLocalName() + " :: " + parser.getLocation());
        }
        docs.add(readDocument(parser));
      } else if (XMLStreamConstants.END_ELEMENT == event) {
        return docs; // only happens once
      }
    }
  }
  private void mergeResponses(ResponseBuilder rb) {
    SolrDocumentList docList = new SolrDocumentList();

    for (ShardRequest sreq : rb.finished) {
      // if shards=shard1,shard2 was used, then  we query both shards for each id and
      // can get more than one response
      for (ShardResponse srsp : sreq.responses) {
        SolrResponse sr = srsp.getSolrResponse();
        NamedList nl = sr.getResponse();
        SolrDocumentList subList = (SolrDocumentList) nl.get("response");
        docList.addAll(subList);
      }
    }

    if (docList.size() <= 1 && rb.req.getParams().getParams("ids") == null) {
      // if the doc was not found, then use a value of null.
      rb.rsp.add("doc", docList.size() > 0 ? docList.get(0) : null);
    } else {
      docList.setNumFound(docList.size());
      rb.rsp.add("response", docList);
    }
  }
  /** SRN-96 */
  @Test
  public void testASCIIFoldingExpansion() throws IOException, SolrServerException {
    this.addJsonString("1", " { \"value\" : \"cafe\" } ");
    this.addJsonString("2", " { \"value\" : \"café\" } ");
    SolrQuery query = new SolrQuery();
    query.setQuery("cafe");
    query.setRequestHandler("keyword");
    query.setIncludeScore(true);

    // should match the two documents, with same score
    QueryResponse response = getWrapper().getServer().query(query);
    SolrDocumentList docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    float score1 = (Float) docList.get(0).getFieldValue("score");
    float score2 = (Float) docList.get(1).getFieldValue("score");
    Assert.assertTrue("Score should be identical", score1 == score2);

    // should match the two documents, but should assign different score
    // id2 should receive better score than id1
    query = new SolrQuery();
    query.setQuery("café");
    query.setRequestHandler("keyword");
    query.setIncludeScore(true);

    response = getWrapper().getServer().query(query);
    docList = response.getResults();
    assertEquals(2, docList.getNumFound());
    if (docList.get(0).getFieldValue("url").equals("id1")) {
      score1 = (Float) docList.get(0).getFieldValue("score");
      score2 = (Float) docList.get(1).getFieldValue("score");
    } else {
      score2 = (Float) docList.get(0).getFieldValue("score");
      score1 = (Float) docList.get(1).getFieldValue("score");
    }
    Assert.assertTrue("id2 should get higher score than id1", score1 < score2);
  }