Elasticsearch 6.x Learning notes: 30. Full-text query __java Java API

Source: Internet
Author: User
1, full-text query overview

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 to running full text queries on full text fields like the ' body of ' an EM Ail. They understand how the field being queried are analyzed and would apply each field ' analyzer (or Search_analyzer) to the Q Uery 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"),                                              
        
1.2 multi_match Query

The Multi-field version of the match query.

QueryBuilder query=querybuilders.multimatchquery (
        "Kimchy elasticsearch",                              
        "user", "message");
1.3 common_terms Query

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

QueryBuilder query=querybuilders.commontermsquery ("name", "Kimchy");  
1.4 query_string Query

Supports the Compact Lucene query string syntax, allowing to specify and|or| Not conditions and Multi-field search within a single query string. For expert users.

QueryBuilder query=querybuilders.querystringquery ("+kimchy-elasticsearch");
1.5 simple_query_string

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

QueryBuilder query=querybuilders.simplequerystringquery ("+kimchy-elasticsearch");
2. Example Demo 2.1 Public Query Classes
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) {//Search results are deposited in SearchResponse searchresponse RESPONSE=CLIENT.P
                Reparesearch (Index). Setquery (query)//set up the Finder. SetSize (size)//number of documents queried at a time
        . 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 ());
            Traverse each field of the document Map<string,object> Map=hit.getsourceasmap ();
            For (String Key:map.keySet ()) {System.out.println (key+ "=" +map.get (key));
 }
        }
    }
}
2.2 Query Instances

(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);
        //Construct query objects
        QueryBuilder qb=querybuilders.matchquery (
                "title",
                "CentOS");
        Util.query (QB). print ();
    }

(2) Operator

public static void Main (string[] args) {
        queryutil util=new queryutil ("website", 5);
        Constructs the query object
        //querybuilder qb=querybuilders.matchquery ("title", "CentOS");
        QueryBuilder qb=querybuilders
                . Matchquery ("title", "CentOS Upgrade")
                . Operator (Operator.and);
        Util.query (QB). print ();
    
source:{"title": "CentOS upgrade gcc", "Author": "Cheng", "postdate": "2016-12-25", "abstract": "CentOS upgrade gcc", "url": "http:// url.cn/53868915 "}
index:website
type:blog
id:3
author= Cheng
Abstract =centos upgrade gcc
title=centos upgrade gcc
url=http://url.cn/53868915

(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 ();
    
source:{"title": "CentOS replacement of domestic yum source", "Author": "Cheng", "postdate": "2016-12-30", "abstract": "CentOS replacement of domestic yum source", "url": " http://url.cn/53946911 "}
index:website
type:blog
id:6
author=
Cheng postdate=2016-12-30 Abstract=centos replacement of domestic yum source
Title=centos replacement of domestic yum source
url=http://url.cn/53946911
source:{"title": " Build ember development environment "," Author ":" Cheng "," postdate ":" 2016-12-30 "," abstract ":" CentOS under the Ember development environment "," url ":" http://url.cn/ 53947507 "}
index:website
type:blog
id:7
author=
Cheng postdate=2016-12-30 abstract= CentOS Build Ember Development environment
title= Build Ember development environment
url=http://url.cn/53947507
source:{"title": "CentOS upgrade gcc", " Author ":" Cheng "," postdate ":" 2016-12-25 "," abstract ":" CentOS upgrade gcc "," url ":" http://url.cn/53868915 "}
Index: Website
type:blog
id:3
author= Cheng postdate=2016-12-25 abstract=centos
upgrade gcc
title =centos Upgrade GCC
url=http://url.cn/53868915

(4)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.