elasticsearch按字符串字段的长度过滤(elasticsearch filter by length of a string field)
我试图获得“标题”中的记录超过X个字符。
注意:并非所有记录都包含标题字段。
我努力了:
GET books/_search { "filter" : { "script" : { "script" : "_source.title.length() > 10" } } }
结果,我得到这个错误:
GroovyScriptExecutionException[NullPointerException[Cannot invoke method length() on null object
我怎么解决呢?
i am trying to get records the has in 'title' more then X characters.
NOTE: not all records contains title field.
i have tried:
GET books/_search { "filter" : { "script" : { "script" : "_source.title.length() > 10" } } }
as a result, i get this error:
GroovyScriptExecutionException[NullPointerException[Cannot invoke method length() on null object
how can i solve it?
原文:https://stackoverflow.com/questions/34041575
最满意答案
这是另一种解决方法,当您需要对某个函数进行部分特化时(这是不允许的)也很有用。 创建一个模板仿函数类(即,其唯一目的是执行单个成员函数的类,通常名为operator()),对其进行特化,然后在模板函数中调用。
我想我从Herb Sutter那里学到了这个技巧,但是不记得是哪本书(或文章)。 根据您的需要,它可能有点过分,但仍然......
template <typename T> struct select; template <bool B> struct testme_helper { void operator()(); }; template <typename T> class A { private: template <bool B> void testme() { testme_helper<B>()(); } public: void test() { testme<select<T>::value>(); } }; template<> void testme_helper<true>::operator()() { std::cout << "true" << std::endl; } template<> void testme_helper<false>::operator()() { std::cout << "false" << std::endl; }
Here is another workaround, also useful when you need to partialy specialize a function (which is not allowed). Create a template functor class (ie. class whose sole purpose is to execute a single member function, usually named operator() ), specialize it and then call from within your template function.
I think I learned this trick from Herb Sutter, but do not remember which book (or article) was that. For your needs it is probably overkill, but nonetheless ...
template <typename T> struct select; template <bool B> struct testme_helper { void operator()(); }; template <typename T> class A { private: template <bool B> void testme() { testme_helper<B>()(); } public: void test() { testme<select<T>::value>(); } }; template<> void testme_helper<true>::operator()() { std::cout << "true" << std::endl; } template<> void testme_helper<false>::operator()() { std::cout << "false" << std::endl; }
相关问答
更多-
在C#中进行内联扩展(Inline expansion in C#)[2021-09-16]
C#编译器不执行内联 - 但在这种情况下JIT可能会这样做。 如果有的话,这当然是内联的一部分。 编辑:我期望它在这种情况下内联,但很难说没有加载代码到类似cordbg的JIT优化打开。 每种不同的CLR版本可能对内联有不同的调整(包括64位与32位CLR),所以我不愿意说任何明确的内容。 The C# compiler doesn't perform inlining - but the JIT may well do so in this case. That's certainly the piece ... -
我不是MSVC ++或Xcode用户,但似乎“内联函数扩展”可以控制内联。 我认为Xcode使用clang或gcc :这两种编译器都有类似的内联方法。 对于gcc ,例如有以下选项(有更多的选项会影响内联决策): -fno-inline禁用内联(除了具有指示它们应始终被内联的属性的函数外) -finline-small-functions使内联功能在实际调用时更大(使用-O2启用) -finline-functions可以内联非inline函数(使用-O3启用) -flto可以启用跨多个翻译单元的内联功能 ...
-
这是海湾合作委员会错误47226 。 它仍然是开放的,并且代码示例在gcc 5.2.0上仍然失败,而它在3.6版本上编译得很好。 你的代码对我来说看起来很正确 This is gcc bug 47226. It's still open, and the code example still fails on gcc 5.2.0, while it compiles just fine on clang 3.6. Your code looks correct to me.
-
是的,这很简单: template
struct Foo { void bar(typename Args::type... a) {} }; [实例] [基于观点-] 请注意,这是一个不使用复数命名参数包的好理由; 实际编写参数包的所有用法,使包名称代表包的单个元素 ,并使用...扩展。 [/基于观点-] Yes, it's fairly straightforward: template struct Foo { ... -
Bash变量扩展(Bash variable expansion)[2020-01-03]
$ dirs="/home /home/a /home/b /home/a/b/c" $ dirsa=($dirs) $ echo "${dirsa[@]::$((${#dirsa[@]}-1))}" /home /home/a /home/b $ dirs="${dirsa[@]::$((${#dirsa[@]}-1))}" $ echo "$dirs" /home /home/a /home/b $ dirs="/home" $ dirsa=($dirs) $ dirs="${dirsa[@]::$(( ... -
使其成为非嵌套的非泛型静态类中的静态方法,并将Extension属性应用于类和方法。 这里棘手的一点可能是静态类的概念 - 可能不存在于C ++ / CLI中......我不确定。 当检测到扩展方法时,C#编译器可能并不关心这个类是否是静态的,只有在声明一个扩展方法的时候。 没有它当然值得一试。 Make it a static method in a non-nested, non-generic static class, with the Extension attribute applied to ...
-
显式C ++宏扩展(Explicit C++ Macro Expansion)[2022-03-19]
您的Log(...)宏接受可变参数,并且预处理器会解析对它的调用。 它会将它在大括号中看到的所有内容都传递给VA_ARG。 我怀疑这可能是在预处理器中使用省略号的限制。 在C ++中编写Log()函数以避免以这种方式进行预处理器解析,或者使用Log()调用之外的宏来构建要输出的字符串。 例如: #if defined(_RELEASE) #define DS1 "RELEASE " #elif defined(_PROFILE) #define DS1 "PROFILE " #else #defin ... -
C / C ++宏扩展(C/C++ macro expansion)[2023-05-12]
#define UNIT_BASIC_UNIT_DEF2 (name) UNIT_BASIC_ ## name #define UNIT_UNIT_TYPE_DEF2 (basic_type, name) UNIT_ ## basic_type ## _ ## name ... #define UNIT_BASIC_UNIT_DEF (name) UNIT_BASIC_UNIT_DEF2(name) #define UNIT_UNIT_TYPE_DEF (basic_type, name) UNIT_UNI ... -
C ++方法扩展(C++ method expansion)[2022-02-26]
这是另一种解决方法,当您需要对某个函数进行部分特化时(这是不允许的)也很有用。 创建一个模板仿函数类(即,其唯一目的是执行单个成员函数的类,通常名为operator()),对其进行特化,然后在模板函数中调用。 我想我从Herb Sutter那里学到了这个技巧,但是不记得是哪本书(或文章)。 根据您的需要,它可能有点过分,但仍然...... templatestruct select; template struct testme_helper { void ... -
C ++:宏扩展解释(C++: macro expansion explanation)[2022-06-27]
在您的情况下, CLASSEXPORT被扩展为CLASSEXPORT (可能因为不需要将它扩展为您的确切配置中的有意义的东西),因此您的类将只是一个class skExecutable {<...>}; 。 此方法通常用于导入/导出指令,例如,在以下代码段中,根据是否定义了COMPILING_DLL宏,将使用适当的指令: #if COMPILING_DLL #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT __de ...