elasticsearch的基本操作5


#delete-by-query 删除查询内容
POST /novel/_delete_by_query
{
  "query":{
    "regexp": {
      "name": "鬼吹灯[0-9]?"
    }
  }
}
#查询name可能为鬼吹灯或鬼吹灯2
#作者不是萧鼎2
#descr包含嘻嘻和哈哈
#bool查询
POST /novel/_search
{
  "query":{
    "bool": {
      "should": [
        {
          "term": {
            "name": {
              "value": "鬼吹灯"
            }
          }
        },
        {
          "term": {
            "name": {
              "value": "鬼吹灯2"
            }
          }
        }
      ],
      "must_not": [
        {
          "term": {
            "author": {
              "value": "萧鼎2"
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "descr": "嘻嘻"
          }
        },
        {
          "match": {
            "descr": "哈哈"
          }
        }
      ]
    }
  }
}

test2026-08-13 15:56


#boosting查询
POST /novel/_search
{
  "query": {
    "boosting": {
      "positive": {#匹配查询结果集
        "match": {
          "descr": "嘻嘻"
        }
      },
      "negative": {#匹配上positive后又匹配negative
        "match": {
          "descr": "哈哈"
        }
      },
      "negative_boost": 0.2#将分数乘以系数
    }
  }
}
#filter查询
POST /novel/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "match": {
            "descr": "嘻嘻"
          }
        },
        {
          "range": {
            "count": {
              "lte": 1000000,
              "gte": 10
            }
          }
        }
      ]
    }
  }
}

#highlight查询
POST /novel/_search
{
  "query": {
    "match": {
      "descr": "嘻嘻"
    }
  },
  "highlight": {
    "fields": {
      "descr":{}
    },
    "post_tags": "</em>",
    "pre_tags": "<em>",
    "fragment_size":10
  }
}

test2026-08-13 16:17


#去重计数
#cardinality 不能直接用在 text 字段上,ES 会直接报错。
POST /novel/_search
{
  "aggs": {
    "qwe": {
      "cardinality": {
        "field": "author"
      }
    }
  }
}
结果中包含:

"aggregations": {
    "qwe": {
      "value": 9
    }
}



test2026-08-13 16:29


test2026-08-13 16:33