美文网首页
搜索模型的建立

搜索模型的建立

作者: 乌鲁木齐001号程序员 | 来源:发表于2020-06-12 00:01 被阅读0次

    按算分排序

    • 距离的单位是 km;
    GET /shop/_search 
    {
      "query":{
        "match": {
          "name": "凯悦"
        }
      },
      "_source": "*",
      "script_fields": {
        "distance": {
          "script": {
            "source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
            "lang": "expression",
            "params": {"lat":43.84038035,"lon":87.56498774}
          }
        }
      }
    }
    

    按距离排序

    • 要给一个入参,表明用户的位置;
    • "distance_type": "arc" 意思是球面的意思;
    GET /shop/_search 
    {
      "query":{
        "match": {
          "name": "凯悦"
        }
      },
      "_source": "*",
      "script_fields": {
        "distance": {
          "script": {
            "source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
            "lang": "expression",
            "params": {"lat":43.84038035,"lon":87.56498774}
          }
        }
      },
      "sort": [
        {
          "_geo_distance": {
            "location": {
              "lat": 43.84038035,
              "lon": 87.56498774
            },
            "order": "asc",
            "unit": "km",
            "distance_type": "arc"
          }
        }
      ]
    }
    

    基于 Function Query 的默认排序规则

    GET /shop/_search 
    {
      "explain": true, 
      "_source": "*",
      "script_fields": {
        "distance": {
          "script": {
            "source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
            "lang": "expression",
            "params": {"lat":31.23916171,"lon":121.48789949}
          }
        }
      },
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "must": [
                {"match": {"name": {"query": "凯悦","boost": 0.1}}},
                {"term": {"seller_disabled_flag": 0}}
              ]
            }
          },
          "functions": [
            {
              "gauss": {
                "location": {
                  "origin": "31.23916171,121.48789949",
                  "scale": "100km",
                  "offset": "0km",
                  "decay": 0.5
                }
              },
              "weight": 9
            },
            {
              "field_value_factor": {
                "field": "remark_score"
              },
              "weight": 0.2
            },
            {
              "field_value_factor": {
                "field": "seller_remark_score"
              },
              "weight": 0.1
            }
          ],
          "score_mode": "sum",
          "boost_mode": "sum"
        }
      },
      "sort": [ 
        {
          "_score": {
            "order":"desc"
          }
        }
      ]
    }
    

    基于 Function Query 的低价排序规则

    GET /shop/_search 
    {
      "_source": "*",
      "script_fields": {
        "distance": {
          "script": {
            "source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
            "lang": "expression",
            "params": {"lat":31.23916171,"lon":121.48789949}
          }
        }
      },
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "must": [
                {"match": {"name": {"query": "凯悦","boost": 0.1}}},
                {"term": {"seller_disabled_flag": 0}}
              ]
            }
          },
          "functions": [
            {
              "field_value_factor": {
                "field": "price_per_man"
              },
              "weight": 1
            }
          ],
          "score_mode": "sum",
          "boost_mode": "replace"
        }
      },
      "sort": [ 
        {
          "_score": {
            "order":"asc"
          }
        }
      ]
    }
    

    在 Function Query 的基础上增加标签的聚合

    GET /shop/_search 
    {
      "_source": "*",
      "script_fields": {
        "distance": {
          "script": {
            "source": "haversin(lat,lon,doc['location'].lat,doc['location'].lon)",
            "lang": "expression",
            "params": {"lat":31.23916171,"lon":121.48789949}
          }
        }
      },
      "query": {
        "function_score": {
          "query": {
            "bool": {
              "must": [
                {"match": {"name": {"query": "凯悦","boost": 0.1}}},
                {"term": {"seller_disabled_flag": 0}},
                {"term": {"tags": "落地大窗"}}
              ]
            }
          },
          "functions": [
            {
              "gauss": {
                "location": {
                  "origin": "31.23916171,121.48789949",
                  "scale": "100km",
                  "offset": "0km",
                  "decay": 0.5
                }
              },
              "weight": 9
            },
            {
              "field_value_factor": {
                "field": "remark_score"
              },
              "weight": 0.2
            },
            {
              "field_value_factor": {
                "field": "seller_remark_score"
              },
              "weight": 0.1
            }
          ],
          "score_mode": "sum",
          "boost_mode": "sum"
        }
      },
      "sort": [ 
        {
          "_score": {
            "order":"desc"
          }
        }
      ],
      "aggs": {
        "group_by_tags": {
          "terms": {
            "field": "tags"
          }
        }
      }
    }
    

    相关文章

      网友评论

          本文标题:搜索模型的建立

          本文链接:https://www.haomeiwen.com/subject/vhnbtktx.html