在弹性搜索上查找空字符串值的文档 [英] Find documents with empty string value on elasticsearch

查看:156
本文介绍了在弹性搜索上查找空字符串值的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用弹性搜索过滤那些在其正文中包含一个空字符串的文档。到目前为止我没有运气。



在我继续之前,我应该提到我已经尝试了围绕Interwebz和StackOverflow传播的许多解决方案。 p>

所以,下面是我试图运行的查询,其次是其对应的:

 <
查询:{
过滤:{
过滤器:{
bool:{
must_not :[
{
missing:{
field:_ textContent
}
}
]
}
}
}
}
}

我也尝试以下:

  {
查询:{
已过滤:{
过滤器:{
bool:{
must_not:[
{
missing:{
field:_ textContent,
exists:true,
null_value:true
}
}
]
}
}
}
}
}

以下内容:

  {
查询:{
filtered:{
filter:{
missing:{field:_textContent}
}
}
}
}

上述都没有。当我知道有一个包含一个空字符串字段的记录时,我得到一个空的结果集。



如果有人可以提供任何帮助,非常感谢。



谢谢!

解决方案

使用默认分析器(标准),无法分析它是否为空字符串。所以你需要逐字记录字段(不分析)。这里有一个例子:



添加一个映射,将索引字段未映射,如果您需要索引的字段的标记副本,您也可以使用多字段类型。

  PUT http:// localhost:9200 / test / _mapping / demo 
{
demo :{
properties:{
_content:{
type:string,
index:not_analyzed
}
}
}
}

接下来,索引一些文档。

  / POST http:// localhost:9200 / test / demo / 1 / 
{
_content:
}

/ POST http:// localhost:9200 / test / demo / 2
{
_content:some content
}

执行搜索:

  POST http:// localhost:9200 / test / demo / _search 
{
query:{
filtered:{
filter:{
term:{
_content:
}
}
}
}
}

返回带有空字符串的文档。

  {
take:2,
timed_out:false,
_shards:{
total:5,
successful :5,
失败:0
},
命中:{
总数:1,
max_score:0.30685282,
命中:[
{
_index:test,
_type:demo,
_id:1,
_score:0.30685282,
_source:{
_content:
}
}
]
}
}


I've been trying to filter with elasticsearch only those documents that contains an empty string in its body. So far I'm having no luck.

Before I go on, I should mention that I've already tried the many "solutions" spread around the Interwebz and StackOverflow.

So, below is the query that I'm trying to run, followed by its counterparts:

{
    "query": {
        "filtered":{
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "missing":{
                                "field":"_textContent"
                            }
                        }
                    ]
                }
            }
        }
    }
}

I've also tried the following:

 {
    "query": {
        "filtered":{
            "filter": {
                "bool": {
                    "must_not": [
                        {
                            "missing":{
                                "field":"_textContent",
                                "existence":true,
                                "null_value":true
                            }
                        }
                    ]
                }
            }
        }
    }
}

And the following:

   {
    "query": {
        "filtered":{
            "filter": {
                    "missing": {"field": "_textContent"}
            }
        }
    }
}

None of the above worked. I get an empty result set when I know for sure that there are records that contains an empty string field.

If anyone can provide me with any help at all, I'll be very grateful.

Thanks!

解决方案

If you are using the default analyzer (standard) there is nothing for it to analyze if it is an empty string. So you need to index the field verbatim (not analyzed). Here is an example:

Add a mapping that will index the field untokenized, if you need a tokenized copy of the field indexed as well you can use a Multi Field type.

PUT http://localhost:9200/test/_mapping/demo
{
  "demo": {
    "properties": {
      "_content": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}

Next, index a couple of documents.

/POST http://localhost:9200/test/demo/1/
{
  "_content": ""
}

/POST http://localhost:9200/test/demo/2
{
  "_content": "some content"
}

Execute a search:

POST http://localhost:9200/test/demo/_search
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "_content": ""
        }
      }
    }
  }
}

Returns the document with the empty string.

{
    took: 2,
    timed_out: false,
    _shards: {
        total: 5,
        successful: 5,
        failed: 0
    },
    hits: {
        total: 1,
        max_score: 0.30685282,
        hits: [
            {
                _index: test,
                _type: demo,
                _id: 1,
                _score: 0.30685282,
                _source: {
                    _content: ""
                }
            }
        ]
    }
}

这篇关于在弹性搜索上查找空字符串值的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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