查询所有(matchAllQuery)

//查询所有
@Test
public void matchAllQuery(){
    //1 执行查询
    SearchResponse searchResponse = client.prepareSearch(\"blog\").setTypes(\"article\").setQuery(QueryBuilders.matchAllQuery()).get();

    //2 打印查询结果
    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.getSourceAsString());
    }
    // 3 关闭连接
    client.close();
}

结果

查询结果有:3条
{“name”:“zyd”,“id”:“2”,“ ”:“大数据”}
{“id”:“35”,“ ”:“努力一定有收获!”,“content”:“你就是未来”}
{“id”:“5”,“ ”:“基于Lucene的搜索服务器”,“content”:“它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。大数据前景无限”,“createDate”:“2017-8-22”}

对所有字段分词查询(queryStringQuery)

其实本质是切分为一个个的汉字

//对所有字段分词查询(queryStringQuery)
@Test
public void querysStringQuuery(){
    //1 条件查询
    SearchResponse searchResponse = client.prepareSearch(\"blog\").setTypes(\"article\").setQuery(QueryBuilders.queryStringQuery(\"大数据\")).get();

    //2 打印查询结果
    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.getSourceAsString());       //获取字符串格式打印
    }

    //3 关闭连接
    client.close();
}

结果

查询结果有: 2条
{“name”:“zyd”,“id”:“2”,“ ”:“大数据”}
{“id”:“5”,“ ”:“基于Lucene的搜索服务器”,“content”:“它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。大数据前景无限”,“createDate”:“2017-8-22”}

通配符查询(wildcardQuery)

*表示多个字符(任意字符)

?:表示单个字符

@Test
public void wildQuery(){
    //1 通配符查询
    SearchResponse searchResponse = client.prepareSearch(\"blog\").setTypes(\"article\").setQuery(QueryBuilders.wildcardQuery(\"content\", \"*你*\")).get();

    //2 打印查询结果
    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.getSourceAsString());
    }
    //3 关闭连接
    client.close();
}

结果

查询结果有: 1条

{“id”:“35”,“ ”:“努力一定有收获!”,“content”:“你就是未来”}

词条查询(TermQuery)

查找的是一个个的汉字

//词条查询(TermQuery)
@Test
public void termQuery(){
    //1 第一个 field查询
    SearchResponse response = client.prepareSearch(\"blog\").setTypes(\"article\").setQuery(QueryBuilders.termQuery(\"content\",\"你\")).get();

    //2 打印查询结果
    SearchHits hits = response.getHits();
    System.out.println(\"查询结果有: \"+hits.getTotalHits()+\"条\");

    Iterator<SearchHit> iterator = hits.iterator();
    while (iterator.hasNext()){
        SearchHit next = iterator.next(); //每个查询对象
        System.out.println(next.getSourceAsString());  //获取字符串格式打印
    }
    client.close();
}

结果

查询结果有: 1条
{“id”:“35”,“ ”:“努力一定有收获!”,“content”:“你就是未来”}

模糊查询(fuzzy)

有一定的容错机制,且不区分单词的大小写

@Test
public void fuzzy(){
     //1 模糊查询
    SearchResponse response = client.prepareSearch(\"blog\").setTypes(\"article\").setQuery(QueryBuilders.fuzzyQuery(\" \", \"lucene\")).get();

    //2 打印查询结果
    SearchHits hits = response.getHits();
    System.out.println(\"查询结果有:\"+hits.getTotalHits()+\"条\");

    Iterator<SearchHit> iterator = hits.iterator();
    while (iterator.hasNext()){
        SearchHit next = iterator.next();
        System.out.println(next.getSourceAsString());
    }

     //关闭连接
    client.close();
}

映射相关操作

@Test
public void createMapping() throws IOException, ExecutionException, InterruptedException {
     //1 设置mapping
    XContentBuilder builder = XContentFactory.jsonBuilder()
            .start ()
            .start (\"article\")
            .start (\"properties\")
            .start (\"id1\")
            .field(\"type\", \"string\")
            .field(\"store\", \"yes\")
            .end ()
            .start (\" 2\")
            .field(\"type\", \"string\")
            .field(\"store\", \"no\")
            .end ()
            .start (\"content\")
            .field(\"type\", \"string\")
            .field(\"store\", \"yes\")
            .end ()
            .end ()
            .end ()
            .end ();

    //2 添加mapping

    PutMappingRequest mapping = Requests.putMappingRequest(\"blog4\").type(\"article\").source(builder);

    client.admin().indices().putMapping(mapping).get();

    //3 关闭资源
    client.close();
}

结果:

\"在这里插入图片描述\"

注意事项:

需要是一个空的索引才能插入

不然报错

java.util.concurrent.ExecutionException: RemoteTransportException[[testnote01][192.168.18.50:9300][indices:admin/mapping/put]]; nested: IllegalArgumentException[Mapper for [content] conflicts with existing mapping in other types:
[mapper [content] has different [store] values]];

收藏 打印