Search in sources :

Example 16 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project canal by alibaba.

the class ES7xTest method init.

@Before
public void init() throws UnknownHostException {
    Settings.Builder settingBuilder = Settings.builder();
    settingBuilder.put("cluster.name", TestConstant.clusterName);
    Settings settings = settingBuilder.build();
    transportClient = new PreBuiltTransportClient(settings);
    String[] hostArray = TestConstant.esHosts.split(",");
    for (String host : hostArray) {
        int i = host.indexOf(":");
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.substring(0, i)), Integer.parseInt(host.substring(i + 1))));
    }
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings) Before(org.junit.Before)

Example 17 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project canal by alibaba.

the class ESTest method init.

@Before
public void init() throws UnknownHostException {
    Settings.Builder settingBuilder = Settings.builder();
    settingBuilder.put("cluster.name", TestConstant.clusterName);
    Settings settings = settingBuilder.build();
    transportClient = new PreBuiltTransportClient(settings);
    String[] hostArray = TestConstant.esHosts.split(",");
    for (String host : hostArray) {
        int i = host.indexOf(":");
        transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.substring(0, i)), Integer.parseInt(host.substring(i + 1))));
    }
}
Also used : PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Settings(org.elasticsearch.common.settings.Settings) Before(org.junit.Before)

Example 18 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project MSEC by Tencent.

the class ESHelper method ClusterAdd.

public void ClusterAdd(ArrayList<String> ips) {
    Logger logger = Logger.getLogger(ESHelper.class);
    TransportClient client = null;
    try {
        Settings settings = Settings.builder().put("cluster.name", cluster_info.getCluster_name()).put("client.transport.sniff", true).build();
        for (String ip : ips) {
            if (client == null) {
                client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
            } else
                client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), default_client_port));
        }
        updateStatus(waitforcluster_message);
        waitForClusterReady(client, ips, ClusterHealthStatus.YELLOW);
    } catch (UnknownHostException e) {
        logger.error(e);
        updateStatus("[ERROR] Connect failed.");
    } catch (IOException e) {
        logger.error(e);
        updateStatus_waitforcluster("[ERROR] Joining cluster failed.");
    } finally {
        if (client != null)
            client.close();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) Logger(org.apache.log4j.Logger) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings)

Example 19 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project xmall by Exrick.

the class SearchServiceImpl method search.

/**
 * 使用QueryBuilder
 * termQuery("key", obj) 完全匹配
 * termsQuery("key", obj1, obj2..)   一次匹配多个值
 * matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
 * multiMatchQuery("text", "field1", "field2"..);  匹配多个字段, field有通配符忒行
 */
@Override
public SearchResult search(String key, int page, int size, String sort, int priceGt, int priceLte) {
    try {
        Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
        SearchResult searchResult = new SearchResult();
        // 设置查询条件
        // 单字段搜索
        QueryBuilder qb = matchQuery("productName", key);
        // 设置分页
        if (page <= 0) {
            page = 1;
        }
        int start = (page - 1) * size;
        // 设置高亮显示
        HighlightBuilder hiBuilder = new HighlightBuilder();
        hiBuilder.preTags("<a style=\"color: #e4393c\">");
        hiBuilder.postTags("</a>");
        hiBuilder.field("productName");
        // 执行搜索
        SearchResponse searchResponse = null;
        if (priceGt >= 0 && priceLte >= 0 && sort.isEmpty()) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).get();
        } else if (priceGt >= 0 && priceLte >= 0 && sort.equals("1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.ASC).get();
        } else if (priceGt >= 0 && priceLte >= 0 && sort.equals("-1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.DESC).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.isEmpty()) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.equals("1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).addSort("salePrice", SortOrder.ASC).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.equals("-1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).addSort("salePrice", SortOrder.DESC).get();
        }
        SearchHits hits = searchResponse.getHits();
        // 返回总结果数
        searchResult.setRecordCount(hits.totalHits);
        List<SearchItem> list = new ArrayList<>();
        if (hits.totalHits > 0) {
            for (SearchHit hit : hits) {
                // 总页数
                int totalPage = (int) (hit.getScore() / size);
                if ((hit.getScore() % size) != 0) {
                    totalPage++;
                }
                // 返回结果总页数
                searchResult.setTotalPages(totalPage);
                // 设置高亮字段
                SearchItem searchItem = new Gson().fromJson(hit.getSourceAsString(), SearchItem.class);
                String productName = hit.getHighlightFields().get("productName").getFragments()[0].toString();
                searchItem.setProductName(productName);
                // 返回结果
                list.add(searchItem);
            }
        }
        searchResult.setItemList(list);
        return searchResult;
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmallException("查询ES索引库出错");
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) SearchHit(org.elasticsearch.search.SearchHit) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) SearchResult(cn.exrick.manager.dto.front.SearchResult) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) XmallException(cn.exrick.common.exception.XmallException) XmallException(cn.exrick.common.exception.XmallException) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchItem(cn.exrick.manager.dto.front.SearchItem) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) SearchHits(org.elasticsearch.search.SearchHits) Settings(org.elasticsearch.common.settings.Settings) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 20 with PreBuiltTransportClient

