栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ElasticSearch学习(九)—— 检索记录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ElasticSearch学习(九)—— 检索记录

目录

检索自动补全

创建索引

插入测试数据

检索

检索记录查询

新增索引

写入测试数据

检索

java api

检索热词查询

检索

java api


  • 检索自动补全

  • 创建索引

PUT /book

POST /book/_mapping
{
  "properties": {
    "title": {
      "type": "text",
      "fields": {
        "keword":{
          "type":"keyword"
        },
        "suggest":{
          "type":"completion",
          "analyzer" : "icu_analyzer"
        }
      },
      "analyzer": "icu_analyzer"
    },
    "author":{
      "type": "text",
      "fields": {
        "keword":{
          "type":"keyword"
        },
        "suggest":{
          "type":"completion",
          "analyzer" : "icu_analyzer"
        }
      },
      "analyzer": "icu_analyzer"
    }
  }
}
  • 插入测试数据

PUT /book/_doc/1
{
  "title":"三国演义",
  "author":"罗贯中"
}
PUT /book/_doc/2
{
  "title":"三国志",
  "author":"陈寿"
}
PUT /book/_doc/3
{
  "title":"三字经",
  "author":"王应霖"
}
PUT /book/_doc/4
{
  "title":"红楼梦",
  "author":"曹雪芹"
}

  • 检索

GET /book/_search
{
  "suggest": {
    "title": {
      "text": "三国",
      "completion": {
        "field": "title.suggest",
        "size":10
      }
    }
  }
}

检索结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "suggest" : {
    "title" : [
      {
        "text" : "三国",
        "offset" : 0,
        "length" : 2,
        "options" : [
          {
            "text" : "三国志",
            "_index" : "book",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "title" : "三国志",
              "author" : "陈寿"
            }
          },
          {
            "text" : "三国演义",
            "_index" : "book",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "title" : "三国演义",
              "author" : "罗贯中"
            }
          }
        ]
      }
    ]
  }
}
  • 检索记录查询

  • 新增索引

PUT /record

POST /record/_mapping
{
  "properties": {
    "userId": {
      "type": "integer"
    },
    "word":{
      "type": "keyword"
    }
    "createTime":{
      "type": "integer"
    }
  }
}
  • 写入测试数据

PUT /record/_doc/1
{
  "userId":"233",
  "word":"三国",
  "createTime":"1635487126"
}
PUT /record/_doc/2
{
  "userId":"233",
  "word":"三国志",
  "createTime":"1635609126"
}
PUT /record/_doc/3
{
  "userId":"233",
  "word":"三国演义",
  "createTime":"1635409136"
}
PUT /record/_doc/4
{
  "userId":"233",
  "word":"三国演义",
  "createTime":"1635459126"
}
  • 检索
GET /record/_search
{
    "query":{
        "match":{
            "userId":"233"
        }
    },
    "aggs":{
        "my_so":{
            "terms":{
                "field":"word",
                "size":10,
                "order":{
                    "max_time":"desc"
                }
            },
            "aggs":{
                "max_time":{
                    "max":{
                        "field":"createTime"
                    }
                }
            }
        }
    },
    "size":0,
    "from":0
}

检索结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_so" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "三国志",
          "doc_count" : 1,
          "max_time" : {
            "value" : 1.635609126E9
          }
        },
        {
          "key" : "三国",
          "doc_count" : 1,
          "max_time" : {
            "value" : 1.635487126E9
          }
        },
        {
          "key" : "三国演义",
          "doc_count" : 2,
          "max_time" : {
            "value" : 1.635459126E9
          }
        }
      ]
    }
  }
}
  • java api

package com.biz.es.service.impl;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;



public class Test {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.0.105", 9200, "http")));
        SearchRequest searchRequest = new SearchRequest().indices("record");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("userId", 233));
        searchSourceBuilder.size(0);
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("word");
        termsAggregationBuilder.field("word")
                // 取前 top 10
                .size(10)
                // 按照子聚合进行排序
                .order(BucketOrder.aggregation("time", false));
        termsAggregationBuilder.subAggregation(AggregationBuilders.max("time").field("createTime"));

        // 构建查询
        searchSourceBuilder.aggregation(termsAggregationBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 解析结果
        Terms terms = searchResponse.getAggregations().get("word");
        for (Terms.Bucket bucket : terms.getBuckets()) {
            // 检索词
            Object key = bucket.getKey();
        }
    }
}
  • 检索热词查询

  • 检索
GET /record/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "my_aggs": {
      "terms":{
                "field":"word",
                "size":10,
                "order":{
                    "_count":"desc"
                }
            },
            "aggs":{}
    }
  },
    "size":0,
    "from":0
}

检索结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_aggs" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "三国演义",
          "doc_count" : 2
        },
        {
          "key" : "三国",
          "doc_count" : 1
        },
        {
          "key" : "三国志",
          "doc_count" : 1
        }
      ]
    }
  }
}
  • java api
package com.biz.es.service.impl;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;



public class Test {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.0.105", 9200, "http")));
        SearchRequest searchRequest = new SearchRequest().indices("record");

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.size(0);
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("word");
        termsAggregationBuilder.field("word")
                // 取前 top 10
                .size(10)
                // 按照子聚合进行排序
                .order(BucketOrder.aggregation("_count", false));

        // 构建查询
        searchSourceBuilder.aggregation(termsAggregationBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 解析结果
        Terms terms = searchResponse.getAggregations().get("word");
        for (Terms.Bucket bucket : terms.getBuckets()) {
            // 检索词
            Object key = bucket.getKey();
            // 检索次数
            long count = bucket.getDocCount();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号