寻梦csdn 2019-09-06 12:27 采纳率: 0%
浏览 183

elasticsearch5.6查询没有elasticsearch2.3.3快,为什么

elasticsearch5.6

索引模板

curl cmserver:9200/track_20190809?pretty
{
  "track_20190809" : {
    "aliases" : { },
    "mappings" : {
      "track" : {
        "_all" : {
          "enabled" : false
        },
        "properties" : {
          "dataSource" : {
            "type" : "keyword"
          },
          "dtTime" : {
            "type" : "date"
          },
          "idtype" : {
            "type" : "keyword"
          },
          "linkId" : {
            "type" : "keyword"
          },
          "strEqpId" : {
            "type" : "keyword"
          },
          "strId" : {
            "type" : "keyword"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1566555759568",
        "number_of_shards" : "4",
        "number_of_replicas" : "0",
        "uuid" : "X0e_Xj-ERB--GJedj6hsJw",
        "version" : {
          "created" : "5061699"
        },
        "provided_name" : "track_20190809"
      }
    }
  }
}

maven

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.9.RELEASE</version>
</parent>
<dependencies>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <exclusions>
                <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                        </exclusion>
                </exclusions>
        </dependency>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>5.6.16</version>
        </dependency>
        <dependency>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>transport</artifactId>
                <version>5.6.16</version>
        </dependency>
        <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
        </dependency>
</dependencies>

客户端创建

public class ESRepository {
    public TransportClient client = null;
    public void init() throws Exception {
        buildClient();
    }
    private void buildClient() throws Exception {
        String[] hosts = { "192.168.0.82" };
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("client.transport.sniff", true)
                .put("client.transport.ping_timeout", "60s").put("action.auto_create_index", true).build();
        client = new PreBuiltTransportClient(settings);
        for (String strTransportHostName : hosts) {
            client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(strTransportHostName), 9300));
        }
    }
}

查询代码

public class QueryData {
    private ESRepository esr = new ESRepository();
    private int total = 0;
    public void doRun(String query) {
        try {
            esr.init();
        } catch (Exception e) {
            log.error("", e);
        }
        log.info("query data start...");
        long a = System.currentTimeMillis();
        String scrollId = getScrollId(query);
        queryByScrollId(scrollId);
        clearScroll(scrollId);
        long b = System.currentTimeMillis();
        log.info("query data end total:" + total + ", time:" + (b-a));
    }
    private void queryByScrollId(String scrollId) {
        SearchScrollRequestBuilder searchScrollRequestBuilder;
        SearchResponse response;
        int i = 0;
        while(true) {
            i++;
            searchScrollRequestBuilder = esr.client.prepareSearchScroll(scrollId);
            searchScrollRequestBuilder.setScroll(TimeValue.timeValueMinutes(5));
            response = searchScrollRequestBuilder.get();
            long count = response.getHits().getHits().length;
            log.info("--------- queryByScrollId size {}, i={}", count, i);
            total += count;
            if (count == 0) {
                break;
            } else {
                /*for (SearchHit searchHit : searchHits) {
                    log.info("--------- queryByScrollId source {}", searchHit.getSourceAsString());
                }*/
            }
            scrollId = response.getScrollId();
        }
    }
    private String getScrollId(String query) {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if(query != null) {
            queryBuilder.must(QueryBuilders.wildcardQuery("strId", "*" + query + "*"));
        }
        SearchRequestBuilder searchRequestBuilder = esr.client.prepareSearch("track_20190809");
        searchRequestBuilder.setTypes("track");
        searchRequestBuilder.setScroll(TimeValue.timeValueMinutes(5));
        searchRequestBuilder.setSearchType(SearchType.DEFAULT);
        searchRequestBuilder.setSize(10000);
        //searchRequestBuilder.addSort(SortBuilders.fieldSort("strId"));
        if(query != null) {
            searchRequestBuilder.setQuery(queryBuilder);
        }
        SearchResponse response = searchRequestBuilder.get();
        long count = response.getHits().getHits().length;
        log.info("--------- getScrollId size {}", count);
        total += count;
        /*for (SearchHit searchHit : searchHits) {
            log.info("--------- getScrollId source {}", searchHit.getSourceAsString());
        }*/
        return response.getScrollId();
    }
    private void clearScroll(String scrollId) {
        ClearScrollRequestBuilder clearScrollRequestBuilder = esr.client.prepareClearScroll();
        clearScrollRequestBuilder.addScrollId(scrollId);
        ClearScrollResponse response = clearScrollRequestBuilder.get();
        log.info("--------- clearScroll result {}", response.isSucceeded());
    }
}

elasticsearch2.3

索引模板

curl cmserver:9200/track_20190809?pretty
{
  "track_20190809" : {
    "aliases" : { },
    "mappings" : {
      "track" : {
        "_all" : {
          "enabled" : false
        },
        "properties" : {
          "dataSource" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "dtTime" : {
            "type" : "date",
            "format" : "strict_date_optional_time||epoch_millis"
          },
          "idtype" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "linkId" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "strEqpId" : {
            "type" : "string",
            "index" : "not_analyzed"
          },
          "strId" : {
            "type" : "string",
            "index" : "not_analyzed"
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1566465473765",
        "number_of_shards" : "4",
        "number_of_replicas" : "0",
        "uuid" : "MfT_bzy1TO-67aVocEt5uQ",
        "version" : {
          "created" : "2030399"
        }
      }
    }
  }
}

maven

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

查询代码

和elasticsearch5.6一样

两个es内存配置都为1G,数据总条数1000万
全部查出来,5.6需要300多秒,而2.3需要130秒左右,不清楚为什么,求大神帮忙

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