栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Elasticsearch--搜索

Elasticsearch--搜索

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.搜索结果高亮显示

例如我们在CSDN中搜索一个关键词,关键词在结果中的颜色或亮度会突出展现,我们可以通过样式来设置.

POST /pditems/_search
{
	"query": {
		"multi_match":{
			"query": "手机",
			"fields": ["title", "sell_point"]
		}
	},
	"highlight" : {
        "pre_tags" : [""],
        "post_tags" : [""],
        "fields" : {
            "title" : {},
            "sell_point" : {
              "pre_tags": "",
              "post_tags": ""
            }
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283036.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号