30.初识搜索引擎_快速掌握query string search语法以及_all metadata原理

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进行搜索。
生产环境不使用。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值