其他分享
首页 > 其他分享> > ElasticSearch-全文检索

ElasticSearch-全文检索

作者:互联网

1.ElasticSearch-全文检索

1.1 简介:

1.2 基本概念:

 

1.3 安装:

1.3.1 安装ElasticSearch:

1.3.2 安装可视化操作工具:

1.4 配置:

1.4.1 配置ElasticSearch

1.4.2 运行Kibana:

1.4.3 设置kibana和ElasticSearch自启动

1.5 elasticsearch入门

1.5.1 命令一 _cat:

1.5.2 命令二 post&put新增数据

1.5.3 命令三 get查询数据&乐观锁字段

1.5.4 命令四 put&post修改数据

1.5.5 命令五 删除数据

1.5.6 命令六 bulk批量操作

1.6 elasticsearch进阶

1.6.1 两种查询方式:

1.6.2 QueryDSL的基本使用:

1.6.3 match全文检索

1.6.4 match_phrase短语匹配:

1.6.5 multi_match 多字段匹配:

1.6.6 bool 复合查询:

1.6.7 filter过滤:

1.6.8 term

1.6.9 aggregations(执行聚合):

1.6.10 参考:

1.6.11 映射:

1.6.12 分词:

1.7 Elasticearch-Rest-Client

1.7.1 9300: TCP

1.7.2 9200: HTTP

1.8 springboot整合Elastic Search:

1.8.1 导入依赖:

1.8.2 测试保存数据

1.8.3 测试获取数据

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-search.html

    @Test
  public void searchData() throws IOException {
      GetRequest getRequest = new GetRequest(
              "users",
              "_-2vAHIB0nzmLJLkxKWk");

      GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
      System.out.println(getResponse);
      String index = getResponse.getIndex();
      System.out.println(index);
      String id = getResponse.getId();
      System.out.println(id);
      if (getResponse.isExists()) {
          long version = getResponse.getVersion();
          System.out.println(version);
          String sourceAsString = getResponse.getSourceAsString();
          System.out.println(sourceAsString);
          Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
          System.out.println(sourceAsMap);
          byte[] sourceAsBytes = getResponse.getSourceAsBytes();
      } else {

      }
  }

查询state="AK"的文档:

{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 22,   //匹配到了22条
"relation": "eq"
},
"max_score": 3.7952394,
"hits": [{
"_index": "bank",
"_type": "account",
"_id": "210",
"_score": 3.7952394,
"_source": {
"account_number": 210,
"balance": 33946,
"firstname": "Cherry",
"lastname": "Carey",
"age": 24,
"gender": "M",
"address": "539 Tiffany Place",
"employer": "Martgo",
"email": "cherrycarey@martgo.com",
"city": "Fairacres",
"state": "AK"
}
},
          ....//省略其他
        ]
}
}

搜索address中包含mill的所有人的年龄分布以及平均年龄,平均薪资

GET bank/_search
{
 "query": {
   "match": {
     "address": "Mill"
  }
},
 "aggs": {
   "ageAgg": {
     "terms": {
       "field": "age",
       "size": 10
    }
  },
   "ageAvg": {
     "avg": {
       "field": "age"
    }
  },
   "balanceAvg": {
     "avg": {
       "field": "balance"
    }
  }
}
}

java实现

    /**
    * 复杂检索:在bank中搜索address中包含mill的所有人的年龄分布以及平均年龄,平均薪资
    * @throws IOException
    */
   @Test
   public void searchData() throws IOException {
       //1. 创建检索请求
       SearchRequest searchRequest = new SearchRequest();

       //1.1)指定索引
       searchRequest.indices("bank");
       //1.2)构造检索条件
       SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
       sourceBuilder.query(QueryBuilders.matchQuery("address","Mill"));

       //1.2.1)按照年龄分布进行聚合
       TermsAggregationBuilder ageAgg=AggregationBuilders.terms("ageAgg").field("age").size(10);
       sourceBuilder.aggregation(ageAgg);

       //1.2.2)计算平均年龄
       AvgAggregationBuilder ageAvg = AggregationBuilders.avg("ageAvg").field("age");
       sourceBuilder.aggregation(ageAvg);
       //1.2.3)计算平均薪资
       AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
       sourceBuilder.aggregation(balanceAvg);

       System.out.println("检索条件:"+sourceBuilder);
       searchRequest.source(sourceBuilder);
       //2. 执行检索
       SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
       System.out.println("检索结果:"+searchResponse);

       //3. 将检索结果封装为Bean
       SearchHits hits = searchResponse.getHits();
       SearchHit[] searchHits = hits.getHits();
       for (SearchHit searchHit : searchHits) {
           String sourceAsString = searchHit.getSourceAsString();
           Account account = JSON.parseObject(sourceAsString, Account.class);
           System.out.println(account);

      }

       //4. 获取聚合信息
       Aggregations aggregations = searchResponse.getAggregations();

       Terms ageAgg1 = aggregations.get("ageAgg");

       for (Terms.Bucket bucket : ageAgg1.getBuckets()) {
           String keyAsString = bucket.getKeyAsString();
           System.out.println("年龄:"+keyAsString+" ==> "+bucket.getDocCount());
      }
       Avg ageAvg1 = aggregations.get("ageAvg");
       System.out.println("平均年龄:"+ageAvg1.getValue());

       Avg balanceAvg1 = aggregations.get("balanceAvg");
       System.out.println("平均薪资:"+balanceAvg1.getValue());


  }  

可以尝试对比打印的条件和执行结果,和前面的ElasticSearch的检索语句和检索结果进行比较;

 

其他

1. kibana控制台命令

ctrl+home:回到文档首部;

ctril+end:回到文档尾部。

 

 

 

标签:索引,mydata,全文检索,ElasticSearch,nginx,elasticsearch,root,match
来源: https://www.cnblogs.com/txzmy/p/16652709.html