在ES查询中添加额外标志以检查字段是否存在

时间:2014-02-19 08:54:50

标签: elasticsearch

我正在编写一个查询来获取这样的记录:

curl -X GET 'http://localhost:9200/posts/post/_search?from=0&size=30&pretty' -d '{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "content:(aid OR hiv)"
        }
      }
    }
  },
  "fields": [
    "content",
    "entity_avatar_link",
    "author_link",
    "name"
  ],
  size: 30,
  from: 0
}

这很好,我得到了结果。

我正在尝试添加一个脚本字段(用于标记),该字段返回文档中是否存在字段以及返回的每个文档(我无法返回字段,因为在大多数情况下,它将是一个非常大的字段(嵌入式领域))。所以,我也将此添加到查询中:

"script_fields": {
    "is_arranged_flag": {
      "script": "!_source.arranged_retweets.empty"
    }
  }

所以整个查询就像:

curl -X GET 'http://localhost:9200/posts/post/_search?from=0&size=30&pretty' -d '{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "content:(aid OR hiv)"
        }
      }
    }
  },
  "fields": [
    "content",
    "entity_avatar_link",
    "author_link",
    "name"
  ],
  "script_fields": {
    "is_arranged_flag": {
      "script": "!_source.arranged_retweets.empty"
    }
  }
  size: 30,
  from: 0
}

但是在添加script_fields部分后,没有结果出现(对于同一个搜索查询,结果为空[])。

我也尝试过:

 "script_fields": {
    "is_arranged_flag": {
      "script": "!doc['arranged_retweets'].empty"
    }
  }

我做错了什么?

以下是映射http://localhost:9200/posts/post/_mapping

{
  "post": {
    "properties": {
      "arranged_retweets": {
        "properties": {
          "author_gender": {
            "type": "string"
          },
          "author_link": {
            "type": "string"
          }
        }
      },
      "content": {
        "type": "string",
        "analyzer": "tweet_analyzer"
      },
      "name": {
        "type": "string",
        "index": "not_analyzed",
        "omit_norms": true,
        "index_options": "docs"
      },
      "author_link": {
        "type": "string",
        "index": "not_analyzed",
        "omit_norms": true,
        "index_options": "docs"
      },
      "entity_avatar_link": {
        "type": "string",
        "index": "not_analyzed",
        "omit_norms": true,
        "index_options": "docs"
      },
    }
  }
}

2 个答案:

答案 0 :(得分:0)

我认为这是有效的script_fields段。

"script_fields": {
   "is_arranged_flag": {
      "script": "!doc['arranged_retweets'].empty"
   }
}

参考:scripting(阅读文档字段部分)

答案 1 :(得分:0)

我在这里的讨论(https://groups.google.com/forum/#!topic/elasticsearch/BJZdlFSJSRg)的帮助下想出来了。字段arrange_retweets是一个对象。因此,我们需要检查内层arranged_retweets.author_gender并检查它是否为空:

"script_fields": {
   "is_arranged_flag": {
      "script": "!doc['arranged_retweets.author_gender'].empty"
   }
}