使用ElasticSearch/Nest过滤空字符串 [英] Filter on empty string using ElasticSearch/Nest

查看:105
本文介绍了使用ElasticSearch/Nest过滤空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是我如何使用Nest在ElasticSearch中过滤空字符串.具体来说,我该如何重新创建以下结果:

This may be a silly question, but how do I filter on an empty string in ElasticSearch using Nest. Specifically, how do I recreate the following result:

curl http://localhost:9200/test/event/_search
{
    "filter" : { "term" : { "target" : "" }}
}

我尝试过:

(f => f
    .Term("target", "")
);

根据 ElasticSearch和Nest过滤不起作用的

是视为无条件查询并返回所有内容,同时添加.Strict()会引发DslException:

which according to ElasticSearch and Nest filtering does not work is treated like a conditionless query and returns everything, while adding a .Strict() throws a DslException:

(f => f
    .Strict().Term("target", "")
);

我还尝试了.Missing()和.Exists()无效.

I've also tried .Missing() and .Exists() to no avail.

我_mapping的相关部分供参考:

The relevant section of my _mapping for reference:

{
    "event": {
        "dynamic": "false",
        "properties": {
            target": {
                "type": "string",
                "index": "not_analyzed",
                "store": true,
                "omit_norms": true,
                "index_options": "docs"
            }
        }
    }
}

任何指针将不胜感激.

推荐答案

作为关于NEST的文档并编写查询提到您可以切换 Strict()模式以触发异常,如果一部分查询结果是无条件,但是如果那是您真正想要的,那么您就被困住了已经发现了.

As the documentation on NEST and writing queries mentions you can toggle Strict() mode to trigger exceptions if a part of your query turns out to be conditionless but if thats what you really wanted then you were stuck as you've found out.

我刚刚提交了一个 .Verbatim()构造,其结构与 .Strict()完全一样,但是它不会引发异常,而是将查询原样呈现并呈现按规定.

I just committed a .Verbatim() construct which works exactly like .Strict() but instead of throwing an exception it will take the query as is and render it as specified.

(f => f
    .Verbatim()
    .Term("target", "")
);

因此应禁用无条件查询重写,并按指定的字面意义插入查询.

Should thus disable the conditionless query rewrite and insert the query literally as specified.

这将在下一个版本的NEST(因此在当前版本0.12.0.0之后)

This will make it in the next version of NEST (so after the current version of 0.12.0.0)

这篇关于使用ElasticSearch/Nest过滤空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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