ElasticSearch-将功能分数查询字段值因子与匹配查询一起使用

时间:2018-10-12 09:32:59

标签: elasticsearch

我想在结合正常匹配和布尔查询的正式文档上使用该示例。该怎么做?

GET /_search
{
    "query": {
        "function_score": {
            "field_value_factor": {
                "field": "likes",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
            }
        }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor

匹配查询:

    "query": {
        "match" : {
            "name" : "star wars"
        }
    }

布尔查询:

{
"query": {
    "bool": {
    "must": [
        {
        "match": {
            "name": "Star Wars"
        }
        }
    ],
    "should": [
        {
        "term": {
            "name.keyword": {
            "value": "Star Wars"
            }
        }
        }
    ]
    }
}
}

1 个答案:

答案 0 :(得分:1)

是的,这应该可行。如果您在documentation that you linked to中进一步阅读,将有一个示例:

GET /_search
{
    "query": {
        "function_score": {
          "functions": [
            {
              "gauss": {
                "price": {
                  "origin": "0",
                  "scale": "20"
                }
              }
            },
            {
              "gauss": {
                "location": {
                  "origin": "11, 12",
                  "scale": "2km"
                }
              }
            }
          ],
          "query": {
            "match": {
              "properties": "balcony"
            }
          },
          "score_mode": "multiply"
        }
    }
}

针对您的用例进行略微修改应如下所示:

GET /_search
{
    "query": {
        "function_score": {
          "functions": [
            {
              "field_value_factor": {
                "field": "likes",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
              }
            }
          ],
          "query": {
            "match": {
              "name": "Star Wars"
            }
          },
          "score_mode": "multiply"
        }
    }
}

免责声明:我还没有测试过,只是从文档中删除。