1.query string search语法
就是将请求的条件加到string的后面,同时也需要掌握+-的原理
搜索test_field中包含hangzhou的数据
GET /test_index/test_type/_search?q=test_field:hangzhou
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.9808292,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 0.9808292,
"_source": {
"test_field": "hangzhou"
}
}
]
}
}
搜索test_field中必须包含hangzhou的数据
GET /test_index/test_type/_search?q=+test_field:hangzhou
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.9808292,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 0.9808292,
"_source": {
"test_field": "hangzhou"
}
}
]
}
}
搜索test_field必须不包含hangzhou的数据
GET /test_index/test_type/_search?q=-test_field:hangzhou
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 11,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "5",
"_score": 1,
"_source": {
"test_field": "taizhou"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 1,
"_source": {
"test_field": "jiaxing"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_score": 1,
"_source": {
"test_field": "zhoushan"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "10",
"_score": 1,
"_source": {
"test_field": "lishui"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "12",
"_score": 1,
"_source": {
"test_field": "jinhua"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 1,
"_source": {
"test_field": "ningbo"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 1,
"_source": {
"test_field": "quzhou"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 1,
"_source": {
"test_field": "zhejiang"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 1,
"_source": {
"test_field": "huzhou"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "3",
"_score": 1,
"_source": {
"test_field": "wenzhou"
}
}
]
}
}
2._all metadata的原理及作用
搜索所有的field,任意一个field包含关键词,就可以搜索出来
GET /test_index/test_type/_search?q=hangzhou
我们在进行搜索 时候,难道是对document中的每一个field都进行一次搜索吗?
不是的。es的_all元数据,在建立索引的时候,我们插入一个document,它里面包含了多各field,此时,es会自动将多个field的值,全部用字符串的方式串连起来,变成一个长的字符串,作为_all field的值,同时建立索引。
后面如果在搜索的时候,没有对某个field指定搜索,就默认搜索_all field ,其中是包含了所有field的值。
例如
{
"name":"jack",
"age":26,
"email":"jack@mail.com",
"address":"zhejiang"
}
那么插入这条数据的时候,es会向_all中插入jack 26 jack@mail.com zhejiang,同时分词后建立倒排索引
以上就是_all metadata的作用