Elasticsearch 类比 mysql 实现 in and like or

Posted Bonnie_ξ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch 类比 mysql 实现 in and like or相关的知识,希望对你有一定的参考价值。

 

常用的查询方式类比mysql

       <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>5.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.6.1</version>
        </dependency>    

 

  • and
select * from user where about=‘中华人民共和国‘ and firstName=‘张’

QueryBuilders.boolQuery().must(QueryBuilders.termQuery("about","中华人民共和国")).must(QueryBuilders.termQuery("firstName","张"))
  • or
select * from user where about=‘中华人民共和国‘ and firstName=‘王’
QueryBuilders.boolQuery().should(QueryBuilders.termQuery("about","中华人民共和国")).should(QueryBuilders.termQuery("firstName","王"))
  • and or 连用
select * from user where  firstName=‘张‘ and( firstName=‘王’ or about=‘中华人民共和国公民‘)

QueryBuilders.boolQuery()
                        .must(QueryBuilders.termQuery("firstName","张"))
                        .must(QueryBuilders.boolQuery()
                                        .should(QueryBuilders.termQuery("firstName","王"))
                                        .should(QueryBuilders.termQuery("about","中华人民共和国公民"))
  • like
select * from user where about like ‘%中华%‘

QueryBuilders.wildcardQuery("about","*中华*")
  • in
select * from user where about in (‘中华人民共和国‘,‘中华人民共和国公民‘)

QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("about","中华人民共和国"))
                        .should(QueryBuilders.termQuery("about","中华人民共和国公民"))

 



以上是关于Elasticsearch 类比 mysql 实现 in and like or的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch 7 : 关于 IndexTypeDocument

Elasticsearch 7 : 关于 IndexTypeDocument

Elasticsearch 7 : 关于 IndexTypeDocument

hadoop生态--ElasticSearch--ES操作

关于 Elasticsearch 移除 type 的简要说明

Elasticsearch系列:安装与介绍