Search in sources :

Example 76 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project e3mall by colg-cloud.

the class TbItemServiceImpl method importAllItems.

@Override
public E3Result importAllItems() {
    try {
        // 查询所有商品列表
        List<TbItemSerach> list = tbItemMapper.getItemList();
        // 遍历商品列表
        for (TbItemSerach serach : list) {
            // 创建文档对象
            SolrInputDocument doc = new SolrInputDocument();
            // 向文档对象中添加域
            doc.addField("id", serach.getId());
            doc.addField("item_title", serach.getTitle());
            doc.addField("item_sell_point", serach.getSellPoint());
            doc.addField("item_price", serach.getPrice());
            doc.addField("item_image", serach.getImage());
            doc.addField("item_category_name", serach.getCategoryName());
            // 把文档对象写入索引库
            solrServer.add(doc);
        }
        // 提交
        solrServer.commit();
        // 返回导入成功
        return E3Result.ok();
    } catch (SolrServerException e) {
        e.printStackTrace();
        return E3Result.fail(500, "SolrServer 通信/解析异常!");
    } catch (IOException e) {
        e.printStackTrace();
        return E3Result.fail(500, "SolrServer I/O异常!");
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) TbItemSerach(cn.e3mall.common.vo.TbItemSerach) IOException(java.io.IOException)

Example 77 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project e3mall by colg-cloud.

the class TbItemServiceImpl method addDocument.

@Override
public E3Result addDocument(Long id) {
    // 根据商品id查询商品信息
    TbItemSerach serach = tbItemMapper.getItemById(id);
    if (serach != null) {
        // 创建一个文档对象
        SolrInputDocument doc = new SolrInputDocument();
        // 向文档对象中添加域
        doc.addField("id", serach.getId());
        doc.addField("item_title", serach.getTitle());
        doc.addField("item_sell_point", serach.getSellPoint());
        doc.addField("item_price", serach.getPrice());
        doc.addField("item_image", serach.getImage());
        doc.addField("item_category_name", serach.getCategoryName());
        // 把文档写入索引库
        try {
            solrServer.add(doc);
            // 提交
            solrServer.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
            return E3Result.fail(500, "SolrServer 通信/解析异常!");
        } catch (IOException e) {
            e.printStackTrace();
            return E3Result.fail(500, "SolrServer I/O异常!");
        }
    }
    return E3Result.ok();
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) TbItemSerach(cn.e3mall.common.vo.TbItemSerach) IOException(java.io.IOException)

Example 78 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project qi4j-sdk by Qi4j.

the class SolrEntityQueryMixin method findEntities.

@Override
public Iterable<EntityReference> findEntities(Class<?> resultType, @Optional Specification<Composite> whereClause, @Optional OrderBy[] orderBySegments, @Optional Integer firstResult, @Optional Integer maxResults, Map<String, Object> variables) throws EntityFinderException {
    try {
        QuerySpecification expr = (QuerySpecification) whereClause;
        SolrServer server = solr.solrServer();
        NamedList<Object> list = new NamedList<Object>();
        list.add("q", expr.query());
        list.add("rows", maxResults != 0 ? maxResults : 10000);
        list.add("start", firstResult);
        if (orderBySegments != null && orderBySegments.length > 0) {
            for (OrderBy orderBySegment : orderBySegments) {
                String propName = ((Member) orderBySegment.property().accessor()).getName() + "_for_sort";
                String order = orderBySegment.order() == OrderBy.Order.ASCENDING ? "asc" : "desc";
                list.add("sort", propName + " " + order);
            }
        }
        SolrParams solrParams = SolrParams.toSolrParams(list);
        logger.debug("Search:" + list.toString());
        QueryResponse query = server.query(solrParams);
        SolrDocumentList results = query.getResults();
        List<EntityReference> references = new ArrayList<EntityReference>(results.size());
        for (SolrDocument result : results) {
            references.add(EntityReference.parseEntityReference(result.getFirstValue("id").toString()));
        }
        return references;
    } catch (SolrServerException e) {
        throw new EntityFinderException(e);
    }
}
Also used : OrderBy(org.qi4j.api.query.grammar.OrderBy) NamedList(org.apache.solr.common.util.NamedList) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrServer(org.apache.solr.client.solrj.SolrServer) QuerySpecification(org.qi4j.api.query.grammar.QuerySpecification) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) EntityFinderException(org.qi4j.spi.query.EntityFinderException) EntityReference(org.qi4j.api.entity.EntityReference) SolrParams(org.apache.solr.common.params.SolrParams)

Example 79 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project beam by apache.

the class SolrIOTest method testDefaultRetryPredicate.

/**
 * Tests predicate performs as documented.
 */
@Test
public void testDefaultRetryPredicate() {
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new IOException("test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new SolrServerException("test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.CONFLICT, "test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.SERVER_ERROR, "test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.INVALID_STATE, "test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.UNKNOWN, "test")));
    assertTrue(DEFAULT_RETRY_PREDICATE.test(new HttpSolrClient.RemoteSolrException("localhost", SolrException.ErrorCode.SERVICE_UNAVAILABLE.code, "test", new Exception())));
    assertFalse(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.BAD_REQUEST, "test")));
    assertFalse(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.FORBIDDEN, "test")));
    assertFalse(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.NOT_FOUND, "test")));
    assertFalse(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.UNAUTHORIZED, "test")));
    assertFalse(DEFAULT_RETRY_PREDICATE.test(new SolrException(SolrException.ErrorCode.UNSUPPORTED_MEDIA_TYPE, "test")));
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Test(org.junit.Test)

Example 80 with SolrServerException

use of org.apache.solr.client.solrj.SolrServerException in project beam by apache.

the class SolrIOTestUtils method commitAndGetCurrentNumDocs.

/**
 * Forces a commit of the given collection to make recently inserted documents available for
 * search.
 *
 * @return The number of docs in the index
 */
static long commitAndGetCurrentNumDocs(String collection, AuthorizedSolrClient client) throws IOException {
    SolrQuery solrQuery = new SolrQuery("*:*");
    solrQuery.setRows(0);
    try {
        UpdateRequest update = new UpdateRequest();
        update.setAction(UpdateRequest.ACTION.COMMIT, true, true);
        client.process(collection, update);
        return client.query(collection, new SolrQuery("*:*")).getResults().getNumFound();
    } catch (SolrServerException e) {
        throw new IOException(e);
    }
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Aggregations

SolrServerException (org.apache.solr.client.solrj.SolrServerException)281 IOException (java.io.IOException)210 SolrQuery (org.apache.solr.client.solrj.SolrQuery)101 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)97 ArrayList (java.util.ArrayList)58 SolrException (org.apache.solr.common.SolrException)57 SolrDocument (org.apache.solr.common.SolrDocument)55 SolrInputDocument (org.apache.solr.common.SolrInputDocument)50 SolrDocumentList (org.apache.solr.common.SolrDocumentList)44 HashMap (java.util.HashMap)30 Map (java.util.Map)29 List (java.util.List)27 UpdateResponse (org.apache.solr.client.solrj.response.UpdateResponse)26 SolrClient (org.apache.solr.client.solrj.SolrClient)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)23 NamedList (org.apache.solr.common.util.NamedList)22 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)19 Date (java.util.Date)18 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)17 SolrParams (org.apache.solr.common.params.SolrParams)13