欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

es742&ka742

程序员文章站 2022-05-16 18:36:17
...

1 部署elasticsearch

1.1 创建elasticsearch数据存储目录 

rm -rf /mydata/elasticsearch
mkdir -p /mydata/elasticsearch/config /mydata/elasticsearch/data /mydata/elasticsearch/plugins/ik
echo "http.host: 0.0.0.0" >> /mydata/elasticsearch/config/elasticsearch.yml
### 保证权限(有目录映射可能会出现非root用户没用访问权限)
chmod -R 777 /mydata/elasticsearch/ 

1.2 下载IK安装分词器

### 下载
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.2/elasticsearch-analysis-ik-7.4.2.zip
### 解压
unzip elasticsearch-analysis-ik-7.4.2.zip -d /mydata/elasticsearch/plugins/ik/
### 删除zip文件
rm -rf elasticsearch-analysis-ik-7.4.2.zip

1.3 启动elasticsearch

docker run \
--name es742 \
-p 9200:9200 \
-p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.4.2

1.4 查看es是否启动 

[[email protected] ~]# curl http://127.0.0.1:9200/
{
  "name" : "db03fad7d132",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "5SfOiSX-SJmxIFAuuv9Rwg",
  "version" : {
    "number" : "7.4.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
    "build_date" : "2019-10-28T20:40:44.881551Z",
    "build_snapshot" : false,
    "lucene_version" : "8.2.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2 部署kibana

docker run \
--name kb742 \
-p 5601:5601 \
-e ELASTICSEARCH_HOSTS=http://10.4.42.21:9200 \
-e "I18N_LOCALE=zh-CN" \
-d kibana:7.4.2
### kibana上测试分词器
POST _analyze
{
    "analyzer": "ik_smart",
    "text": "我是中国人"
}

3 访问

elasticsearch:http://10.4.42.21:9200/,kiban:http://10.4.42.21:5601/app/kibana


4 测试

4.1 插入数据

### 创建book索引
PUT /book/
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "description":{
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "studymodel":{
        "type": "keyword"
      },
      "price":{
        "type": "double"
      },
      "timestamp": {
         "type": "date",
         "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      },
      "pic":{
        "type":"text",
        "index":false
      }
    }
  }
}
### 插入数据
PUT /book/_doc/1
{
	"name": "Bootstrap开发",
	"description": "Bootstrap是由Twitter推出的一个前台页面开发css框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长css页面开发的程序人员)轻松的实现一个css,不受浏览器限制的精美界面css效果。",
	"studymodel": "201002",
	"price":38.6,
	"timestamp":"2019-08-25 19:11:35",
	"pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
	"tags": [ "bootstrap", "dev"]
}

PUT /book/_doc/2
{
	"name": "java编程思想",
	"description": "java语言是世界第一编程语言,在软件开发领域使用人数最多。",
	"studymodel": "201001",
	"price":68.6,
	"timestamp":"2019-08-25 19:11:35",
	"pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
	"tags": [ "java", "dev"]
}

PUT /book/_doc/3
{
	"name": "spring开发基础",
	"description": "spring 在java领域非常流行,java程序员都在用。",
	"studymodel": "201001",
	"price":88.6,
	"timestamp":"2019-08-24 19:11:35",
	"pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg",
	"tags": [ "spring", "java"]
}

4.2 curl查询 

curl -XPOST http://10.4.42.21:9200/book/_search -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "studymodel": "201001"
    }
  }
}'

4.3 springboot 

<properties>
    <java.version>8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.2.0.RELEASE</spring-boot.version>
    <es.version>7.3.0</es.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <!-- boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- es客户端 -->
    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>${es.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${es.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>${es.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.16</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
/**
 * https://www.jb51.net/article/197673.htm
 */
@Slf4j
public class TestSearchJsonOutput {
    private RestHighLevelClient restHighLevelClient;
    @Before
    public void init() {restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(ES_HOST, ES_PORT, "http")));}
    private static final com.fasterxml.jackson.databind.ObjectMapper objectMapper = new com.fasterxml.jackson.databind.ObjectMapper();
    private static final String ES_INDEX = "book";
    private static final String ES_HOST = "10.4.42.21";
    private static final Integer ES_PORT = 9200;
    @Test
    public void test_search() throws IOException {
        //1、构建搜索请求
        SearchRequest searchRequest = new SearchRequest(ES_INDEX);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("studymodel", "201001"));
        boolQueryBuilder.should(QueryBuilders.rangeQuery("price").lte("90"));

        String[] includes = {"name", "description", "price"};
        searchSourceBuilder.fetchSource(includes, new String[]{});
        searchSourceBuilder.query(boolQueryBuilder);
        //2、执行搜索
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        logCurlString(searchSourceBuilder);
        //3、获取结果
        SearchHits hits = searchResponse.getHits();
        //数据数据
        SearchHit[] searchHits = hits.getHits();
        //for (SearchHit searchHit : searchHits) {
        //    Map<String, Object> source = searchHit.getSourceAsMap();
        //    System.out.println("res => \n" + JSON.toJSONString(source, true));
        //}
    }
    private void logCurlString(SearchSourceBuilder searchSourceBuilder) {
        String body = getPrettyFormat(searchSourceBuilder.toString());
        String pattern = "\ncurl -XPOST -H 'Content-Type: application/json' http://{}:{}/{}/_search?pretty -d'\n{}'";
        log.info(pattern, ES_HOST, ES_PORT, ES_INDEX, body);
    }
    /**
     * 格式化输出JSON字符串
     *
     * @return 格式化后的JSON字符串
     */
    private static String getPrettyFormat(String json) {
        String ret = "";
        try {
            Object obj = objectMapper.readValue(json, Object.class);
            ret = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (JsonProcessingException ignored) {}
        return ret;
    }
}

 

相关标签: ES

推荐阅读