1、query string基础语法
查询包含test_field:test的数据:
GET /test_index/test_type/_search?q=test_field:test 或
GET /test_index/test_type/_search?q=+test_field:test
---------------------------------结果---------------------------------
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.25316024,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 0.25316024,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 0.25316024,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 0.25316024,
"_source": {
"test_field": "test service 1"
}
}
]
}
}
查询不包含test_field:test的数据:
GET /test_index/test_type/_search?q=-test_field:test
---------------------------------结果---------------------------------
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 1,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "AW9QyvfMvHutHJSW2Hir",
"_score": 1,
"_source": {
"test_content": "my test_content automatic"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "11",
"_score": 1,
"_source": {
"num": 0,
"tags": [
"tag1"
]
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "2",
"_score": 1,
"_source": {
"test_field2": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "5",
"_score": 1,
"_source": {
"test_field1": "test client 5",
"test_field2": "test service 5"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "9",
"_score": 1,
"_source": {
"test_field1": "test1",
"test_field2": "test update test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "3",
"_score": 1,
"_source": {
"test_field1": "test client 3",
"test_field2": "test update 3"
}
}
]
}
}
2、_all metadata的原理和作用
直接可以搜索所有的field,任意一个field包含指定的关键字就可以搜索出来。
GET /test_index/test_type/_search?q=test
我们在进行中搜索的时候,难道是对document中的每一个field都进行一次搜索吗?
不是的,es中的_all元数据,在建立索引的时候,我们插入一条document,它里面包含了多个field,此时,es会自动将多个field的值,全部用字符串的方式串联起来,变成一个长的字符串,作为_all field的值,同时建立索引
后面如果在搜索的时候,没有对某个field指定搜索,就默认搜索_all field,其中是包含了所有field的值的。
举例:
{
"name": "jack",
"age": 26,
"email": "jack@sina.com",
"address": "guamgzhou"
}
"jack 26 jack@sina.com guangzhou"作为这一条document的_all field的值,同时进行分词后建立对应的倒排索引,如果进行_all搜索,会对这个_all field进行搜索。
生产环境不使用。
网友评论