在Elasticsearch查询中排除字段 [英] Exclude a field on a Elasticsearch query

查看:1479
本文介绍了在Elasticsearch查询中排除字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下映射:

curl -XPUT 'localhost:9200/testidx?pretty=true' -d '{
  "mappings": {
    "items": {
       "dynamic": "strict",
       "properties" : {
            "title" : { "type": "string" },
            "body" : { "type": "string" }
}}}}'

我在上面放了两个项目:

I put two items on it:

curl -XPUT 'localhost:9200/testidx/items/1' -d '{
  "title": "Titulo anterior",
  "body": "blablabla blablabla blablabla blablabla blablabla blablabla"
}'

curl -XPUT 'localhost:9200/testidx/items/2' -d '{
  "title": "Joselr",
  "body": "Titulo stuff more stuff" 
}'

现在我要搜索词 titulo ,除了 body 之外的所有字段,所以我要做的是(跟随帖子):

Now I want to search the word titulo on every field but body, so what I do is (following this post):

curl -XGET 'localhost:9200/testidx/items/_search?pretty=true' -d '{
    "query" : {
         "query_string": {
              "query": "Titulo"
         }},
         "_source" : {
              "exclude" : ["*.body"]
         }
    }'

应该只显示 1 项,因为第二个项中的单词是 Titulo ,但是它在 body 上,这就是我要忽略的内容。

It's supposed to show only the 1 item, as the second one has the word Titulo but it's on the body and that's what I want to ignore. How can archive this?

PS:这只是一个简单的示例,我有一个包含很多属性的映射,在某些搜索中我想忽略其中的一些属性。

PS2:我正在使用ES 2.3.2

PS: This is just a simple example, I've a mapping with a lot of properties and I want to ignore some of them in some searches.
PS2: I'm using ES 2.3.2

推荐答案

_source / exclude 设置仅在不返回响应中的 body 字段时有用,但这并不排除搜索该字段。

The _source/exclude setting is only useful for not returning the body field in the response, but that doesn't exclude that field from being searched.

您可以做的是改为指定要搜索的所有字段(白名单方法)

What you can do is to specify all the fields you want to search instead (whitelist approach)

curl -XGET 'localhost:9200/testidx/items/_search?pretty=true' -d '{
  "query" : {
     "query_string": {
          "fields": ["title", "field2", "field3"],      <-- add this
          "query": "Titulo"
     }},
     "_source" : {
          "exclude" : ["*.body"]
     }
}'

您可以做的另一件事是明确指定 body 不应与 -body:Titulo

Another thing you can do is to explicitly specify that body should not be matched with -body:Titulo

curl -XGET 'localhost:9200/testidx/items/_search?pretty=true' -d '{
  "query" : {
     "query_string": {
          "query": "Titulo AND -body:Titulo"                <-- modify this
     }},
     "_source" : {
          "exclude" : ["*.body"]
     }
}'

这篇关于在Elasticsearch查询中排除字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