elasticsearch高级查询api

Posted 小徐敲java

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch高级查询api相关的知识,希望对你有一定的参考价值。

yml配置

  #es配置
spring:
  elasticsearch:
    rest:
      uris: 192.168.16.188:9200

添加依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

使用编程的形式设置连接的ES服务器,并获取客户端对象,配置ES服务器地址与端口9200,记得客户端使用完毕需要手工关闭。由于当前客户端是手工维护的,因此不能通过自动装配的形式加载对象

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootTest.class)
public class JeecgTest 
    @Test
//    @SneakyThrows
    public void name() 

        HttpHost host = HttpHost.create("http://localhost:9200");
        RestClientBuilder builder = RestClient.builder(host);

        client = new RestHighLevelClient(builder);

        try 
            client.close();
         catch (IOException e) 
            throw new RuntimeException(e);
        
   

使用客户端对象操作ES,例如创建索引

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootTest.class)
public class JeecgTest 
    private RestHighLevelClient client;
      @Test
      void testCreateIndex() throws IOException 
          HttpHost host = HttpHost.create("http://localhost:9200");
          RestClientBuilder builder = RestClient.builder(host);
          client = new RestHighLevelClient(builder);
        
          CreateIndexRequest request = new CreateIndexRequest("books");
          client.indices().create(request, RequestOptions.DEFAULT); 
        
          client.close();
      


添加文档,添加文档使用的请求对象是IndexRequest,与创建索引使用的请求对象不同

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootTest.class)
public class JeecgTest 
    @Test
    public void testCreateIndex() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", 20);
        IndexRequest request = new IndexRequest("user");
        String json = JSON.toJSONString(map);
        request.source(json, XContentType.JSON);
        IndexResponse index = client.index(request, RequestOptions.DEFAULT);
        client.close();
    


/增量更新文档

  @Test
  public void testUpdateDoc() throws IOException 
      HttpHost host = HttpHost.create("http://192.168.16.188:9200");
      RestClientBuilder builder = RestClient.builder(host);
      client = new RestHighLevelClient(builder);
      UpdateRequest updateRequest = new UpdateRequest("user", "88");
      updateRequest.timeout("1s");
      User user = new User();
      user.setAge(222);
      updateRequest.doc(JSON.toJSONString(user), XContentType.JSON);
      UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);
      client.close();
  

批量添加文档:批量做时,先创建一个BulkRequest的对象,可以将该对象理解为是一个保存request对象的容器,将所有的请求都初始化好后,添加到BulkRequest对象中,再使用BulkRequest对象的bulk方法,一次性执行完毕

    @Test
    //批量添加文档
    public void testCreateDocAll() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        List<Map> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < 3; i++) 
            map.put("aa" + i, i);
            list.add(map);
        
        BulkRequest bulk = new BulkRequest();
        for (Map map1 : list) 
            IndexRequest request = new IndexRequest("user");
            String json = JSON.toJSONString(map1);
            request.source(json, XContentType.JSON);
            bulk.add(request);
        
        client.bulk(bulk, RequestOptions.DEFAULT);
        client.close();
    

按id查询1文档:根据id查询文档使用的请求对象是GetRequest

    @Test
    //按id查询
    public void getById() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        GetRequest request = new GetRequest("user", "88");
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        String json = response.getSourceAsString();
        System.out.println(json);
        client.close();
    

通过id查询2

    @Test
    //按id查询
    public void getById2() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        SearchRequest request = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.termQuery("_id",88));
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) 
            String source = hit.getSourceAsString();
            System.out.println(source);
        
        client.close();
    

按条件查询文档:按条件查询文档使用的请求对象是SearchRequest,查询时调用SearchRequest对象的termQuery方法,需要给出查询属性名,此处支持使用合并字段,也就是前面定义索引属性时添加的all属性

    @Test
    //按条件查询
    public void getBySearch() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        SearchRequest request = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("name","张三"));
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) 
            String source = hit.getSourceAsString();
            System.out.println(source);
        
        client.close();
    

