【问题标题】:How to use a bool Query with function_score in Elasticsearch?如何在 Elasticsearch 中使用带有 function_score 的布尔查询?
【发布时间】:2015-01-27 01:19:36
【问题描述】:

我有 3 种类型的数据 [医生、文章、地点],我想根据布尔查询匹配他们的文档。但我想给医生类型提升 3,地方类型提升 2,文章提升 1。我有一个使用 function_scorebool query 匹配文档的搜索查询。但问题是它抛出了一个异常,上面写着No function with the name [bool] is registered.。这是我的查询:

GET /my_index/doctor,article,place/_search
{
    "query": {
        "function_score": {
            "bool": {
                "should":
                [
                    {
                      "common": {
                        "title": {
                            "query":"$q",
                            "cutoff_frequency": 0.001,
                            "low_freq_operator": "and"
                            }
                        }
                    },
                    {
                        "prefix": {
                            "title": "$q"
                            }

                    },
                    {
                        "match_phrase_prefix" : {
                            "title" : {
                                "query": "$q",
                                "slop":  10
                            }
                        }
                    }
                ]
            },
            "functions":[
                {
                    "filter":{
                        "type":{
                           "value":"doctor"
                        }
                    },
                    "weight":3
                },
                {
                    "filter":{
                        "type":{
                           "value":"place"
                        }
                    },
                    "weight":2
                },
                {
                    "filter":{
                        "type":{
                           "value":"article"
                        }
                    },
                    "weight":1
                }
            ],
            "score_mode":"first",
            "boost_mode":"multiply"
        }
    }
}

感谢您的帮助。

编辑

错误:

{
  "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;
            shardFailures {[fWqkaeoLSva_QwhrYHnG3A][darmanjoo][0]:
            SearchParseException[[darmanjoo][0]: from[-1],size[-1]:
              Parse Failure [Failed to parse source [
                {
                  "query": {
                    "function_score": {
                      "query": {
                        "bool": {
                          "should": [
                            {
                              "common": {
                                "title": {
                                  "query": "دکتر",
                                  "cutoff_frequency": 0.001,
                                  "low_freq_operator": "and"
                                }
                              }
                            },
                            {
                              "prefix": {
                                "title": "دکتر"
                              }
                            },
                            {
                              "match_phrase_prefix": {
                                "title": {
                                  "query": "دکتر",
                                  "slop": 10
                                }
                              }
                            }
                          ]
                        }
                      },
                      "functions": [
                        {
                          "filter": {
                            "type": {
                              "value": "doctor"
                            }
                          },
                          "weight": 3
                        },
                        {
                          "filter": {
                            "type": {
                              "value": "place"
                            }
                          },
                          "weight": 2
                        },
                        {
                          "filter": {
                            "type": {
                              "value": "article"
                            }
                          },
                          "weight": 1
                        }
                      ],
                      "score_mode": "first",
                      "boost_mode": "multiply"
                    }
                  }
                }
              ]]];
            nested: QueryParsingException[[darmanjoo] No function with the name [weight] is registered.]; }{[fWqkaeoLSva_QwhrYHnG3A][darmanjoo][1]:
            SearchParseException[[darmanjoo][1]: from[-1],size[-1]:
              Parse Failure [Failed to parse source [
                {
                  "query": {
                    "function_score": {
                      "query": {
                        "bool": {
                          "should": [
                            {
                              "common": {
                                "title": {
                                  "query": "دکتر",
                                  "cutoff_frequency": 0.001,
                                  "low_freq_operator": "and"
                                }
                              }
                            },
                            {
                              "prefix": {
                                "title": "دکتر"
                              }
                            },
                            {
                              "match_phrase_prefix": {
                                "title": {
                                  "query": "دکتر",
                                  "slop": 10
                                }
                              }
                            }
                          ]
                        }
                      },
                      "functions": [
                        {
                          "filter": {
                            "type": {
                              "value": "doctor"
                            }
                          },
                          "weight": 3
                        },
                        {
                          "filter": {
                            "type": {
                              "value": "place"
                            }
                          },
                          "weight": 2
                        },
                        {
                          "filter": {
                            "type": {
                              "value": "article"
                            }
                          },
                          "weight": 1
                        }
                      ],
                      "score_mode": "first",
                      "boost_mode": "multiply"
                    }
                  }
                }
              ]]];
            nested: QueryParsingException[[darmanjoo] No function with the name [weight] is registered.]; }{[fWqkaeoLSva_QwhrYHnG3A][darmanjoo][4]:
            SearchParseException[[darmanjoo][4]: from[-1],size[-1]:
              Parse Failure [Failed to parse source [
                {
                  "query": {
                    "function_score": {
                      "query": {
                        "bool": {
                          "should": [
                            {
                              "common": {
                                "title": {
                                  "query": "دکتر",
                                  "cutoff_frequency": 0.001,
                                  "low_freq_operator": "and"
                                }
                              }
                            },
                            {
                              "prefix": {
                                "title": "دکتر"
                              }
                            },
                            {
                              "match_phrase_prefix": {
                                "title": {
                                  "query": "دکتر",
                                  "slop": 10
                                }
                              }
                            }
                          ]
                        }
                      },
                      "functions": [
                        {
                          "filter": {
                            "type": {
                              "value": "doctor"
                            }
                          },
                          "weight": 3
                        },
                        {
                          "filter": {
                            "type": {
                              "value": "place"
                            }
                          },
                          "weight": 2
                        },
                        {
                          "filter": {
                            "type": {
                              "value": "article"
                            }
                          },
                          "weight": 1
                        }
                      ],
                      "score_mode": "first",
                      "boost_mode": "multiply"
                    }
                  }
                }
              ]]];
            nested: QueryParsingException[[darmanjoo] No function with the name [weight] is registered.]; }]",
  "status": 400
}

