国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

七、ElasticSearch-高級查詢操作三

這篇具有很好參考價值的文章主要介紹了七、ElasticSearch-高級查詢操作三。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

1、高亮查詢

在進行關(guān)鍵字搜索時,搜索出的內(nèi)容中的關(guān)鍵字會顯示不同的顏色,稱之為高亮。
Elasticsearch 可以對查詢內(nèi)容中的關(guān)鍵字部分,進行標(biāo)簽和樣式 ( 高亮 ) 的設(shè)置。
在使用 match 查詢的同時,加上一個 highlight 屬性:
  • pre_tags:前置標(biāo)簽
  • post_tags:后置標(biāo)簽
  • fields:需要高亮的字段
  • title:這里聲明 title 字段需要高亮,后面可以為這個字段設(shè)置特有配置,也可以空
GET /my_index_data/_search
{
 "query": {
 "match": {
 "first_name": "tom6"
 }
 },
 "highlight": {
 "pre_tags": "<font color='red'>",
 "post_tags": "</font>",
 "fields": {
 "first_name": {}
 }
 } }


####結(jié)果
{
  "took" : 266,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7155422,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 1.7155422,
        "_source" : {
          "first_name" : "tom6",
          "last_name" : "Smith tom1",
          "age" : 39
        },
        "highlight" : {
          "first_name" : [
            "<font color='red'>tom6</font>"
          ]
        }
      }
    ]
  }
}

2、分頁查詢

from :當(dāng)前頁的起始索引,默認從 0 開始。 from = (pageNum - 1) * size
size :每頁顯示多少條
#####模糊查詢first_name為tom 按照age倒敘排序,前兩條數(shù)據(jù)
GET /my_index_data/_search
{
  "query": {
    "fuzzy": {
      "first_name": {
        "value": "tom"
      }
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2
}

########結(jié)果
{
  "took" : 44,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "first_name" : "tom3",
          "last_name" : "Smith",
          "age" : 39
        },
        "sort" : [
          39
        ]
      },
      {
        "_index" : "my_index_data",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "first_name" : "tom6",
          "last_name" : "Smith tom1",
          "age" : 39
        },
        "sort" : [
          39
        ]
      }
    ]
  }
}

3、聚合查詢

聚合允許使用者對 es 文檔進行統(tǒng)計分析,類似與關(guān)系型數(shù)據(jù)庫中的 group by ,當(dāng)然還有很
多其他的聚合,例如取最大值、平均值等等。
  • 對age取最大值 max
GET /my_index_data/_search
{
  "aggs": {
    "max_age【別名】": {
      "max": {
        "field": "age"
      }
    }
  },
  "size": 0 
}


#結(jié)果
{
  "took" : 44,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "max_age" : {
      "value" : 39.0
    }
  }
}
  • 對age取最小值min
GET /my_index_data/_search
{
  "aggs": {
    "min_age": {
      "min": {
        "field": "age"
      }
    }
  },
  "size": 0 
}

###結(jié)果
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "min_age" : {
      "value" : 20.0
    }
  }
}
  • 求和
GET /my_index_data/_search
{
  "aggs": {
    "ageSum": {
      "sum": {
        "field": "age"
      }
    }
  },
  "size": 0 
}


#####求和結(jié)果
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageSum" : {
      "value" : 255.0
    }
  }
}
  • 平均值
GET /my_index_data/_search
{
  "aggs": {
    "ageAvg": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0 
}


#平均值
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "ageAvg" : {
      "value" : 31.875
    }
  }
}
  • 數(shù)據(jù)總數(shù)count
GET /my_index_data/_search
{
  "aggs": {
    "count": {
      "value_count": {
        "field": "age"
      }
    }
  }, 
  "size": 0 
}

####結(jié)果
{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "count" : {
      "value" : 8
    }
  }
}
  • 數(shù)據(jù)總數(shù)count (去重之后)
GET /my_index_data/_search
{
  "aggs": {
    "distinct_age": {
      "cardinality": {
        "field": "age"
      }
    }
  }, 
  "size": 0 
}

####結(jié)果
{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "distinct_age" : {
      "value" : 6
    }
  }
}

4、State 聚合