按条件高亮查询

    @Test
    //按条件高亮查询
    public void getBySearch2() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        SearchRequest request = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchPhraseQuery("name","张三"));
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        searchSourceBuilder.highlighter(highlightBuilder.field("name"));
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) 
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            System.out.println(highlightFields);
        
        client.close();
    

全文高亮查询

    @Test
    //全文高亮查询
    public void getBySearch2() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        SearchRequest request = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery("张三"));
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        searchSourceBuilder.highlighter(highlightBuilder.field("*"));
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) 
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            System.out.println(highlightFields);
        
        client.close();
    

分页查询

    @Test
    //分页查询
    public void getBySearch3() throws IOException 
        // 开始查询的记录数
        //页码
        Integer pageNum=1;
        //页数
        Integer pageSize=2;
        int start = (pageNum - 1) * pageSize;
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        SearchRequest request = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchSourceBuilder.from(start);
        searchSourceBuilder.size(pageSize);
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) 
            String sourceAsString = hit.getSourceAsString();
            System.out.println(sourceAsString);
        
        client.close();
    

MultiQuery 全部字段联合搜索

    @Test
    // MultiQuery 全部字段联合搜索 
    public void getBySearch4() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client = new RestHighLevelClient(builder);
        SearchRequest request = new SearchRequest("user");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.multiMatchQuery("三"));
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        for (SearchHit hit : hits) 
            String source = hit.getSourceAsString();
            System.out.println(source);
        
        client.close();
    

MultiQuery 多字段联合搜索 ,使用多字段查询的时候,查询的字段要和查询的内容类型一直,不然就会报错,类似age字段是int类型,和name字段是string类型查询就会报错

    @Test
    // MultiQuery 多字段联合搜索
    public void getBySearch5() throws IOException 
        HttpHost host = HttpHost.create("http://192.168.16.188:9200");
        RestClientBuilder builder = RestClient.builder(host);
        client 
https://www.imooc.com/video/15759/0

ElasticSearch查询





1,子条件查询:特定字段查询所指特定值

1.1query context,有_score
1.1.1全文本查询,针对文本类型数据
1.1.1.1 模糊匹配
POST http://127.0.0.1/book/_search
{
    "query":{
        "match":{
            "author":"瓦力"
        }
    }
}
{
    "query":{
        "match":{
            "title":"ElasticSearch入门"
        }
    }
}


1.1.1.2 习语匹配
{
    "query":{
        "match_phrase":{
            "title":"ElasticSearch入门"
        }
    }
}

1.1.1.3 多字段匹配
{
    "query":{
        "multi_match":{
            "query":"瓦力",
            "fields":["author","title"]
        }
    }
}

1.1.1.4 语法查询
{
    "query":{
        "query_string":{
            "query":"ElasticSearch AND 大法"
        }
    }
}

{
    "query":{
        "query_string":{
            "query":"(ElasticSearch AND 大法) OR Python"
        }
    }
}

{
    "query":{
        "query_string":{
            "query":"瓦力 OR ElasticSearch",
            "fields":["title","author"]
        }
    }
}
1.1.2字段级别查询,针对结构化数据,如数字、日期等

{
    "query":{
        "term":{
            "word_count":1000
        }
    }
}

{
    "query":{
        "term":{
            "author":"瓦力"
        }
    }
}
{
    "query":{
        "range":{
            "word_count":{
                "gte":1000,
                "lte":2000
            }
        }
    }
}
{
    "query":{
        "range":{
            "word_count":{
                "gt":1000,
                "lte":2000
            }
        }
    }
}
{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2017-01-01",
                "lte":"2017-12-31"
            }
        }
    }
}
{
    "query":{
        "range":{
            "publish_date":{
                "gte":"2017-01-01",
                "lte":"now"
            }
        }
    }
}
1.2filter context
filter表示查找是不是

{
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "word_count": 1000
                }
            }
        }
    }
}



2,复合条件查询:以一定的逻辑组合子条件查询


2.1 固定分数查询
{
    "query": {
        "match": {
            "title": "ElasticSearch"
        }
    }
}

{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title": "ElasticSearch"
                }
            }
        }
    }
}

