es之java各种查询操作

matchAllQuery匹配所有文档
queryStringQuery 基于Lucene的字段检索
wildcardQuery 通配符查询匹配多个字符,?匹配1个字符*
termQuery 词条查询
matchQuery 字段查询
idsQuery 标识符查询
fuzzyQuery 文档相似度查询
includeLower includeUpper 范围查询
boolQuery 组合查询(复杂查询)
SortOrder 排序查询

插入样例数据:

插入样例数据:

curl -XPOST http://hadoop01:9200/sanguo/dahan/_bulk -d '
{ "index": {}}    
{ "studentNo" : 1, "name" : "刘备", "male" : "男", "age" : 24 , "birthday" : "1985-02-03" , "classNo" : 1 , "address" : "湖南省长沙市" , "isLeader" : true}
{ "index": {}}            
{ "studentNo" : 2, "name" : "关羽", "male" : "男", "age" : 22 , "birthday" : "1987-08-23" , "classNo" : 2, "address" : "四川省成都市" , "isLeader" : false}
{ "index": {}}                                
{ "studentNo" : 3, "name" : "糜夫人", "male" : "女", "age" : 19 , "birthday" : "1990-06-12" , "classNo" : 1 , "address" : "上海市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 4, "name" : "张飞", "male" : "男", "age" : 20 , "birthday" : "1989-07-30" , "classNo" : 3 , "address" : "北京市" , "isLeader" : false}
{ "index": {}}
{ "studentNo" : 5, "name" : "诸葛亮", "male" : "男", "age" : 18 , "birthday" : "1992-04-27" , "classNo" : 2 , "address" : "江苏省南京市" , "isLeader" : true}
{ "index": {}}    
{ "studentNo" : 6, "name" : "孙尚香", "male" : "女", "age" : 16 , "birthday" : "1994-05-21" , "classNo" : 3 , "address" : "广东省深圳市" , "isLeader" : false}
{ "index": {}}              
{ "studentNo" : 7, "name" : "马超", "male" : "男", "age" : 19 , "birthday" : "1991-10-20" , "classNo" : 1 , "address" : "黑龙江省哈尔滨市" , "isLeader" : false}
{ "index": {}}                        
{ "studentNo" : 8, "name" : "赵云", "male" : "男", "age" : 23 , "birthday" : "1986-10-26" , "classNo" : 2 , "address" : "浙江省杭州市" , "isLeader" : false}
'

1:查询所有

matchAllQuery()匹配所有文件

match_all查询是Elasticsearch中最简单的查询之一。它使我们能够匹配索引中的所有文件。

/**
*matchAllQuery()匹配所有文件
match_all查询是Elasticsearch中最简单的查询之一。它使我们能够匹配索引中的所有文件
* */
@Test
public void searchAll(){
   SearchResponse searchResponse = client.prepareSearch("sanguo")
          .setTypes("dahan").setQuery(QueryBuilders.matchAllQuery())
          .get();

   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

2:解析查询字符串

相比其他可用的查询,query_string查询支持全部的Apache Lucene查询语法

针对多字段的query_string查询

/**
* 相比其他可用的查询,query_string查询支持全部的Apache Lucene查询语法
针对多字段的query_string查询

* */
@Test
public void query_String(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.queryStringQuery("孙尚香")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

3:通配符查询(wildcardQuery)

*匹配多个字符,?匹配1个字符

注意:避免* 开始, 会检索大量内容造成效率缓慢

/**
* *匹配多个字符,?匹配1个字符
注意:避免* 开始, 会检索大量内容造成效率缓慢
* */
@Test
public void wildcardQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.wildcardQuery("address", "广东*")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

4:词条查询(termQuery)

词条查询是Elasticsearch中的一个简单查询。它仅匹配在给定字段中含有该词条的文档,而且是确切的、未经分析的词条

termQuery("key", obj) 完全匹配

termsQuery("key", obj1, obj2..)

一次匹配多个值,有一个值是正确的,就可以查询出数据

/**
* 词条查询是Elasticsearch中的一个简单查询。它仅匹配在给定字段中含有该词条的文档,而
且是确切的、未经分析的词条
termQuery("key", obj) 完全匹配
termsQuery("key", obj1, obj2..)   一次匹配多个值,只有有一个值是正确的,就可以查询出数据

* */
@Test
public void termQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.termsQuery("name", "张飞","刘备","关羽")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

5:字段匹配查询

match**查询把query参数中的值拿出来,加以分析,然后构建相应的查询**。使用match查询时,Elasticsearch将对一个字段选择合适的分析器,所以可以确定,传给match查询的词条将被建立索引时相同的分析器处理。

 

/**
* match query搜索的时候,首先会解析查询字符串,进行分词,然后查询,
而term query,输入的查询内容是什么,就会按照什么去查询,并不会解析查询内容,对它分词。
* */
@Test
public void MatchQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
.setQuery(QueryBuilders.matchQuery("address", " 上海")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }


}

Matchquery 和termquery的区别:

match:匹配的时候,会将查询的关键字进行分词,然后根据分词后的结果进行查询。
term:直接使用关键字进行查询,不对关键字进行分词。

multiMatchQuery("text", "field1", "field2"..); 匹配多个字段

mutilMatchQuery:

/**
* multiMatchQuery(要搜索的值 , 在哪些字段上)
* */
@Test
public void mutileMatchQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.multiMatchQuery("北京", "address","name")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

6:只查询ID(标识符查询)

标识符查询是一个简单的查询,仅用提供的标识符来过滤返回的文档。

/**
* 按照id进行查询,通过id返回我们想要的结果
* */
@Test
public void idsQuery() {
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.idsQuery().addIds("AWNkQSCJzU0_wTuf7egi")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

7:相似度查询

插入两条测试数据:

PUT /sanguo/dahan/123
{
 "name": "曹操",
 "male" : "男",
 "age" : 40 ,
 "birthday" : "1975-02-03" ,
 "classNo" : 1 ,
 "address" : "海淀区坐落于北京市" ,
 "isLeader" : true
}

PUT /sanguo/dahan/234
{
 "name": "董卓",
 "male" : "男",
 "age" : 35 ,
 "birthday" : "1975-02-03" ,
 "classNo" : 1 ,
 "address" : "北京it程序员" ,
 "isLeader" : true
}

fuzzy查询是模糊查询中的第三种类型,它基于编辑距离算法来匹配文档

/**
* 相似度查询:fuzzy查询是模糊查询中的第三种类型,它基于编辑距离算法来匹配文档
* */
@Test
public void fuzzyQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.fuzzyQuery("address", "北京市")).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   System.out.println("查询结果有:" + hits.getTotalHits() + "条");
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("得分:"+next.getScore());
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("===============================================");
  }
}

