首页 > 其他分享 >elasticsearch的使用

elasticsearch的使用

时间:2023-06-15 10:45:50浏览次数:43  
标签:title doc price elasticsearch 使用 test 文档 id

elasticsearch的使用

索引管理

1、创建索引

对比关系型数据库,创建索引相当于创建数据库

  • url:http:/ip:9200/test

  • 方式:PUT

不允许重复put相同索引

2、获取索引

a、将请求方式改为get即获取当前索引信息

  • url:ip:9200/test

  • 方式:GET

b、获取所有索引信息

  • url:ip:9200/_cat/indices?v

  • 方式:GET

3、删除索引

将请求方式改为delete即删除当前索引信息

  • url:ip:9200/test

  • 方式:DELETE

文档管理

1、创建文档

  • url:ip:9200/test/_doc/1001或者是ip:9200/test/_create/1001的post请求

  • 方式:POST

  • 请求体:{ "title": "小米", "price": 1999 }

若不指定id会自动创建id

2、文档查询

a、主键查询

将请求方式改为get即获取当前文档信息

  • url:ip:9200/test/_doc/1002

  • 方式:GET

b、获取索引中所有文档

  • url:ip:9200/test/_search

  • 方式:GET 请求体必须为空

3、文档修改

a、全部覆盖

  • url:ip:9200/test/_doc/1001

  • 方式:PUT

  • 请求体:{ "title": "小米", "price": 2000 }

b、局部覆盖

  • url:ip:9200/test/_update/1001

  • 方式:POST

  • 请求体:{ "doc":{ "title": "小米", "price": 11 } }

4、文档删除

  • url:ip:9200/test/_doc/1001

  • 方式:DELETE

5、文档参数条件查询

a、路径方式查询

  • url:ip:9200/test/_search?q=title:oppo

  • 方式:GET

返回结果
{
    "took": 409,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.8923234,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.8923234,
                "_source": {
                    "title": "oppo",
                    "price": 2222
                }
            }
        ]
    }
}

b、请求体携带参数方式查询

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:{ "query":{ "match":{ "title":"oppo" } } }

c、请求体携带参数方式查询全部数据

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:{ "query":{ "match_all":{ } } }

6、文档参数分页查询排序

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "query":{
        "match_all":{
        }
    },
    "from":0, // 偏移量
    "size":2, // 查询数量
    "_source":["title"], // 只查询的字段
    "sort":{
        "price":{ //排序字段
            "order": "asc" //排序正序倒序
        }
    }
}

7、文档多条件查询,范围查询

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "query":{
        "bool":{
            "must":[ // must 相当于 and 数组中的条件必须全部满足
                {
                "match":{
                    "title":"oppo"
                }
            },{
                "match":{
                    "price":2222
                }
            }
            ],
            "should":[ // should相当于 or 
                {
                    "match":{
                        "title":"小米"
                    }
                }
            ],
            "filter":{ // 范围查询
                "range":{
                    "price":{
                        "gt":1000
                    }
                }
            }
        }
    }
}

8、文档模糊查询

a、分词模糊查询

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "query":{
        "match":{
            "title":"小米"
        }
    }
}
返回结果
{
    "took": 341,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 2.7208898,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "16",
                "_score": 2.7208898,
                "_source": {
                    "title": "小米",
                    "price": 666
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "17",
                "_score": 2.7208898,
                "_source": {
                    "title": "小米",
                    "price": 777
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "15",
                "_score": 1.8103764,
                "_source": {
                    "title": "op小米po",
                    "price": 55
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "18",
                "_score": 1.254745,
                "_source": {
                    "title": "小红",
                    "price": 777
                }
            }
        ]
    }
}

b、全匹配模糊查询

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "query":{
        "match_phrase":{
            "title":"小米"
        }
    }
}
返回结果
{
    "took": 48,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 2.72089,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "16",
                "_score": 2.72089,
                "_source": {
                    "title": "小米",
                    "price": 666
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "17",
                "_score": 2.72089,
                "_source": {
                    "title": "小米",
                    "price": 777
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "15",
                "_score": 1.8103766,
                "_source": {
                    "title": "op小米po",
                    "price": 55
                }
            }
        ]
    }
}

小红就没了

c、高亮显示

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "query":{
        "match_phrase":{
            "title":"小米"
        }
    },
    "highlight":{
        "fields":{
            "title":{}
        }
    }
}
返回结果
{
    "took": 155,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 2.72089,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "16",
                "_score": 2.72089,
                "_source": {
                    "title": "小米",
                    "price": 666
                },
                "highlight": {
                    "title": [
                        "<em>小</em><em>米</em>"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "17",
                "_score": 2.72089,
                "_source": {
                    "title": "小米",
                    "price": 777
                },
                "highlight": {
                    "title": [
                        "<em>小</em><em>米</em>"
                    ]
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "15",
                "_score": 1.8103766,
                "_source": {
                    "title": "op小米po",
                    "price": 55
                },
                "highlight": {
                    "title": [
                        "op<em>小</em><em>米</em>po"
                    ]
                }
            }
        ]
    }
}

9、文档聚合查询

a、分组

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "aggs":{ //聚合操作
        "price_group":{ // 分钟名称
            "terms":{ // 分组
                "field": "price" // 分组字段
            }
        }
    }
}
返回结果
{
    "took": 111,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 20,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "title": "vivo",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "1002",
                "_score": 1.0,
                "_source": {
                    "title": "华为",
                    "price": 1999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "1001",
                "_score": 1.0,
                "_source": {
                    "title": "oppo",
                    "price": 2222
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "vivo2",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "title": "vivo3",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "title": "vivo4",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "title": "vivo5",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "6",
                "_score": 1.0,
                "_source": {
                    "title": "apple",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "7",
                "_score": 1.0,
                "_source": {
                    "title": "aifeng",
                    "price": 999
                }
            },
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "8",
                "_score": 1.0,
                "_source": {
                    "title": "lisis",
                    "price": 999
                }
            }
        ]
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 999,
                    "doc_count": 8
                },
                {
                    "key": 777,
                    "doc_count": 3
                },
                {
                    "key": 55,
                    "doc_count": 2
                },
                {
                    "key": 11,
                    "doc_count": 1
                },
                {
                    "key": 454,
                    "doc_count": 1
                },
                {
                    "key": 666,
                    "doc_count": 1
                },
                {
                    "key": 1999,
                    "doc_count": 1
                },
                {
                    "key": 2222,
                    "doc_count": 1
                },
                {
                    "key": 3333,
                    "doc_count": 1
                },
                {
                    "key": 4541,
                    "doc_count": 1
                }
            ]
        }
    }
}
可指定size来决定显示多少原始数据

b、平均值

  • url:ip:9200/test/_search

  • 方式:GET

  • 请求体:

{
    "aggs":{ //聚合操作
        "price_avg":{ // 分钟名称
            "avg":{ // 分组
                "field": "price" // 分组字段
            }
        }
    }
}

结果

{
    "aggs":{
        "price_group":{
            "avg":{
                "field": "price"
            }
        }
    },
    "size":0
}

映射关系

1、创建索引

  • url:ip:9200/user

  • 方式:PUT

2、创建映射关系

  • url:ip:9200/user/_mapping

  • 方式:PUT

  • 请求体:

{
    "properties":{
        "name":{ // 字段
            "type":"text", // 分词模糊查询
            "index":"true" // 可索引查询
        },
        "sex":{
            "type":"keyword",
            "index":"true"
        },
        "tel":{
            "type":"keyword",// 全匹配模糊查询查询7
            "index":"false"// 不可索引查询
        }
    }
}

可get查看映射

3、创建文档

4、查询文档

name可分词查,sex全匹配查,tel不可查

javaApi 整合springBoot

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.8.0</version>
</dependency>

配置yml

spring:
  elasticsearch:
      rest:
        uris: ip:9200
        username: elasticsearch
        password: xxx

创建文档类

@Document 指定索引信息

@Field(type = FieldType.Text) 设置分词模糊查询

@Document(indexName = "exam-question-index")
public class ExamQuestionDocument {
    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @Id
    private Long id;

    /**
     * 试题内容以及选项
     */
    @Field(type = FieldType.Text)
    private String qstContentAndOpts;

    public ExamQuestionDocument() {
    }

    public ExamQuestionDocument(Long id, String qstContentAndOpts) {
        this.id = id;
        this.qstContentAndOpts = qstContentAndOpts;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getQstContentAndOpts() {
        return qstContentAndOpts;
    }

    public void setQstContentAndOpts(String qstContentAndOpts) {
        this.qstContentAndOpts = qstContentAndOpts;
    }

    @Override
    public String toString() {
        return "ExamQuestionDocument{" +
                "id=" + id +
                ", qstContentAndOpts='" + qstContentAndOpts + '\'' +
                '}';
    }
}

工具类

@Component
public class ElasticSearchUtil {
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 创建索引
     * @param clazz 包含Document注解并指定索引名称
     * @return 是否创建成功
     */
    public Boolean createIndex(Class<?> clazz){
        // 索引名称只能是lowerCase
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(clazz);
        //创建索引
        if (indexOperations.create()){
            //生成映射
            Document mapping = indexOperations.createMapping();
            //推送映射
            return indexOperations.putMapping(mapping);
        }
        return false;
    }

