【问题标题】:Elasticsearch (version 2.3) Function Score Query with filtered type queryElasticsearch (version 2.3) Function Score Query 过滤类型查询
【发布时间】:2016-06-01 13:51:52
【问题描述】:

我对弹性搜索非常陌生,我们正在从 Solr 迁移到弹性搜索。作为迁移工作的一部分,将现有 Solr 查询转换为弹性搜索 DSL 查询。

这是我使用函数评分功能部分完成的 DSL 查询。

{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "match": {
            "name": "barack obama"
          },
          "filter": {
            "range": {
              "relevance": {
                "gte": 6
              }
            },
            "bool": {
              "must_not": [
                {
                  "terms": {
                    "classIds": [
                      199,
                      220
                    ],
                    "execution": "and"
                  }
                }
              ],
              "must": [
                {
                  "term": {
                    "classIds": 10597
                  }
                }
              ]
            }
          }
        }
      },
      "boost_mode": "replace",
      "functions": [
        {
          "script_score": {
            "script": {
              "lang": "groovy",
              "file": "calculate-score",
              "params": {
                "relevance_boost": 1,
                "class_penalize": 0.25
              }
            }
          }
        }
      ]
    }
  }
}

此查询在针对弹性搜索集群运行时返回错误。请帮我解决问题。

这里的 calculate-score 是 groovy 脚本,它工作正常,我用简单的查询对其进行了测试。

这是错误响应:

{
  "error": {
    "root_cause": [
      {
        "type": "query_parsing_exception",
        "reason": "[filtered] query does not support [match]",
        "index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0",
        "line": 6,
        "col": 11
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0",
        "node": "NOAwAtVwQS25egu7AIaHEg",
        "reason": {
          "type": "query_parsing_exception",
          "reason": "[filtered] query does not support [match]",
          "index": "nodes_5e27a7d3-b370-40bd-9e71-cf04a36297c0",
          "line": 6,
          "col": 11
        }
      }
    ]
  },
  "status": 400
}

这是我尝试转换为弹性搜索的 Solr 查询:

SOLR QUERY (UNIQUE_NODE_CORE): q={!boost b="product(pow(field(relevance),1.0000),if(exists(query({!v='all_class_ids:226'})),0.25,1),if(exists(query({!v='all_class_ids:14106'})),0.25,1),if(exists(query({!v='all_class_ids:656'})),0.25,1))"}
raw_name:"barack obama"
&rows=1
&start=0
&sort=score desc,relevance desc
-&fq=class_id:"10597"
-fq=relevance:[6 TO *]
-&fq=-all_class_ids:"14127"
-&fq=-all_class_ids:"14106"
-&fq=-all_class_ids:"226"
&fl=ontology_id,url_friendly_name,name,score,raw_notable_for,property_207578

只需要帮助来运行带有函数分数的过滤查询。

【问题讨论】:

    标签: elasticsearch solr spring-data-elasticsearch


    【解决方案1】:

    干得好,您就快到了,您只是在 filtered 查询中缺少一个 query 部分,以便包装 match 查询。同样,range 过滤器可以插入到bool/must 中。我知道,相当拗口。

    {
      "query": {
        "function_score": {
          "query": {
            "filtered": {
              "query": {
                "match": {
                  "name": "barack obama"
                }
              },
              "filter": {
                "bool": {
                  "must_not": [
                    {
                      "terms": {
                        "classIds": [
                          199,
                          220
                        ],
                        "execution": "and"
                      }
                    }
                  ],
                  "must": [
                    {
                      "range": {
                        "relevance": {
                          "gte": 6
                        }
                      }
                    },
                    {
                      "term": {
                        "classIds": 10597
                      }
                    }
                  ]
                }
              }
            }
          },
          "boost_mode": "replace",
          "functions": [
            {
              "script_score": {
                "script": {
                  "lang": "groovy",
                  "file": "calculate-score",
                  "params": {
                    "relevance_boost": 1,
                    "class_penalize": 0.25
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    请注意,自 ES 2.0 起,filtered 查询已被弃用,您可以使用 bool/must/filter 查询重写它,如下所示:

    {
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "must": {
                "match": {
                  "name": "barack obama"
                }
              },
              "filter": [
                {
                  "range": {
                    "relevance": {
                      "gte": 6
                    }
                  }
                },
                {
                  "term": {
                    "classIds": 10597
                  }
                }
              ],
              "must_not": [
                {
                  "terms": {
                    "classIds": [
                      199,
                      220
                    ],
                    "execution": "and"
                  }
                }
              ]
            }
          },
          "boost_mode": "replace",
          "functions": [
            {
              "script_score": {
                "script": {
                  "lang": "groovy",
                  "file": "calculate-score",
                  "params": {
                    "relevance_boost": 1,
                    "class_penalize": 0.25
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多