Elasticsearch教程:返回结果过滤 _source_includes和_source_excludes使用

默认情况下,ES查询时返回所有字段的,但是有时,我们只想返回个别字段,或者不想返回某几个字段,这个时候就可以用到_source_includes和_source_excludes。
从字面上看_source_includes就是包含的字段,_source_excludes就是屏蔽的字段。
1. 造点测试数据
PUT /pigg_test/_doc/1
{
  "name": "冬哥",
  "age": 33,
  "interest": ["music", "food"]
}

PUT /pigg_test/_doc/2
{
  "name": "珣爷",
  "age": 28,
  "interest": ["music", "sleep"],
  "about": "I am a tester"
}

PUT /pigg_test/_doc/3
{
  "name": "胖丫",
  "age": 3,
  "interest": ["food", "sleep"],
  "about": "I am a girl"
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2. _source参数
2.1 根据id查询时,只返回_source
先看下默认查询id=1时的返回

GET /pigg_test/_doc/1

返回:
{
  "_index" : "pigg_test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "冬哥",
    "age" : 33,
    "interest" : [
      "music",
      "food"
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
再看下加了_source参数的返回

GET /pigg_test/_doc/1/_source

ES版本到7.x后,得这么写
GET /pigg_test/_source/1

只是返回了_source里的内容,不返回_index,_type,_version等信息
{
  "name" : "冬哥",
  "age" : 33,
  "interest" : [
    "music",
    "food"
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2.2 设置_source=false,则不返回_source
默认情况下_source=true,如果不想返回_source,需要指定_source=false

GET /pigg_test/_doc/1?_source=false

返回如下,结果中没有了_source
{
  "_index" : "pigg_test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true
}
1
2
3
4
5
6
7
8
9
10
11
12
3. 在请求体中设置_source_includes和_source_excludes
3.1 _source_includes:只返回部分字段
GET pigg_test/_search
{
  "query": {
    "term": {
      "interest": {
        "value": "food"
      }
    }
  },
  "_source": {
    "includes": ["name", "interest"]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
返回的_source中只包含 “music”, “food”

...省略别的信息...
    "hits" : [
      {
        "_index" : "pigg_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "interest" : [
            "music",
            "food"
          ],
          "name" : "冬哥"
        }
      },
      {
        "_index" : "pigg_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.4700036,
        "_source" : {
          "interest" : [
            "food",
            "sleep"
          ],
          "name" : "胖丫"
        }
      }
    ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
3.2 如果只是_source_includes可以简写如下

GET pigg_test/_search?_source=false
{
  "query": {
    "term": {
      "interest": {
        "value": "food"
      }
    }
  },
  "_source": ["name", "interest"]
}
1
2
3
4
5
6
7
8
9
10
11
12
3.3 _source_excludes:屏蔽部分字段
GET pigg_test/_search
{
  "query": {
    "term": {
      "interest": {
        "value": "food"
      }
    }
  },
  "_source": {
    "excludes": ["age", "about", "interest"]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
返回结果中,_source就屏蔽了"age", “about”, “interest”

...省略别的信息...
 "hits" : [
      {
        "_index" : "pigg_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "冬哥"
        }
      },
      {
        "_index" : "pigg_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "胖丫"
        }
      }
    ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3.4 都有时,excludes优先级>includes优先级
GET pigg_test/_search
{
  "query": {
    "term": {
      "interest": {
        "value": "food"
      }
    }
  },
  "_source": {
    "includes": ["interest", "name"],
    "excludes": ["interest", "about"]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
includes包含了interest,但excludes也包含了excludes。
返回结果中只有name。

...省略别的信息...
"hits" : [
      {
        "_index" : "pigg_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "冬哥"
        }
      },
      {
        "_index" : "pigg_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.4700036,
        "_source" : {
          "name" : "胖丫"
        }
      }
    ]
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]中的错误信息,TypeError: __new__() missing 1 required positional argument: 'is_raw',这个错误通常是由于在创建一个新的对象时,缺少了一个必需的参数is_raw导致的。根据引用\[3\]中的代码,可以看出这个错误是在创建一个RawDataElement对象时发生的。可能是在调用这个对象的构造函数时,没有正确地传递dataset_root参数导致的。 另外,根据引用\[2\]中的描述,也有可能是由于Python库版本不兼容导致的。作者在使用es库时,Python版本为8.1.0,而本地es服务器版本为8.1.1。为了解决这个问题,作者将本地es服务器版本降级到了5.1.1,并将Python的es库版本降级到了5.1.0,这样问题就得到了解决。 综上所述,解决TypeError: __init__() missing 1 required positional argument: 'dataset_root'的方法是确保在创建RawDataElement对象时正确传递了dataset_root参数,并且检查Python库的版本兼容性。 #### 引用[.reference_title] - *1* *3* [TypeError: __new__() missing 1 required positional argument: ‘is_raw](https://blog.csdn.net/m0_60568871/article/details/127291889)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [已解决 | python 操作 elasticsearch TypeError: __init__() missing 1 required positional argument: ...](https://blog.csdn.net/shaotianyang12/article/details/125098389)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值