且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在弹性搜索中进行索引时,

更新时间:2023-10-27 15:44:46

我建议你看看你对提升的要求,因为你目前的脚本没有什么意义。

I would suggest you to look at your requirements regarding boosting, since your current script doesn't make much sense.

另外,请查看弹性搜索的文档查询DSL 。它提供复合查询和简单查询,可以组合在一起。如错误所示,您不能将过滤器放在自定义分数查询中。您可以在自定义分数查询内使用过滤查询 :$ {


Also, have a look at the documentation for the elasticsearch query DSL. It provides either compound queries and simple ones, which you can combine together. As the error says, you can't put a filter inside a custom score query. You can either use a filtered query inside the custom score query:

{
  "query": {
    "custom_score": {
      "query": {
        "filtered" : {
          "query" : {
            "match": {
              "xxx": {
                "query": "foobar"
              }
            }
          },
          "filter" : {
            "and": [
              {
                "query": {
                  "match": {
                    "yyyy": {
                      "query": "barfoo"
                    }
                  }
                }
              }
            ]
          }
        }
      },
      "script": "_score * doc['_score']"
    }
  }
}

或使用***过滤器,如下所示:

or use a top level filter like this:

{
  "query": {
    "custom_score": {
      "query": {
        "match": {
          "xxx": {
            "query": "foobar"
          }
        }
      },
      "script": "_score * doc['_score']"
    }
  },
  "filter": {
    "and": [
      {
        "query": {
          "match": {
            "yyyy": {
              "query": "barfoo"
            }
          }
        }
      }
    ]
  }
}

两个选项之间的区别是,如果您制作了顶层过滤器,则不考虑***过滤器您的搜索请求中的方面也是如此,而如果您将过滤器放在查询中,则会被考虑。

The difference between the two options is that the top level filter is not considered if you make facets too in your search request, while if you put the filters within the query they are considered.

另外需要注意的事项:您不需要过滤器如果你只有一个子句。而且,过滤器中的全文搜索通常是没有意义的,因为过滤器是可缓存的,并且由于全文搜索是免费的,并且几乎不可预测,因此缓存它们将是浪费的。

One other thing to look at: you don't need an and filter if you have only a single clause. Also, it usually doesn't make sense to put a full-text search within a filter, since filters are cacheable and given that full-text searches are free and pretty much unpredictable it would be a waste to cache them.