我从 MARVEL SENSE 直接复制的已编辑查询:

GET /darmanjoo/doctor,article,place/_search
{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "common": {
                                "title": {
                                    "query": "$q",
                                    "cutoff_frequency": 0.001,
                                    "low_freq_operator": "and"
                                }
                            }
                        },
                        {
                            "prefix": {
                                "title": "$q"
                            }
                        },
                        {
                            "match_phrase_prefix": {
                                "title": {
                                    "query": "$q",
                                    "slop": 10
                                }
                            }
                        }
                    ]
                }
            },
            "functions": [
                {
                    "filter": {
                        "type": {
                            "value": "doctor"
                        }
                    },
                    "weight": 3
                },
                {
                    "filter": {
                        "type": {
                            "value": "place"
                        }
                    },
                    "weight": 2
                },
                {
                    "filter": {
                        "type": {
                            "value": "article"
                        }
                    },
                    "weight": 1
                }
            ],
            "score_mode": "first",
            "boost_mode": "multiply"
        }
    }
}

【问题讨论】:

  • 我看过你的第一篇文章(最后被删除了),但我仍然不明白你在做什么。可能您已经在 es 网站上看到了有关 function_score 的语法,并且您遇到的错误应该不会让您感到惊讶。你到底想对你的数据和这个查询做什么?
  • @AndreiStefan,我正在尝试使用 bool 查询搜索文档,但文档类型的提升不同。
  • 不,用真实数据解释。 “我有医生、文章和地点,我想……”
  • @AndreiStefan,完成,请再看一遍。
  • 这三种类型都有title字段?

标签: php jquery elasticsearch scoring booleanquery


【解决方案1】:

试试这个:

GET /my_index/doctor,article,place/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "common": {
                "title": {
                  "query": "$q",
                  "cutoff_frequency": 0.001,
                  "low_freq_operator": "and"
                }
              }
            },
            {
              "prefix": {
                "title": "$q"
              }
            },
            {
              "match_phrase_prefix": {
                "title": {
                  "query": "$q",
                  "slop": 10
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "filter": {
            "type": {
              "value": "doctor"
            }
          },
          "boost_factor": 3
        },
        {
          "filter": {
            "type": {
              "value": "place"
            }
          },
          "boost_factor": 2
        },
        {
          "filter": {
            "type": {
              "value": "article"
            }
          },
          "boost_factor": 1
        }
      ],
      "score_mode": "first",
      "boost_mode": "multiply"
    }
  }
}

【讨论】:

  • 现在它抛出了另一个嵌套异常:QueryParsingException[[my_index] 没有注册名为 [weight] 的函数。]
  • 那个查询对我有用。确保您正确复制粘贴。
  • 我已经三次检查了我的请求。感谢您的尝试,但它在 POST 的 EDIT SECTION 中出现错误
  • 你用的是什么ES版本?
  • 版本号:“1.3.4”
猜你喜欢
  • 2016-02-01
  • 2018-02-12
  • 2019-10-11
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多