简体   繁体   English

Elasticsearch:嵌套查询中的脚本字段

[英]Elasticsearch: script fields in nested query

Is there a way to use a "script_fields" in a nested query adding fields to the returned inner_hits? 有没有一种方法可以在嵌套查询中使用“ script_fields”,将字段添加到返回的inner_hits中? Example: 例:

{
   "nested": {
      "inner_hits": {},
      "path": "companies",
      "score_mode": "sum",
      "query": {},
      "script_fields": {
         "overlap" : {
            "script": {
               "source": "................................",
               "params": {
                   "from": "2012-01-01",
                    "to": "2015-06-30"
                }
            }
         }                
      }         
}

I execute n nested queries passing to each one a set of specific params. 我执行n个嵌套查询,将每个特定参数传递给每个嵌套查询。 The idea is having the source script to assign a value to the overlap field for each one of the inner hits based on the provided params. 想法是让源脚本根据所提供的参数为每个内部匹配的重叠字段分配一个值。

Update after Val suggestion on adding script_fields into the inner hits Val建议在内部命中添加script_fields之后更新

Looks like executing more nested queries on the same nested path defining different inner_hits makes ES to strip inner_hits matches. 看起来像在同一嵌套路径上执行更多嵌套查询,定义了不同的inner_hits使得ES剥离了inner_hits匹配项。 Example: 例:

{
       "nested": {
          "inner_hits": {
            "script_fields": {
               "overlap" : {
                  "script": {
                     "source": "................................",
                        "params": {
                           "from": "2012-01-01",
                           "to": "2015-06-30"
                         }
                      }
                   }                
                 } 
          },
          "path": "companies",
          "score_mode": "sum",
          "query": {},

    },
    {
       "nested": {
          "inner_hits": {
            "script_fields": {
               "overlap" : {
                  "script": {
                     "source": "................................",
                        "params": {
                           "from": "2012-01-01",
                           "to": "2015-06-30"
                         }
                      }
                   }                
                 } 
          },
          "path": "companies",
          "score_mode": "sum",
          "query": {},

    } 

If a run more nested query like these I get the correct matches but not all the expected inner_hits are returned. 如果运行像这样的更多嵌套查询,我将获得正确的匹配项,但不会返回所有预期的inner_hits。 I can obtain all the hits only giving a random name to the different inner_hits like: 我可以获得所有匹配,只给不同的inner_hits随机命名,例如:

"inner_hits": {
   "name": [random name]
   "script_fields": {
      "overlap" : {
         ......
      }
    }
 }   

Then I get the correct hits results but with some downside. 然后我得到正确的命中结果,但有一些缺点。 I must check for duplicated results as hits are added to different result sets and are not uniq anymore. 我必须检查重复的结果,因为匹配已添加到不同的结果集中,并且不再唯一。 I have to parse the results using a regex for rebuilding a unified list. 我必须使用正则表达式解析结果以重建统一列表。

Does this sounds like a bug? 这听起来像个虫子吗?

Yes that's doable. 是的,那是可行的。 you simply need to move the script_fields section inside inner_hits , like this: 您只需要在inner_hits内部移动script_fields部分,就像这样:

{
   "nested": {
      "path": "companies",
      "score_mode": "sum",
      "query": {}.
      "inner_hits": {
         "script_fields": {
           "overlap" : {
             "script": {
               "source": "................................",
               "params": {
                   "from": "2012-01-01",
                    "to": "2015-06-30"
               }
             }
           }                
         }     
      }
   }              
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM