前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch 6.x 学习笔记:30.Java API之全文查询

ElasticSearch 6.x 学习笔记:30.Java API之全文查询

作者头像
程裕强
发布2022-05-06 19:22:35
3130
发布2022-05-06 19:22:35
举报
文章被收录于专栏:大数据学习笔记

1、全文查询概述

https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-full-text-queries.html

The high-level full text queries are usually used for running full text queries on full text fields like the body of an email. They understand how the field being queried is analyzed and will apply each field’s analyzer (or search_analyzer) to the query string before executing.

1.1 match query

The standard query for performing full text queries, including fuzzy matching and phrase or proximity queries.

代码语言:javascript
复制
QueryBuilder query=QueryBuilders.matchQuery(
        "name",                                              
        "kimchy elasticsearch"); 

1.2 multi_match query

The multi-field version of the match query.

代码语言:javascript
复制
QueryBuilder query=QueryBuilders.multiMatchQuery(
        "kimchy elasticsearch",                              
        "user", "message");

1.3 common_terms query

A more specialized query which gives more preference to uncommon words.

代码语言:javascript
复制
QueryBuilder query=QueryBuilders.commonTermsQuery("name", "kimchy");  

1.4 query_string query

Supports the compact Lucene query string syntax, allowing you to specify AND|OR|NOT conditions and multi-field search within a single query string. For expert users only.

代码语言:javascript
复制
QueryBuilder query=QueryBuilders.queryStringQuery("+kimchy -elasticsearch");

1.5 simple_query_string

A simpler, more robust version of the query_string syntax suitable for exposing directly to users.

代码语言:javascript
复制
QueryBuilder query=QueryBuilders.simpleQueryStringQuery("+kimchy -elasticsearch");

2、实例演示

2.1 公用查询类

代码语言:javascript
复制
package cn.hadron.es;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import java.util.Map;

public class QueryUtil {
    private String index="index";
    private int size=3;
    private SearchHits hits;
    private TransportClient client = ESUtil.getClient();

    public QueryUtil(String index,int size){
        this.index=index;
        this.size=size;
    }

    public QueryUtil query(QueryBuilder query){
        //搜索结果存入SearchResponse
        SearchResponse response=client.prepareSearch(index)
                .setQuery(query) //设置查询器
                .setSize(size)      //一次查询文档数
                .get();
        this.hits=response.getHits();
        return this;
    }

    public void print(){
        if(hits==null){
            return ;
        }
        for(SearchHit hit:hits){
            System.out.println("source:"+hit.getSourceAsString());
            System.out.println("index:"+hit.getIndex());
            System.out.println("type:"+hit.getType());
            System.out.println("id:"+hit.getId());
            //遍历文档的每个字段
            Map<String,Object> map=hit.getSourceAsMap();
            for(String key:map.keySet()){
                System.out.println(key+"="+map.get(key));
            }
        }
    }
}

2.2 查询实例

(1)matchQuery

代码语言:javascript
复制
package cn.hadron.es;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class FullTextQuery {
    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.matchQuery(
                "title",
                "centos");
        util.query(qb).print();
    }
}
这里写图片描述
这里写图片描述

(2)Operator

代码语言:javascript
复制
public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        //QueryBuilder qb=QueryBuilders.matchQuery("title", "centos");
        QueryBuilder qb=QueryBuilders
                .matchQuery("title", "centos升级")
                .operator(Operator.AND);
        util.query(qb).print();
    }
代码语言:javascript
复制
source:{ "title": "CentOS升级gcc","author":"程裕强","postdate":"2016-12-25","abstract":"CentOS升级gcc","url":"http://url.cn/53868915"}
index:website
type:blog
id:3
author=程裕强
postdate=2016-12-25
abstract=CentOS升级gcc
title=CentOS升级gcc
url=http://url.cn/53868915

(3)multiMatchQuery

代码语言:javascript
复制
    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        QueryBuilder qb=QueryBuilders.multiMatchQuery("centos","title","abstract");
        util.query(qb).print();
    }
代码语言:javascript
复制
source:{ "title": "CentOS更换国内yum源","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS更换国内yum源","url":"http://url.cn/53946911"}
index:website
type:blog
id:6
author=程裕强
postdate=2016-12-30
abstract=CentOS更换国内yum源
title=CentOS更换国内yum源
url=http://url.cn/53946911
source:{ "title": "搭建Ember开发环境","author":"程裕强","postdate":"2016-12-30","abstract":"CentOS下搭建Ember开发环境","url":"http://url.cn/53947507"}
index:website
type:blog
id:7
author=程裕强
postdate=2016-12-30
abstract=CentOS下搭建Ember开发环境
title=搭建Ember开发环境
url=http://url.cn/53947507
source:{ "title": "CentOS升级gcc","author":"程裕强","postdate":"2016-12-25","abstract":"CentOS升级gcc","url":"http://url.cn/53868915"}
index:website
type:blog
id:3
author=程裕强
postdate=2016-12-25
abstract=CentOS升级gcc
title=CentOS升级gcc
url=http://url.cn/53868915
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、全文查询概述
    • 1.1 match query
      • 1.2 multi_match query
        • 1.3 common_terms query
          • 1.4 query_string query
            • 1.5 simple_query_string
            • 2、实例演示
              • 2.1 公用查询类
                • 2.2 查询实例
                相关产品与服务
                Elasticsearch Service
                腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档