8:范围查询

范围查询使我们能够找到在某一字段值在某个范围里的文档,字段可以是数值型,也可以是

基于字符串的

includeLower(true):包含上界

IncludeUpper(true):包含下界

/**
范围查询使我们能够找到在某一字段值在某个范围里的文档,字段可以是数值型,也可以是
基于字符串的
includeLower(true):包含上界
IncludeUpper(true):包含下界
* */
@Test
public void rangeQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.rangeQuery("age").from(18).to(22)
                  .includeLower(true)
                  .includeUpper(false))
          .get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("得分:"+next.getScore());
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("studentNo : "+ next.getSource().get("studentNo"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("birthday : "+ next.getSource().get("birthday"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("age : "+ next.getSource().get("age"));
       System.out.println("===============================================");
  }
}

9:组合查询(复杂查询)

must(QueryBuilders) : AND
mustNot(QueryBuilders): NOT
should(QueryBuilders):OR 

插入一条数据:

PUT /sanguo/dahan/345
{
 "name": "貂蝉",
 "male" : "女",
 "age" : 16 ,
 "birthday" : "2000-02-03" ,
 "classNo" : 1 ,
 "address" : "北京市女it程序员" ,
 "isLeader" : true
}

 

  /*
 * 组合查询:
 *
must(QueryBuilders) : AND
mustNot(QueryBuilders): NOT
should(QueryBuilders):OR

 * */
 @Test
 public void boolQuery(){
     SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
            .setQuery(QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchQuery("address", "程序员"))
                    .must(QueryBuilders.termQuery("male", "女"))).get();
     SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
     Iterator<SearchHit> iterator = hits.iterator();
     while (iterator.hasNext()) {
         SearchHit next = iterator.next();
         System.out.println("得分:"+next.getScore());
         System.out.println("name : "+ next.getSource().get("name"));
         System.out.println("studentNo : "+ next.getSource().get("studentNo"));
         System.out.println("male : "+ next.getSource().get("male"));
         System.out.println("birthday : "+ next.getSource().get("birthday"));
         System.out.println("classNo : "+ next.getSource().get("classNo"));
         System.out.println("address : "+ next.getSource().get("address"));
         System.out.println("age : "+ next.getSource().get("age"));
         System.out.println("===============================================");
    }
}

10:排序查询

/**
* ASC : 正序(从小到大)
* DESC: 倒序(从大到小)
* */
@Test
public void SortOrderQuery(){
   SearchResponse searchResponse = client.prepareSearch("sanguo").setTypes("dahan")
          .setQuery(QueryBuilders.matchAllQuery())
          .addSort("age", SortOrder.ASC).get();
   SearchHits hits = searchResponse.getHits(); // 获取命中次数,查询结果有多少对象
   Iterator<SearchHit> iterator = hits.iterator();
   while (iterator.hasNext()) {
       SearchHit next = iterator.next();
       System.out.println("name : "+ next.getSource().get("name"));
       System.out.println("male : "+ next.getSource().get("male"));
       System.out.println("classNo : "+ next.getSource().get("classNo"));
       System.out.println("address : "+ next.getSource().get("address"));
       System.out.println("age : "+ next.getSource().get("age"));
       System.out.println("===============================================");
  }
}

 

posted @ 2017-05-22 23:29  niutao  阅读(29257)  评论(0编辑  收藏  举报