简体   繁体   English

在弹性搜索中将子过滤器应用于术语的正确方法是什么?

[英]What is the correct way to apply a sub filter to a term in Elastic search?

I have a query I am trying to fetch results via Elastic Search 6.4.2. 我有一个查询,我试图通过Elastic Search 6.4.2获取结果。

It is working. 这是工作。 But when I apply the Tags part which should be under the NewsArticle type, it brings back results for the CaseStudyPage type. 但是,当我应用应该在NewsArticle类型下的Tags部分时,它将带回CaseStudyPage类型的结果。

Am I doing this correctly? 我这样做正确吗?

Basically I want to sub filter tags on the NewsArticle type but it doesn't seem to work correctly. 基本上,我想对NewsArticle类型的标签进行子过滤,但是它似乎无法正常工作。

I have tried many different formats which I have found on StackOverflow and various web pages. 我尝试了许多不同的格式,这些格式可以在StackOverflow和各种网页上找到。

   "highlight":{  
      "pre_tags":[  
         ""
      ],
      "post_tags":[  
         "<\/strong>"
      ],
      "fields":{  
         "*":{  

         }
      },
      "require_field_match":false,
      "fragment_size":100,
      "number_of_fragments":3,
      "highlight_query":{  
         "query_string":{  
            "query":"",
            "analyze_wildcard":true,
            "default_operator":"AND"
         }
      }
   },
   "sort":[{"PublishedDate":"desc"}],
   "size":5000,
   "query":{  
      "bool":{  
         "filter":{  
            "bool":{  
               "should":[
                  {
                     "terms":{  
                        "ClassName":[  
                           "CaseStudyPage"
                        ]
                     }

                  },
                  {
                     "bool":{ 
                        "must": [
                           {
                              "terms":{  
                                 "ClassName":[
                                    "NewsArticle"
                                 ]
                              }
                           },
                           {
                              "terms":{  
                                 "Tags.ID":[
                                    "9"
                                 ]
                              }
                           }
                        ]
                     }
                  }
               ]
            }
         }
      }
   }
}

No error messages. 没有错误讯息。 It brings back results for both CaseStudyPage and NewsArticle which both have the Tag.ID = 9. But it should only bring back CaseStudyPage (Full results) and NewsArticle results that only have the Tag.ID = 9. 它会带回具有Tag.ID = 9的CaseStudyPage和NewsArticle的结果。但是,它只应带回仅具有Tag.ID = 9的CaseStudyPage(完整结果)和NewsArticle结果。

The results return tagged content from the CaseStudyPage type and the NewsArticle type, but it should only display the tagged content from the NewsArticle type and all the CaseStudyPage type. 结果返回CaseStudyPage类型和NewsArticle类型的已标记内容,但结果应仅显示NewsArticle类型和所有CaseStudyPage类型的已标记内容。

First of all, for debuging purposes, I'd remove the "highlight" section & also the "sort" (I'm not sure if a document gets filtered out if the sort field - in your case 'PublishedDate' is empty? probably not) 首先,出于调试目的,我将删除“突出显示”部分以及“排序”(我不确定如果排序字段(在您的情况下,“ PublishedDate”为空)是否将文档过滤掉?不)

Now, focusing on the query itself; 现在,重点关注查询本身; it appears that your problem (If i undrestood correctly) is that the "Tags flter term query" is not working, because you are receiving in your result all CaseStudyPage and NewsArticle, even tough for those last ones you want specifically the ones with a Tags.ID = 9 (right?). 看来您的问题(如果我没有正确地理解)是“ Tags flter词条查询”不起作用,因为您在结果中收到了所有CaseStudyPage和NewsArticle,对于那些您想要的最后一个,尤其是那些带有Tags的那些,甚至很难.ID = 9(对吗?)。

I believe your Tags.ID is an integer type right?, if so please remove the quotes arround the 9 (if you didn't create a mapping before indexing specifying your Tags.ID was indeed an "int" type, then the elasticsearch created a type mapping for the field based on your first insert, please verify that you Tags.ID is either a "not analyzed String - KEYWORD" or an integer; this is necessary for filter queries & terms queries to work properly). 我相信您的Tag.ID是整数类型,对吗?如果是的话,请删除9周围的引号(如果您在创建索引之前未指定您的Tag.ID实际上是“ int”类型,则创建了elasticsearch基于您的第一个插入的字段的类型映射,请验证您的Tag.ID是“未分析的字符串-关键字”还是整数;这对于过滤器查询和字词查询正常运行是必要的)。

Another possibility could be that the 'className' field contains several values? 另一种可能是“ className”字段包含多个值? for example CaseStudyPage and NewsArticle both like an array? 例如CaseStudyPage和NewsArticle都像数组吗? if so; 如果是这样的话; then your should block will pick all those documents containing "CaseStudyPage" independently on the "NewsArticle" being prsent, or the TAGS.ID value. 那么您的should块将根据所呈现的“ NewsArticle”或TAGS.ID值独立地选择所有包含“ CaseStudyPage”的文档。 But this is rather unlikely. 但这不太可能。

I'd create a small filter query where I'd only test that you are correctly filtering the documents with the Tags.ID = 9; 我将创建一个小的过滤器查询,在这里我仅测试您是否使用Tags.ID = 9;正确过滤了文档; and after you've got that working, the you can put that filter again in the must block. 在完成该工作之后,您可以将该过滤器再次放入must块中。 The rest of your query looks fine. 您的其余查询看起来不错。

EDIT: btw if for the 'CaseStudyPage' filter, you explicitly want entries that are not tagged, then you also need to add a' must not' block for that, or must with != 编辑:顺便说一句,如果对于'CaseStudyPage'过滤器,您明确希望未标记的条目,那么您还需要为此添加一个'must not'块,或者必须使用!=

Hope it helps. 希望能帮助到你。

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

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