    /**
     * 获取索引信息
     * @param clazz 包含Document注解并指定索引名称
     * @return 索引信息
     */
    public IndexOperations getIndex(Class<?> clazz){
        return elasticsearchRestTemplate.indexOps(clazz);
    }

    /**
     * 删除索引
     * @param clazz 包含Document注解并指定索引名称
     * @return 是否删除成功
     */
    public Boolean deleteIndex(Class<?> clazz){
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(clazz);
        return indexOperations.delete();
    }

    /**
     * 创建文档
     * @param data 文档数据
     * @param docId 文档id (id存在则会覆盖)
     * @param IndexName 索引名称
     * @return 是否创建成功
     */
    public String createDocument(Object data,String docId,String IndexName){
        IndexQuery indexQuery = new IndexQuery();
        // 设置文档内容
        indexQuery.setObject(data);
        // 设置文档id
        indexQuery.setId(docId);
        // 添加文档到指定索引
        return elasticsearchRestTemplate.doIndex(indexQuery, IndexCoordinates.of(IndexName));
    }

    /**
     * 批量创建文档
     * @param indexQueryList 文档数据
     * @param IndexName 索引名称
     */
    public void createDocument(List<IndexQuery> indexQueryList,String IndexName){
        // 添加文档到指定索引
        elasticsearchRestTemplate.bulkIndex(indexQueryList, IndexCoordinates.of(IndexName));
    }

    /**
     * 根据文档id获取文档
     * @param id 文档id
     * @param indexName 索引名称
     * @param tClass 文档类class
     * @param <T> 文档类型
     * @return 文档
     */
    public <T> T getDocumentById(String id,String indexName,Class<T> tClass){
        IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery();
        idsQueryBuilder.addIds(id);

        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(idsQueryBuilder)
                .build();

        SearchHit<T> itemSearchHit = elasticsearchRestTemplate
                .searchOne(query, tClass, IndexCoordinates.of(indexName));
        assert itemSearchHit != null;
        return itemSearchHit.getContent();
    }

    /**
     * 根据文档id获取指定字段文档
     * @param id 文档id
     * @param indexName 索引名称
     * @param tClass 文档类class
     * @param fields 文档指定获取字段
     * @param <T> 文档类型
     * @return 文档
     */
    public <T> T getDocumentById(String id,String indexName,Class<T> tClass,String... fields){
        IdsQueryBuilder idsQueryBuilder = QueryBuilders.idsQuery();
        idsQueryBuilder.addIds(id);

        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(idsQueryBuilder)
                .withFields(fields)
                .build();

        SearchHit<T> itemSearchHit = elasticsearchRestTemplate
                .searchOne(query, tClass, IndexCoordinates.of(indexName));
        assert itemSearchHit != null;
        return itemSearchHit.getContent();
    }

    /**
     * 删除文档
     * @param id 文档id
     * @param clazz 包含Document注解并指定索引名称
     * @return 是否删除成功
     */
    public String deleteDocument(String id,Class<?> clazz){
        return elasticsearchRestTemplate.delete(id, clazz);
    }

    /**
     * 分词查询指定字段文档
     * @param queryColumn 请求字段
     * @param queryData 模糊查询数据
     * @param indexName 索引名称
     * @param tClass 文档类class
     * @param <T> 文档类型
     * @return 模糊查询返回值
     */
    public <T> SearchHits<T> queryLikeDocumentByField(String queryColumn,String queryData,String indexName,Class<T> tClass){
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        // 多条件查询
        // 分词查询 查询字段需要添加@Field(type = FieldType.Text)注解
        queryBuilder
                .must(QueryBuilders.matchQuery(queryColumn, queryData))
        // 过滤
        // .filter()
        ;
        // 高亮处理, 其实就是拼接<span/>标签, 浏览器会自动解析该标签
        HighlightBuilder.Field highlightField = new HighlightBuilder.Field(queryColumn)
                // 设置为red
                .preTags("<span style=\"color:red\">")
                .postTags("</span>");
        Query query = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                // post_filter, 查询后过滤结果
                // .withFilter(queryBuilder)
                // 分页
                //.withPageable(PageRequest.of(0, 10))
                // 返回字段
                //.withFields("id","qstContent")
                // 排序
                // .withSort()
                // 聚合
                // .addAggregation()
                // 高亮
                .withHighlightFields(highlightField)
                .build();
        return elasticsearchRestTemplate.search(query, tClass, IndexCoordinates.of(indexName));

    }
}

标签:title,doc,price,elasticsearch,使用,test,文档,id
From: https://www.cnblogs.com/hwjShl/p/17478650.html

相关文章

