首页 \ 问答 \ elasticsearch按字符串字段的长度过滤(elasticsearch filter by length of a string field)

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
更新时间:2022-06-11 18:06

最满意答案

这是另一种解决方法,当您需要对某个函数进行部分特化时(这是不允许的)也很有用。 创建一个模板仿函数类(即,其唯一目的是执行单个成员函数的类,通常名为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#编译器不执行内联 - 但在这种情况下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 { ...
  • $ 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 ...
  • 您的Log(...)宏接受可变参数,并且预处理器会解析对它的调用。 它会将它在大括号中看到的所有内容都传递给VA_ARG。 我怀疑这可能是在预处理器中使用省略号的限制。 在C ++中编写Log()函数以避免以这种方式进行预处理器解析,或者使用Log()调用之外的宏来构建要输出的字符串。 例如: #if defined(_RELEASE) #define DS1 "RELEASE " #elif defined(_PROFILE) #define DS1 "PROFILE " #else #defin ...
  • #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 ...
  • 这是另一种解决方法,当您需要对某个函数进行部分特化时(这是不允许的)也很有用。 创建一个模板仿函数类(即,其唯一目的是执行单个成员函数的类,通常名为operator()),对其进行特化,然后在模板函数中调用。 我想我从Herb Sutter那里学到了这个技巧,但是不记得是哪本书(或文章)。 根据您的需要,它可能有点过分,但仍然...... template struct select; template struct testme_helper { void ...
  • 在您的情况下, CLASSEXPORT被扩展为CLASSEXPORT (可能因为不需要将它扩展为您的确切配置中的有意义的东西),因此您的类将只是一个class skExecutable {<...>}; 。 此方法通常用于导入/导出指令,例如,在以下代码段中,根据是否定义了COMPILING_DLL宏,将使用适当的指令: #if COMPILING_DLL #define DLLEXPORT __declspec(dllexport) #else #define DLLEXPORT __de ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)