Elasticsearch--搜索

本文详细介绍了Elasticsearch的搜索功能,包括导入测试数据、创建索引和映射、使用Head插件查看索引、数据导入与查看、关键词搜索、结果过滤器和高亮显示。同时,通过一个搜索框案例展示了如何在实际应用中实现Elasticsearch的搜索功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导入测试数据

测试数据下载

2. 搜索功能样例数据

{ "index": {"_index": "pditems", "_id": "536563"}}
{ "id":"536563","brand":"联想","title":"联想(Lenovo)小新Air13 Pro 13.3英寸14.8mm超轻薄笔记本电脑","sell_point":"清仓!仅北京,武汉仓有货!","price":"6688.0","barcode":"","image":"/images/server/images/portal/air13/little4.jpg","cid":"163","status":"1","created":"2015-03-08 21:33:18","updated":"2015-04-11 20:38:38"}

3.创建索引和映射

PUT /pditems
{
   
  "settings": {
   
    "number_of_shards": 3, 
    "number_of_replicas": 2
  },
  "mappings": {
   
    "properties": {
   
      "id": {
   
        "type": "long"
      },
      "brand": {
   
        "type": "text",
        "analyzer": "ik_smart"
      },
      "title": {
   
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "sell_point": {
   
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "price": {
   
        "type": "float"
      },
      "image": {
   
        "type": "keyword"
      },
      "cid": {
   
        "type": "long"
      },
      "status": {
   
        "type": "byte"
      },
      "created": {
   
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "updated": {
   
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    } 
  }
}

说明:“number_of_shards”: 3 ,创建分片数为3, “number_of_replicas”: 2 ,每个分片创建两个副本.mappings映射,properties映射所有字段.以及规定了搜索的方式.
在Kibana工具中执行上述代码,创建索引.

4.用head查看索引

在这里插入图片描述
边框粗的代表是主控,下边数字对应的是对应主控的副本,当主服务器宕机,则会从副本中进行数据操作.

5.导入数据

在服务器上,进入 pditems.json 所在的文件夹,执行批量数据导入:

curl -XPOST 'localhost:9200/pditems/_bulk' \
    -H 'Content-Type:application/json' \
    --data-binary @pditems.json

6.查看数据

搜索 pditems 索引中全部 3160 条数据:

GET /pditems/_search
{
   
  "query": {
   
    "match_all": {
   }
  },
  "size": 3160
}

7.搜索所有数据

搜索 pditems 索引中全部数据

POST /pditems/_search
{
   
  "query": {
   
    "match_all": {
   }
  }
}

8.关键词搜索

# 查询 pditems 索引中title中包含"电脑"的商品
POST /pditems/_search
{
   
  "query": {
   
    "match": {
   
      "title": "电脑"
    }
  }
}

9.搜索结果过滤器

多个条件搜索,需要对结果进行过滤,例如:搜索价格大于2000,并且title中包含"电脑"的商品.我们搜索时"电脑"设置必须匹配,"价格大于2000"是一个范围,范围用range来表示,gte表示大于.

# 价格大于2000,并且title中包含"电脑"的商品
POST /pditems/_search
{
   
  "query": {
   
    "bool": {
   
      "must": [
        {
   
          "match": {
   
            "title": "电脑"
          }
        }
      ],

      "filter": [
        {
   
          "range": {
   
            "price": {
   
              "gte": "2000"
            }
          }
        }
      ]
    }
  }
}

10.搜索结果高亮显示

例如我们在优快云中搜索一个关键词,关键词在结果中的颜色或亮度会突出展现,我们可以通过样式来设置.
在这里插入图片描述

POST /pditems/_search
{
   
	"query": {
   
		"multi_match":{
   
			"query": "手机",
			"fields": ["title", "sell_point"]
		}
	},
	"highlight" : {
   
        "pre_tags" : ["<i class=\"highlight\">"],
        "post_tags" : ["</i>"],
        "fields" : {
   
            "title" : {
   },
            "sell_point" : {
   
              "pre_tags": "<em>",
              "post_tags": "</em>"
            }
        }
    }
}

11.Elasticsearch实现搜索框案例:

a) 添加ItemRepository类,设置高亮显示搜索结果

package com.pd.es;

import com.pd.pojo.Item;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Highlight;
import org.springframework.data.elasticsearch.annotations.HighlightField;
import org.springframework.data.elasticsearch.annotations.HighlightParameters;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

public interface ItemRepository
        extends ElasticsearchRepository<Item, Long> {
   
    @Highlight(
            parameters = @HighlightParameters(
                    preTags = "<em>",
                    postTags = "</em>"
            ),
            fields = @HighlightField(name="title")
    )
    List<SearchHit<Item>> findByTitleOrSellPoint(String key1, String key2, Pageable pageable);
}

b)新建service接口

package com.pd.service;

import com.pd.pojo.Item;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.SearchHit;

import java.util.List;

public interface SearchService {
   
    List<SearchHit<Item>> search(String key, Pageable pageable);
}

c)新建controller

package com.pd.controller;

import com.pd.pojo.Item;
import com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗人在流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值