您当前的位置: 首页 >  搜索

Dongguo丶

暂无认证

  • 5浏览

    0关注

    472博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

42实战多搜索条件组合查询

Dongguo丶 发布时间:2021-11-08 22:35:01 ,浏览量:5

bool综合相关度分数

每个子查询都会计算一个document针对它的相关度分数,然后bool综合所有分数,合并为一个分数,当然filter是不会计算分数的

GET /website/article/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "elasticsearch"
          }
        }
      ],
      "should": [
        {
          "match": {
            "content": "elasticsearch"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "author_id": 111
          }
        }
      ]
    }
  }
}

响应结果

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.5408423,
    "hits": [
      {
        "_index": "website",
        "_type": "article",
        "_id": "3",
        "_score": 0.5408423,
        "_source": {
          "title": "my elasticsearch article",
          "content": "elasticsearch is very bad",
          "author_id": 113
        }
      },
      {
        "_index": "website",
        "_type": "article",
        "_id": "2",
        "_score": 0.25316024,
        "_source": {
          "title": "my elasticsearch article",
          "content": "es is very good",
          "author_id": 112
        }
      }
    ]
  }
}
constant_score

当我们不关心检索词频率TF(Term Frequency)对搜索结果排序的影响时,可以使用constant_score将查询语句query或者过滤语句filter包装起来。

GET /company/employee/_search 
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "age": {
            "gte": 30
          }
        }
      }
    }
  }
}

响应结果

{
  "took": 102,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "company",
        "_type": "employee",
        "_id": "2",
        "_score": 1,
        "_source": {
          "address": {
            "country": "china",
            "province": "jiangsu",
            "city": "nanjing"
          },
          "name": "tom",
          "age": 30,
          "join_date": "2016-01-01"
        }
      },
      {
        "_index": "company",
        "_type": "employee",
        "_id": "3",
        "_score": 1,
        "_source": {
          "address": {
            "country": "china",
            "province": "shanxi",
            "city": "xian"
          },
          "name": "marry",
          "age": 35,
          "join_date": "2015-01-01"
        }
      }
    ]
  }
}
关注
打赏
1638062488
查看更多评论
立即登录/注册

微信扫码登录

0.0876s