1、构造数据
定义 mapping
PUT url_index
{
"mappings": {
"url_type": {
"properties": {
"url":{
"type": "text"
}
}
}
}
}
# 返回结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "url_index"
}
查看 mapping
GET /url_index/_mapping/url_type
# 返回结果
{
"url_index": {
"mappings": {
"url_type": {
"properties": {
"url": {
"type": "text"
}
}
}
}
}
}
插入数据
PUT /url_index/url_type/1
{
"url": "index.js"
}
PUT /url_index/url_type/2
{
"url": "index.jpg"
}
PUT /url_index/url_type/3
{
"url": "jquery.js"
}
PUT /url_index/url_type/4
{
"url": "index.png"
}
查看数据
GET /url_index/url_type/_search
# 返回结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "url_index",
"_type": "url_type",
"_id": "2",
"_score": 1,
"_source": {
"url": "index.jpg"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "4",
"_score": 1,
"_source": {
"url": "index.png"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "1",
"_score": 1,
"_source": {
"url": "index.js"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "3",
"_score": 1,
"_source": {
"url": "jquery.js"
}
}
]
}
}
2、前缀搜索,搜索以 index 开头的 url
GET /url_index/url_type/_search
{
"query": {
"prefix": {
"url": {
"value": "index"
}
}
}
}
# 返回结果
{
"took": 233,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "url_index",
"_type": "url_type",
"_id": "2",
"_score": 1,
"_source": {
"url": "index.jpg"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "4",
"_score": 1,
"_source": {
"url": "index.png"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "1",
"_score": 1,
"_source": {
"url": "index.js"
}
}
]
}
}
3、通配符搜索, 匹配以 .js 结尾的
GET /url_index/url_type/_search
{
"query": {
"wildcard": {
"url": {
"value": "*.js"
}
}
}
}
# 返回结果
{
"took": 30,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "url_index",
"_type": "url_type",
"_id": "1",
"_score": 1,
"_source": {
"url": "index.js"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "3",
"_score": 1,
"_source": {
"url": "jquery.js"
}
}
]
}
}
4、正则搜索, 匹配以 .js 结尾的
GET /url_index/url_type/_search
{
"query": {
"regexp": {
"url": "[a-z]+\\.js"
}
}
}
# 返回结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "url_index",
"_type": "url_type",
"_id": "1",
"_score": 1,
"_source": {
"url": "index.js"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "3",
"_score": 1,
"_source": {
"url": "jquery.js"
}
}
]
}
}
5、通配符不匹配搜索,不匹配以 .js 结尾的
GET /url_index/url_type/_search
{
"query": {
"bool": {
"must_not": {
"wildcard": {
"url": {
"value": "*.js"
}
}
}
}
}
}
# 返回结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "url_index",
"_type": "url_type",
"_id": "2",
"_score": 1,
"_source": {
"url": "index.jpg"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "4",
"_score": 1,
"_source": {
"url": "index.png"
}
}
]
}
}
6、正则不匹配搜索,不匹配以 .js 结尾的
GET /url_index/url_type/_search
{
"query": {
"bool": {
"must_not": {
"regexp": {
"url":
"[a-z]+\\.js"
}
}
}
}
}
# 返回结果
{
"took": 41,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "url_index",
"_type": "url_type",
"_id": "2",
"_score": 1,
"_source": {
"url": "index.jpg"
}
},
{
"_index": "url_index",
"_type": "url_type",
"_id": "4",
"_score": 1,
"_source": {
"url": "index.png"
}
}
]
}
}