stats 聚合,對某個字段一次性返回 count max , min , avg sum 五個指標(biāo)
GET /my_index_data/_search
{
 "aggs":{
 "stats_age":{
 "stats":{"field":"age"}
 }
 },
 "size":0
}



####結(jié)果
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "stats_age" : {
      "count" : 8,
      "min" : 20.0,
      "max" : 39.0,
      "avg" : 31.875,
      "sum" : 255.0
    }
  }
}

5、桶聚合查詢

桶聚和相當(dāng)于 sql 中的 group by 語句
terms 聚合,分組統(tǒng)計
#####對age group by
GET /my_index_data/_search
{
 "aggs":{
 "age_groupby":{
 "terms":{"field":"age"}
 }
 },
 "size":0
}


########結(jié)果
{
  "took" : 26,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "age_groupby" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : 30,
          "doc_count" : 2
        },
        {
          "key" : 39,
          "doc_count" : 2
        },
        {
          "key" : 20,
          "doc_count" : 1
        },
        {
          "key" : 29,
          "doc_count" : 1
        },
        {
          "key" : 33,
          "doc_count" : 1
        },
        {
          "key" : 35,
          "doc_count" : 1
        }
      ]
    }
  }
}

文章來源地址http://www.zghlxwxcb.cn/news/detail-400623.html

