elasticsearch使用

加上允许跨域设置:

http.cors.enabled: true
http.cors.allow-origin: "*"

 然后去elasticsearch-head-master文件中,cmd,启动控制es(elasticsearch)的网页,类似navicat软件一样。

  • npm install
  • npm run start

 然后再启动kibana,这个就是一个专门查询,添加es的项目:

这个更简单,直接点击启动:

不过启动后时英文的,可以配置为中文:

i18n.locale: "zh-CN" 

 ik分词器:

下载下来后解压到plugins目录中:

然后运行:kibana

ik分词器两种操作:

1、ik_max_word 最细粒度

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "我爱中国共产党"
}
 

2、ik_smart 最粗粒度

#插入数据
POST /sys_user1/_doc
{
  "name":"qx",
  "age":18
}

#创建索引规则(建库)
PUT /sys_user1
{
  "mappings": {
    "properties": {
      "user_type": {
        "type": "text"
      },
      "user_name":{
        "type": "text"
      },
      "user_age":{
        "type":"long"
      }
    }
  }
}

GET /sys_user1

PUT /test3/_doc/1
{
"name":"qx1",
"age":20
}

GET /test3

#修改1
PUT /shujuku/type1/t3
{
  "name":"qx2",
  "age":182,
  "gender":12
}
#修改2
POST /shujuku/_update/t3
{
  "doc": {
    "name": "qx942"
  }
}
#删除
DELETE /shujuku/_doc/t3


GET /shujuku/type1/_search
{
  "query": {
    "match": {
      "name": "qx"
    }
  },
  指定显示字段
  "_source": ["name","age"],
  排序
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  分页
  "from": 0,
  "size": 2
}


GET shujuku/type1/_search
{
  "query": {
    "bool": {
      or查询
      "should": [
        {
          "match": {
            "name": "qx"
          }
        },
        {
          "match": {
            "age": 20
          }
        }
      ]
    }
  }
}



GET shujuku/type1/_search
{
  "query": {
    "bool": {
      and查询
      "must": [
        {
          "match": {
            "name": "qx"
          }
        },
        {
          "match": {
            "age": 20
          }
        }
      ]
    }
  }
}



GET shujuku/type1/_search
{
  "query": {
    "bool": {
范围查询,age大于20,小于300
      "filter": {
        "range": {
          "age": {
            "gt": 20,
            "lt": 300
          }
        }
      }
    }
  },
  "_source": [
    "age"
  ]
}



match查询时text类型会被分词器解析,
GET querytest/_search
{
  "query": {
    "match": {
      "name":"瞿肖"
    }
  }
}

match查询时keyword类型也是精确查询
GET querytest/_search
{
  "query": {
    "match": {
      "desc":"你说得没错"
    }
  }
}

term只能和keyword配合
GET querytest/_search
{
  "query": {
    "term": {
      "desc":"你说得没错"
    }
  }
}



GET querytest/_search
{
  "query": {
    "match": {
      "age":20
    }
  },
  "highlight": {
    "fields": {
      "name": {},
      "age":P{
    },
    高亮前缀
        "pre_tags": "<span style='color: red'>",
    高亮后缀
    "post_tags": "</span>"
  }
}



GET querytest/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "desc": "你说得没错"
          }
        },
        {
          "match": {
            "name": "瞿肖"
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "desc": {},
      "name": {}
    },
    "pre_tags": "<span style='color: red'>",
    "post_tags": "</span>"
  }
}

jav

a集成es操作:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>elasticsearchDom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <elasticsearch.version>7.6.1</elasticsearch.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--        java 操作es框架-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.25</version>
        </dependency>
        <!--        生成uuid数字工具包-->
        <!--        UUID.fromString(Generators.timeBasedGenerator().generate().toString()).node()-->
        <dependency>
            <groupId>com.fasterxml.uuid</groupId>
            <artifactId>java-uuid-generator</artifactId>
            <version>3.1.4</version>
        </dependency>

    </dependencies>
</project>

然后直接将客户端配置到bean中,然后使用它:

package com.es.server;

import com.es.entity.ItemES;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: elasticsearchDom
 * @author: quxiao
 * @create: 2024-04-04 09:55
 **/
@Service
public interface ItemServer extends ElasticsearchRepository<ItemES, String> {
    /**
     * 查询名称或描述
     */
    List<ItemES> findByNameOrDesc(String name, String desc);

    /**
     * 查询名称 并且分页
     */
    Page<ItemES> findByName(String name, Pageable pageable);

    /**
     * 查询价格在min-max之间的商品
     */
    List<ItemES> findByPriceBetween(Double min, Double max);

    /**
     * 查询价格在min-max之间的商品,并且按照价格降序
     */
    List<ItemES> findByPriceBetweenOrderByPriceDesc(Double min, Double max);
}
基础的增删改查也实现了:
springBoot约定大于配置又开始了,只要我们按照Date Elasticsearch 框架约定的方法名方式,就可以快速的写好对es的操作代码。
​​​​​​​官方文档:​​​​​​​Query methods :: Spring Data Elasticsearch

相关推荐

  1. Elasticsearch-Kibana使用教程

    2024-04-06 18:06:03       29 阅读
  2. ElasticSearch高阶使用

    2024-04-06 18:06:03       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-06 18:06:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-06 18:06:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-06 18:06:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-06 18:06:03       20 阅读

热门阅读

  1. JVM基础

    JVM基础

    2024-04-06 18:06:03      15 阅读
  2. Node.js命令介绍

    2024-04-06 18:06:03       18 阅读
  3. 说明计算机视觉(CV)技术的优势和挑战

    2024-04-06 18:06:03       20 阅读
  4. 零日攻击测试

    2024-04-06 18:06:03       25 阅读
  5. 如何解决Docker镜像仓库认证失败的问题?

    2024-04-06 18:06:03       12 阅读
  6. NumPy 数组对象

    2024-04-06 18:06:03       18 阅读
  7. 星弟专享面试题汇总(星弟亲自总结)

    2024-04-06 18:06:03       19 阅读