  • 如何生成和使用requirements.txt
    当开发Python项目时,使用第三方库是很常见的。为了确保项目的可移植性和可重复性,通常会将项目所依赖的库及其版本记录在一个名为requirements.txt的文件中。这样,其他人可以通过该文件轻松地安装项目所需的所有库及其指定版本。以下是如何使用pip生成和安装requirements.txt......
  • 使用Spring Boot和H2完全工作的原型
    我们在Spring中使用了很多H2,特别是用于单元测试。但是,我们可能希望拥有一个包含数据的全功能原型,而不是单元测试。H2是完美的候选者。它适用于Spring,与大多数数据库具有很强的语法兼容性,并提供用于检查数据的UI。想象一下面试任务的情景。您希望您的示例开箱即用,尽可能少的配置为审......
  • 为什么软件要使用代码签名证书?
    在当下木马和病毒横行的互联网世界,越来越多的软件被恶意攻击,这一现实状况使得用户开始在下载软件之前验证其真实性。而代码签名证书的作用正在于验证软件的真实来源,它将向用户证明负责该代码的企业或个人的身份,并确认该代码自应用签名以来从未修改过。代码签名的定义代码签名是......
  • docker安装与使用教程
    https://mp.weixin.qq.com/s?__biz=MjM5NTY1MjY0MQ==&mid=2650860524&idx=3&sn=02dfc31d637f70b066a6ef9842beeac5&chksm=bd017ea28a76f7b466773e68f7dab26e65ffae2918c28aa1d87c84acfc54460a7b82aa57279f&scene=27  官方的一键安装方式:curl -fsSL https://ge......
  • C#中使用CAS实现无锁算法
    CAS的基本概念CAS(Compare-and-Swap)是一种多线程并发编程中常用的原子操作,用于实现多线程间的同步和互斥访问。它操作通常包含三个参数:一个内存地址(通常是一个共享变量的地址)、期望的旧值和新值。CompareAndSwap(内存地址,期望的旧值,新值)CAS操作会比较内存地址处的值与期望......
  • 使用cordova
    常用指令提前搭建好node环境使用node安装cordovanpminstall-gcordova创建项目cordovacreateHelloCordovaio.hellocordovaCordovaApp添加插件cordovapluginaddeg:cordovapluginaddcordova-plugin-file添加平台cordovaplatformadd查看平台和插件下的......
  • Elasticsearch专题精讲—— Aggregations —— Metrics aggregations(度量聚合)
    Aggregations——Metricsaggregations(度量聚合)https://www.elastic.co/guide/en/elasticsearch/reference/8.8/search-aggregations-metrics.html#search-aggregations-metricsTheaggregationsinthisfamilycomputemetricsbasedonvaluesextractedinone......
  • 使用sessionStorage获取值和设置值 sessionStorage.setItem('key','value') sessionS
    使用sessionStorage获取值和设置值sessionStorage.setItem('key','value')sessionStorage.getItem('myname')https://www.shuzhiduo.com/A/lk5a4ZL2J1/<body><buttonid="btn1">设置值</button><buttonid="btn2&......
  • 介绍vue3的钩子函数activated和deactivated使用场景
    activated和deactivated是Vue3中的两个生命周期钩子函数。activated钩子函数在组件被激活时调用,通常用于恢复组件的状态或执行一些初始化操作。例如,如果一个组件被从路由中激活,你可能需要在该组件被激活时从本地存储中加载一些数据。下面是一个示例代码:<template><div>......
  • m基于MPC模型预测控制算法的永磁直线同步电机控制系统simulink仿真,MPC分别使用工具箱
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要MPC(ModelPredictiveControl)模型预测控制算法是一种先进的控制算法,能够有效地解决非线性、多变量、约束条件等复杂系统的控制问题。永磁直线同步电机是一种高性能、高效率的电机,广泛应用于机器人、医疗设备、工业......