到了這里,關(guān)于七、ElasticSearch-高級查詢操作三的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Elasticsearch入門之Http操作(高級查詢)

    Elasticsearch入門之Http操作(高級查詢)

    Http操作: 高級查詢: 高級查詢:Elasticsearch 提供了基于 JSON 提供完整的查詢 DSL 來定義查詢 初始化數(shù)據(jù): 查詢所有文檔: 在 Postman 中,向 ES 服務(wù)器發(fā) GET 請求 :http://172.18.20.254:9200/shopping/_search 返回值: 返回值解釋: 匹配查詢: match 匹配類型查詢,會把查詢條件進行分詞

    2024年02月02日
    瀏覽(29)
  • SpringBoot操作ES進行各種高級查詢(值得收藏)

    SpringBoot操作ES進行各種高級查詢(值得收藏)

    創(chuàng)建SpringBoot項目,導(dǎo)入 ES 6.2.1 的 RestClient 依賴和 ES 依賴。在項目中直接引用 es-starter 的話會報容器初始化異常錯誤,導(dǎo)致項目無法啟動。如果有讀者解決了這個問題,歡迎留言交流 為容器定義 RestClient 對象 在 yml 文件中配置 eshost 調(diào)用相關(guān) API 執(zhí)行操作 創(chuàng)建操作索引的對象

    2024年02月03日
    瀏覽(20)
  • C#操作MySQL從入門到精通(8)——對查詢數(shù)據(jù)進行高級過濾

    C#操作MySQL從入門到精通(8)——對查詢數(shù)據(jù)進行高級過濾

    我們在查詢數(shù)據(jù)庫中數(shù)據(jù)的時候,有時候需要剔除一些我們不想要的數(shù)據(jù),這時候就需要對數(shù)據(jù)進行過濾,比如學(xué)生信息中,我只需要年齡等于18的,同時又要家鄉(xiāng)地址是安徽的,類似這種操作專欄第7篇的C#操作MySQL從入門到精通(7)——對查詢數(shù)據(jù)進行簡單過濾簡單過濾方法就

    2024年04月15日
    瀏覽(22)
  • 原生語言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文檔的基本操作,es的高級查詢.查詢結(jié)果處理. 數(shù)據(jù)聚合.相關(guān)性系數(shù)打分

    原生語言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文檔的基本操作,es的高級查詢.查詢結(jié)果處理. 數(shù)據(jù)聚合.相關(guān)性系數(shù)打分

    ? Elasticsearch 是一個分布式、高擴展、高實時的搜索與數(shù)據(jù)分析引擎。它能很方便的使大量數(shù)據(jù)具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸縮性,能使數(shù)據(jù)在生產(chǎn)環(huán)境變得更有價值。Elasticsearch 的實現(xiàn)原理主要分為以下幾個步驟,首先用戶將數(shù)據(jù)提交到Elasti

    2024年02月05日
    瀏覽(123)
  • SpringBoot操作ES進行各種高級查詢(值得收藏),阿里P7大佬手把手教你

    SpringBoot操作ES進行各種高級查詢(值得收藏),阿里P7大佬手把手教你

    for(SearchHit?hit:searchHits){ //?文檔的主鍵 String?id?=?hit.getId(); //?源文檔內(nèi)容 MapString,?Object?sourceAsMap?=?hit.getSourceAsMap(); String?name?=?(String)?sourceAsMap.get(“name”); //?由于前邊設(shè)置了源文檔字段過慮,這時description是取不到的 String?description?=?(String)?sourceAsMap.get(“description”

    2024年04月24日
    瀏覽(24)
  • 【ElasticSearch】JavaRestClient實現(xiàn)文檔查詢、排序、分頁、高亮

    【ElasticSearch】JavaRestClient實現(xiàn)文檔查詢、排序、分頁、高亮

    先初始化JavaRestClient對象: 代碼和DSL對應(yīng)上就是: 運行結(jié)果: 然后是對結(jié)果的解析,對照響應(yīng)結(jié)果: 示例代碼: 運行結(jié)果: 總結(jié): 構(gòu)建DSL是通過HighLevelRestClient中的resource()方法來實現(xiàn)的,這里包含了查詢、排序、分頁、高亮等操作 構(gòu)建查詢條件的核心部分,即查詢類型,

    2024年02月14日
    瀏覽(30)
  • ElasticSearch - 基于 JavaRestClient 查詢文檔(match、精確、復(fù)合查詢,以及排序、分頁、高亮)

    ElasticSearch - 基于 JavaRestClient 查詢文檔(match、精確、復(fù)合查詢,以及排序、分頁、高亮)

    目錄 一、基于 JavaRestClient 查詢文檔 1.1、查詢 API 演示 1.1.1、查詢基本框架 DSL 請求的對應(yīng)格式 響應(yīng)的解析 1.1.2、全文檢索查詢 1.1.3、精確查詢 1.1.4、復(fù)合查詢 1.1.5、排序和分頁 1.1.6、高亮 1.1.1、查詢基本框架 接下里通過一個 match_all 查詢所有,來演示以下基本的 API. 由上可

    2024年02月07日
    瀏覽(23)
  • ElasticSearch DSL語句(bool查詢、算分控制、地理查詢、排序、分頁、高亮等)

    ElasticSearch DSL語句(bool查詢、算分控制、地理查詢、排序、分頁、高亮等)

    查詢所有:查詢所有數(shù)據(jù),一般在測試時使用。march_all,但是一般顯示全部,有一個分頁的功能 全文檢索(full text)查詢:利用分詞器對用戶的輸入內(nèi)容進行分詞,然后去倒排索引庫匹配。例如: match_query mutil_match_query 精確查詢:根據(jù)精確詞條值查詢數(shù)據(jù),一般查找的時k

    2024年02月12日
    瀏覽(23)
  • SpringBoot 整合ElasticSearch實現(xiàn)模糊查詢,批量CRUD,排序,分頁,高亮

    SpringBoot 整合ElasticSearch實現(xiàn)模糊查詢,批量CRUD,排序,分頁,高亮

    準(zhǔn)備一個空的SpringBoot項目 寫入依賴 注意你的SpringBoot和你的es版本,一定要對應(yīng),如果不知道的可以查看這篇文章:https://blog.csdn.net/u014641168/article/details/130386872 我的版本是2.2.6,所以用的ES版本是 6.8.12,安裝es請看這篇文章:https://blog.csdn.net/u014641168/article/details/130622430 查看

    2024年02月08日
    瀏覽(41)
  • springboot——集成elasticsearch進行搜索并高亮關(guān)鍵詞

    springboot——集成elasticsearch進行搜索并高亮關(guān)鍵詞

    目錄 1.elasticsearch概述 3.springboot集成elasticsearch 4.實現(xiàn)搜索并高亮 (1)是什么: Elasticsearch 是位于 Elastic Stack 核心的分布式搜索和分析引擎。 Lucene 可以被認為是迄今為止最先進、性能最好的、功能最全的搜索引擎庫。但Lucene 只是一個基于java下的庫,需要使用 Java 并要

    2023年04月20日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包