elasticsearch - 如何在 Elastic Search 中为更像这样的查询添加过滤器?

标签 elasticsearch

我想在 Elastic Search 中使用 More like this 查询来查找类似文档。但是,我需要过滤执行查询的文档。

示例如下:我想查找与 ID 为 123456 的博客类似但作者为 120 或作者 123 的博客项目。

当执行这个查询时,我从所有作者那里得到了类似的博客,因此没有被过滤......

{
  "query":{
    "more_like_this" : {

        "fields" : ["body" ],  
        "docs" : [
              {
                "_id" : "123456"
              }
            ],
        "percent_terms_to_match" : 0.4,
            "min_term_freq" : 1
      }
  }
 },

"filter":{
  "and":[            
          {
            "type":{ "value":"blog" }
          },
          {
            "terms":{ "authorId": ["120", "123"] }
          }
        ]
}
}

最佳答案

像这样尝试过滤查询:

{
    "query": {
        "filtered": {
            "query": {
                "more_like_this": {
                    "fields": [
                        "body"
                    ],
                    "docs": [
                        {
                            "_id": "123456"
                        }
                    ],
                    "percent_terms_to_match": 0.4,
                    "min_term_freq": 1
                }
            },
            "filter": {
                "and": [
                    {
                        "type": {
                            "value": "blog"
                        }
                    },
                    {
                        "terms": {
                            "authorId": [
                                "120",
                                "123"
                            ]
                        }
                    }
                ]
            }
        }
    }
}

希望对您有所帮助...!

关于elasticsearch - 如何在 Elastic Search 中为更像这样的查询添加过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960921/

相关文章:

elasticsearch - 部分更新ElasticSearch索引中的嵌套记录

elasticsearch - 在Elasticsearch中索引字符串的最后一个单词

Docker 容器集群中的 Elasticsearch

elasticsearch - 是否可以使用查询在 elasticsearch 中使用 "join"两个索引

elasticsearch - 在Elasticsearch中匹配json对象数组中的数组元素

csv - 无法更改es2csv cli的输出CSV分隔符

ruby-on-rails - Elasticsearch、轮胎与协会

elasticsearch - 为 SynonymFilterFactory 术语赋予权重

elasticsearch - 使用包含点的值在标签键上使用术语进行过滤不会返回任何结果

elasticsearch - ElasticSearch Springboot NativeSearchQueryBuilder问题