es多条件查询之must用法(Java实现)

24 篇文章 3 订阅
20 篇文章 0 订阅

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

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

package com.zx.znyd.es;

 

import java.io.IOException;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;import org.apache.commons.lang.StringUtils;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;

import org.elasticsearch.action.get.GetResponse;

import org.elasticsearch.action.index.IndexRequest;

import org.elasticsearch.action.search.SearchResponse;

import org.elasticsearch.action.search.SearchType;

import org.elasticsearch.client.Client;

import org.elasticsearch.client.Requests;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.mustache.Mustache;

import org.elasticsearch.common.settings.ImmutableSettings;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.common.transport.TransportAddress;

import org.elasticsearch.common.xcontent.XContentBuilder;

import org.elasticsearch.common.xcontent.XContentFactory;

import org.elasticsearch.index.query.MatchQueryBuilder.Operator;

import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.search.SearchHit;

import org.elasticsearch.search.SearchHits;

import org.elasticsearch.search.sort.SortOrder;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

import com.alibaba.fastjson.JSONObject;

import com.zx.znyd.common.MD5;

import com.zx.znyd.common.SpringContextUtil;

import com.zx.znyd.dao.AutoReplyDao;

import com.zx.znyd.data.model.AutoReply;

import com.zx.znyd.data.model.LogIndex;

import com.zx.znyd.service.AutoReplyDeployService;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:applicationContext.xml")

public class TestES {

 

    @Autowired

    @Qualifier("esClient")

    private Client client;

     

    @Test 

    public void testSearch() 

    

        String index="index_log_daily";

        String type="uq_log";

        String uquestion = "139";

        String province = "江苏";

        SearchResponse searchResponse = client.prepareSearch(index) 

                .setTypes(type) 

                .setQuery(QueryBuilders.matchAllQuery()) //查询所有

                .setQuery(QueryBuilders.boolQuery()

                .must(QueryBuilders.matchQuery("uquestion", uquestion))//查询uquestion为139的

                .must(QueryBuilders.matchQuery("province", province)))//查询省份为江苏的

                //.setQuery(QueryBuilders.matchQuery("uquestion", "12599").operator(Operator.AND)) //根据tom分词查询name,默认or

                //.setQuery(QueryBuilders.matchQuery("province", "江苏").operator(Operator.AND)) //根据tom分词查询name,默认or

                //.setQuery(QueryBuilders.multiMatchQuery("tom", "name", "age")) //指定查询的字段 

                //.setQuery(QueryBuilders.queryString("name:to* AND age:[0 TO 19]")) //根据条件查询,支持通配符大于等于0小于等于19 

                //.setQuery(QueryBuilders.termQuery("name", "tom"))//查询时不分词 

                .setSearchType(SearchType.QUERY_THEN_FETCH) 

                .setFrom(0).setSize(10)//分页 

                //.addSort("age", SortOrder.DESC)//排序 

                .get(); 

           

        SearchHits hits = searchResponse.getHits(); 

        long total = hits.getTotalHits(); 

        System.out.println(total); 

        SearchHit[] searchHits = hits.hits(); 

        for(SearchHit s : searchHits) 

        

            System.out.println(s.getSourceAsString());

            String []logindex=s.getSourceAsString().split(",");

             

        

    

}

  

你想拥有什么,你就去追求什么!

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值