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




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.

QueryBuilder query=QueryBuilders.matchQuery(
        "name",                                              
        "kimchy elasticsearch"); 
 
 
  • 1
  • 2
  • 3

1.2 multi_match query

The multi-field version of the match query.

QueryBuilder query=QueryBuilders.multiMatchQuery(
        "kimchy elasticsearch",                              
        "user", "message");
 
 
  • 1
  • 2
  • 3

1.3 common_terms query

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

QueryBuilder query=QueryBuilders.commonTermsQuery("name", "kimchy");  
 
 
  • 1

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.

QueryBuilder query=QueryBuilders.queryStringQuery("+kimchy -elasticsearch");
 
 
  • 1

1.5 simple_query_string

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

QueryBuilder query=QueryBuilders.simpleQueryStringQuery("+kimchy -elasticsearch");

 
 
  • 1
  • 2

2、实例演示

2.1 公用查询类

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));
            }
        }
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2.2 查询实例

(1)matchQuery

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();
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

这里写图片描述

(2)Operator

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();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(3)multiMatchQuery

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        QueryBuilder qb=QueryBuilders.multiMatchQuery("centos","title","abstract");
        util.query(qb).print();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

(4)




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值