{
    "query": {
        "constant_score": {
            "filter": {
                "match": {
                    "title": "ElasticSearch"
                }
            },
            "boost": 2
        }
    }
}

2.2 bool查询

should - 表示 或者
{
    "query": {
        "bool": { 
            "should": [
                {
                    "match":{
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                    
                }
                
            ]
        }
    }
}

must - 表示 必须

{
    "query": {
        "bool": { 
            "must": [
                {
                    "match":{
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                    
                }
                
            ]
        }
    }
}

{
    "query": {
        "bool": { 
            "must": [
                {
                    "match":{
                        "author": "瓦力"
                    }
                },
                {
                    "match": {
                        "title": "ElasticSearch"
                    }
                    
                }
                
            ],
            "filter": [
                {
                    "term": {
                        "word_count": 1000
                    }
                }
            ]
        }
    }
}
{
    "query": {
        "bool": { 
            "must_not": {
                "term": {
                    "author": "瓦力"
                }
            }
        }
    }
}

 

https://www.imooc.com/video/15759/0
ElasticSearch查询




1,子条件查询:特定字段查询所指特定值
1.1query context,有_score1.1.1全文本查询,针对文本类型数据1.1.1.1 模糊匹配POST http://127.0.0.1/book/_search{    "query":{        "match":{            "author":"瓦力"        }    }}{    "query":{        "match":{            "title":"ElasticSearch入门"        }    }}

1.1.1.2 习语匹配{    "query":{        "match_phrase":{            "title":"ElasticSearch入门"        }    }}
1.1.1.3 多字段匹配{    "query":{        "multi_match":{            "query":"瓦力",            "fields":["author","title"]        }    }}
1.1.1.4 语法查询{    "query":{        "query_string":{            "query":"ElasticSearch AND 大法"        }    }}
{    "query":{        "query_string":{            "query":"(ElasticSearch AND 大法) OR Python"        }    }}
{    "query":{        "query_string":{            "query":"瓦力 OR ElasticSearch",            "fields":["title","author"]        }    }}1.1.2字段级别查询,针对结构化数据,如数字、日期等
{    "query":{        "term":{            "word_count":1000        }    }}
{    "query":{        "term":{            "author":"瓦力"        }    }}{    "query":{        "range":{            "word_count":{                "gte":1000,                "lte":2000            }        }    }}{    "query":{        "range":{            "word_count":{                "gt":1000,                "lte":2000            }        }    }}{    "query":{        "range":{            "publish_date":{                "gte":"2017-01-01",                "lte":"2017-12-31"            }        }    }}{    "query":{        "range":{            "publish_date":{                "gte":"2017-01-01",                "lte":"now"            }        }    }}1.2filter contextfilter表示查找是不是
{"query": {"bool": {"filter": {"term": {"word_count": 1000}}}}}


2,复合条件查询:以一定的逻辑组合子条件查询

2.1 固定分数查询{"query": {"match": {"title": "ElasticSearch"}}}
{"query": {"constant_score": {"filter": {"match": {"title": "ElasticSearch"}}}}}
{"query": {"constant_score": {"filter": {"match": {"title": "ElasticSearch"}},"boost": 2}}}
2.2 bool查询
should - 表示 或者{"query": {"bool": { "should": [{"match":{"author": "瓦力"}},{"match": {"title": "ElasticSearch"}}]}}}
must - 表示 必须
{"query": {"bool": { "must": [{"match":{"author": "瓦力"}},{"match": {"title": "ElasticSearch"}}]}}}
{"query": {"bool": { "must": [{"match":{"author": "瓦力"}},{"match": {"title": "ElasticSearch"}}],"filter": [{"term": {"word_count": 1000}}]}}}{"query": {"bool": { "must_not": {"term": {"author": "瓦力"}}}}}

以上是关于elasticsearch高级查询api的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch高级操作

elasticsearch高级查询api

elasticsearch高级组合查询ava

第07讲:Java操作之ElasticSearch高级查询

Elasticsearch:高级调优 - 查找和修复慢速 Elasticsearch 查询

Elasticsearch:高级调优 - 查找和修复慢速 Elasticsearch 查询