ElasticSearch 6.x 学习笔记:31.Java API之词项查询

1、term查询

Find documents which contain the exact term specified in the field specified.

package cn.hadron;
import cn.hadron.es.QueryUtil;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

public class TermQuery {
    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.termQuery("title","vmware");
        util.query(qb).print();
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
source:{ "title": "vmware复制虚拟机","author":"程裕强","postdate":"2016-12-29","abstract":"vmware复制虚拟机","url":"http://url.cn/53946664"}
index:website
type:blog
id:4
author=程裕强
postdate=2016-12-29
abstract=vmware复制虚拟机
title=vmware复制虚拟机
url=http://url.cn/53946664
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、terms查询

Find documents which contain any of the exact terms specified in the field specified.

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.termsQuery("title","centos","yum");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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": "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

3、range查询

Find documents where the field specified contains values (dates, numbers, or strings) in the range specified.

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.rangeQuery("postdate").from("2017-01-01").to("2017-12-31").format("yyyy-MM-dd");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
source:{ "title": "es高亮","author":"程裕强","postdate":"2017-01-03","abstract":"Elasticsearch查询关键字高亮","url":"http://url/53991802"}
index:website
type:blog
id:8
author=程裕强
postdate=2017-01-03
abstract=Elasticsearch查询关键字高亮
title=es高亮
url=http://url/53991802
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4、exists查询

Find documents where the field specified contains any non-null value.

5、prefix查询

Find documents where the field specified contains terms which being with the exact prefix specified.

package cn.hadron;
import cn.hadron.es.QueryUtil;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

public class TermQuery {
    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("my-index",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.prefixQuery("name","程");
        util.query(qb).print();
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
source:{
  "name":"程裕强",
  "age":31,
  "gender":"男",
  "salary":20000,
  "dep":"bigdata"
}

index:my-index
type:persion
id:5
gender=男
name=程裕强
salary=20000
age=31
dep=bigdata
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

6、wildcard查询

Find documents where the field specified contains terms which match the pattern specified, where the pattern supports single character wildcards (?) and multi-character wildcards (*)

 public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.wildcardQuery("title","*yum*");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

7、regexp查询

Find documents where the field specified contains terms which match the regular expression specified.

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.regexpQuery("title","gc.*");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
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

8、fuzzy查询

Find documents where the field specified contains terms which are fuzzily similar to the specified term. Fuzziness is measured as a Levenshtein edit distance of 1 or 2.

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",5);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.fuzzyQuery("title","vmwere");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
source:{ "title": "vmware复制虚拟机","author":"程裕强","postdate":"2016-12-29","abstract":"vmware复制虚拟机","url":"http://url.cn/53946664"}
index:website
type:blog
id:4
author=程裕强
postdate=2016-12-29
abstract=vmware复制虚拟机
title=vmware复制虚拟机
url=http://url.cn/53946664
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

9、type查询

Find documents of the specified type.

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",2);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.typeQuery("blog");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
source:{ "title": "libstdc++.so.6","author":"程裕强","postdate":"2016-12-30","abstract":"libstdc++.so.6问题解决","url":"http://url.cn/53946911"}
index:website
type:blog
id:5
author=程裕强
postdate=2016-12-30
abstract=libstdc++.so.6问题解决
title=libstdc++.so.6
url=http://url.cn/53946911
source:{ "title": "es高亮","author":"程裕强","postdate":"2017-01-03","abstract":"Elasticsearch查询关键字高亮","url":"http://url/53991802"}
index:website
type:blog
id:8
author=程裕强
postdate=2017-01-03
abstract=Elasticsearch查询关键字高亮
title=es高亮
url=http://url/53991802
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

10、ids查询

Find documents with the specified type and IDs.

    public static void main(String[] args) {
        QueryUtil util=new QueryUtil("website",2);
        //构造查询对象
        QueryBuilder qb=QueryBuilders.idsQuery().addIds("1","3");
        util.query(qb).print();
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

source:{ "title": "Ambari源码编译","author":"程裕强","postdate":"2016-12-21","abstract":"CentOS7.x下的Ambari2.4源码编译","url":"http://url.cn/53788351"} 

index:website 

type:blog 

id:1 

author=程裕强 

postdate=2016-12-21 

abstract=CentOS7.x下的Ambari2.4源码编译 

title=Ambari源码编译 

url=http://url.cn/53788351 

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




猜你喜欢

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