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","*公民"))

 

上一篇:COMP 409


下一篇:RecyclerView 内item点击失效