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)






1、全文查询概述

猜你喜欢

转载自blog.csdn.net/u011428598/article/details/81082087
今日推荐