use of org.elasticsearch.transport.client.PreBuiltTransportClient in project xmall by Exrick.

the class ItemESMessageListener method onMessage.

@Override
public void onMessage(Message message) {
    try {
        // 从消息中取商品id
        TextMessage textMessage = (TextMessage) message;
        log.info("得到消息:" + textMessage.getText());
        String[] text = textMessage.getText().split(",");
        Long itemId = new Long(text[1]);
        // 等待事务提交
        Thread.sleep(1000);
        // 更新索引
        Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
        if ("add".equals(text[0])) {
            // 根据商品id查询商品信息
            SearchItem searchItem = itemMapper.getItemById(itemId);
            String image = searchItem.getProductImageBig();
            if (image != null && !"".equals(image)) {
                String[] strings = image.split(",");
                image = strings[0];
            } else {
                image = "";
            }
            searchItem.setProductImageBig(image);
            IndexResponse indexResponse = client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId())).setSource(jsonBuilder().startObject().field("productId", searchItem.getProductId()).field("salePrice", searchItem.getSalePrice()).field("productName", searchItem.getProductName()).field("subTitle", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("categoryName", searchItem.getCategoryName()).field("cid", searchItem.getCid()).endObject()).get();
        } else if ("delete".equals(text[0])) {
            DeleteResponse deleteResponse = client.prepareDelete(ITEM_INDEX, ITEM_TYPE, String.valueOf(itemId)).get();
        }
        log.info("处理消息成功");
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) IndexResponse(org.elasticsearch.action.index.IndexResponse) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TextMessage(javax.jms.TextMessage) Settings(org.elasticsearch.common.settings.Settings) SearchItem(cn.exrick.manager.dto.front.SearchItem)

Aggregations

PreBuiltTransportClient (org.elasticsearch.transport.client.PreBuiltTransportClient)41 Settings (org.elasticsearch.common.settings.Settings)33 TransportClient (org.elasticsearch.client.transport.TransportClient)25 InetSocketTransportAddress (org.elasticsearch.common.transport.InetSocketTransportAddress)23 TransportAddress (org.elasticsearch.common.transport.TransportAddress)17 UnknownHostException (java.net.UnknownHostException)10 InetSocketAddress (java.net.InetSocketAddress)5 InetAddress (java.net.InetAddress)4 HashMap (java.util.HashMap)4 PostConstruct (javax.annotation.PostConstruct)4 SearchItem (cn.exrick.manager.dto.front.SearchItem)3 IOException (java.io.IOException)3 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 IndexResponse (org.elasticsearch.action.index.IndexResponse)3 Builder (org.elasticsearch.common.settings.Settings.Builder)3 XmallException (cn.exrick.common.exception.XmallException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Logger (org.apache.log4j.Logger)2 ElasticsearchClusterRunner (org.codelibs.elasticsearch.runner.ElasticsearchClusterRunner)2