Elasticsearch查询那些事

工具  

服务:            http://localhost:8080/elastic/all

elasticsearch 7.9.2    http://localhost:9200/

kibana 7.9.2         http://localhost:5601/

查询结果构成

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 1,                            //耗费了几毫秒
  "timed_out" : false,                   //是否超时
  "_shards" : {                          //数据拆成1个分片
    "total" : 5, 
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 13,                       //查询结果的数量,这里有 13 个 document
      "relation" : "eq"
    },
    "max_score" : null,                  //score的含义就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也越高
    "hits" : [                           //包含了匹配搜索的document的详细数据
      {
        "_index" : "blog",
        "_type" : "blog",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "id" : 1,
          "summary" : "title1",
          "content" : "summary1",
          "title" : "content1"
        },
        "sort" : [
          1
        ]
      },
      {
        "_index" : "blog",
        "_type" : "blog",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "id" : 2,
          "summary" : "title2",
          "content" : "summary2",
          "title" : "content2"
        },
        "sort" : [
          2
        ]
      },
      {
        "_index" : "blog",
        "_type" : "blog",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "id" : 3,
          "summary" : "title3",
          "content" : "summary3",
          "title" : "content3"
        },
        "sort" : [
          3
        ]
      }
    ]
  }
}

初始化ES的数据

[
{"id":1,"summary":"title1","content":"summary1 term","title":"content1","dateTime":"2020-10-10T03:55:24.165+0000"},
{"id":2,"summary":"title2","content":"summary2 term","title":"content2","dateTime":"2020-10-10T03:55:24.284+0000"},
{"id":3,"summary":"title3","content":"summary3 term","title":"content3","dateTime":"2020-10-10T03:55:24.421+0000"},
{"id":4,"summary":"title4","content":"summary4 term","title":"content4","dateTime":"2020-10-10T03:55:24.596+0000"},
{"id":5,"summary":"title5","content":"summary5 term","title":"content5","dateTime":"2020-10-10T03:55:24.772+0000"},
{"id":6,"summary":"title6","content":"summary6 term","title":"content6","dateTime":"2020-10-10T03:55:24.989+0000"},
{"id":7,"summary":"title7","content":"summary7 term","title":"content7","dateTime":"2020-10-10T03:55:25.203+0000"},
{"id":8,"summary":"title8","content":"summary8 term","title":"content8","dateTime":"2020-10-10T03:55:25.317+0000"},
{"id":9,"summary":"title9","content":"summary9 term","title":"content9","dateTime":"2020-10-10T03:55:25.422+0000"},
{"id":10,"summary":"title10","content":"summary10 term","title":"content10","dateTime":"2020-10-10T03:55:25.542+0000"},
{"id":11,"summary":"title11","content":"summary11 term","title":"content11","dateTime":"2020-10-10T03:55:25.648+0000"},
{"id":12,"summary":"title12","content":"summary12 term","title":"content12","dateTime":"2020-10-10T03:55:25.763+0000"},
{"id":13,"summary":"title13","content":"summary13 term","title":"content13","dateTime":"2020-10-10T03:55:25.871+0000"}
]

HEAD 索引名

查询所有

GET blog/blog/_search
{
  "query": {
    "match_all": {}
  },
  "sort":{
    "id":"asc"
  },
  "from": 1,
  "size": 13
}

查询id为13的

GET blog/blog/13

自定义查询的结果集

GET /blog/blog/_search
{
  "query": {
    "match": {
      "summary": "title1"
    }
  },
  "sort":[
    {"id":"asc"}
    ],
  "_source": ["summary","content"]
}

模糊匹配查询

GET /blog/blog/_search
{
  "query": {
    "wildcard": {
      "content": {
        "value": "*term*"
      }
    }
  },
  "sort":[
    {"id":"asc"}
    ],
  "_source": ["summary","content"]
}

也可以这样查询

GET /blog/blog/_search?q=summary:title2&sort=id:asc

match进行搜索的时候,会先进行分词拆分,拆完后,再来匹配,并且属于或的关系

GET /blog/blog/_search
{
  "query": {
    "match": {
      "content": "summary4 term"
    }
  }
}

match_phrase称为短语搜索,要求所有的分词必须同时出现在文档中,同时位置必须紧邻一致。

GET /blog/blog/_search
{
  "query": {
    "match_phrase": {
      "content": "summary4 term"
    }
  }
}

term是代表完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词拆解。

GET /blog/blog/_search
{
  "query": {
     "term": {
      "content": "term"
    }
  }
}

term属于精确匹配,只能查单个词

GET /blog/blog/_search
{
  "query": {
     "term": {
      "content": "summary4"
    }
  }
}

terms里的[ ] 多个是或者的关系,只要满足其中一个词就可以

 GET /blog/blog/_search
{
  "query": {
     "terms": {
      "content": ["summary4", "term"]
    }
  }
}

GET blog/blog/_search
{
  "query": {
    "terms": {
      "id": [ "1","7", "13" ] 
    }
  },
  "sort":{
    "id":"asc"
  }
}

想要通知满足两个词的话,就得使用bool的must来做

 GET /blog/blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "content": "summary4"
          }
        },
        {
          "term": {
            "content": "term"
          }
        }
      ],
     "filter": {
        "range": {
          "id": {
            "gt": 3
          }
        }
      }
    }
  }
}

高亮搜索

GET /blog/blog/_search
{
  "query": {
    "match": {
      "content": "summary4"
    }
  },
  "highlight": {
      "pre_tags": [
          "<em class=\"c_color\">"
      ],
      "post_tags": [
        "</em>"
      ],
    "fields": {
      "content": {}
    }
  }
}

来源参考:  https://www.jianshu.com/p/d5583dff4157

查询语句下载:https://download.csdn.net/download/tianruozhaomi/12917264

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值