如何在Elasticsearch中使用带有function_score的bool查询?

时间:2014-11-28 11:26:21

标签: php jquery elasticsearch scoring booleanquery

我有3种类型的数据[医生,文章,地方]我希望根据bool查询匹配他们的文档。但是我想给医生类型一个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的我已编辑的QUERY STRAIGHT:

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"
        }
    }
}

1 个答案:

答案 0 :(得分:3)

试试这个:

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"
    }
  }
}