ElasticSearch - 如果field为null,则视为0?

时间:2022-10-12 03:11:03

I am using ElasticSearch, and am running into NullPointerExceptions because a field I am basing the score on is NULL for several documents.

我正在使用ElasticSearch,并且正在运行NullPointerExceptions,因为我基于得分的字段对于多个文档来说是NULL。

How do I make ElasticSearch treat these null fields as 0?

如何让ElasticSearch将这些空字段视为0?

FWIW, I'm using the Tire gem and this is my Ruby code:

FWIW,我正在使用Tire gem,这是我的Ruby代码:

s = Tire.search "articles" do
            query do
                custom_score :script => "_score < 2 ? 0 : doc['num_likes'].value" do
                   match fields, keyword
                end     
            end               
end

2 个解决方案

#1


2  

I discovered I need to do something like this:

我发现我需要做这样的事情:

_score < 2 ? 0 : (doc['num_likes'].empty ? 0 : doc['num_likes'].value)

#2


1  

What about a script like :

脚本怎么样:

"_score < 2 ? 0 : (doc['num_likes'] == null ? 0 : doc['num_likes'].value)"

Does it work?

它有用吗?

#1


2  

I discovered I need to do something like this:

我发现我需要做这样的事情:

_score < 2 ? 0 : (doc['num_likes'].empty ? 0 : doc['num_likes'].value)

#2


1  

What about a script like :

脚本怎么样:

"_score < 2 ? 0 : (doc['num_likes'] == null ? 0 : doc['num_likes'].value)"

Does it work?

它有用吗?