ES 多条件搜索问题

if(StringUtils.isNotEmpty(condition.getRequestUri())){

//之前的代码是进行匹配搜索符合的request URl 目前新区求是可以满足多个url的查询 ,比如 /index,/get

//要分别他们的两者的结果展示出来

boolQueryBuilder.must(matchPhraseQuery("requestUri", condition.getRequestUri()));

}

还是分别匹配。然后把结果根据需求做 交集或者并集

加q:1571488201

@浴火_凤凰 那样效率很慢呀

效率慢 但是好扩展啊。。

自由组合 想怎么组合就怎么组合。

可能我回答的不想洗, 我想要的就类似sql 的这个语句 where url = like post or like  get 组装成类似这个样子

具体代码如下

public AggregatedPage<ProcessLog> query(ProcessLog condition, Pageable pageable) {

        BoolQueryBuilder boolQueryBuilder = boolQuery();

        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder().withPageable(pageable)
                .withQuery(boolQueryBuilder);

        if(StringUtils.isNotEmpty(condition.getUser())){
            boolQueryBuilder.must(matchPhraseQuery("user", condition.getUser()));
        }

        if(StringUtils.isNotEmpty(condition.getRequestUri())){
            boolQueryBuilder.must(matchPhraseQuery("requestUri", condition.getRequestUri()));
        }

        if(StringUtils.isNotEmpty(condition.getParams())){
            boolQueryBuilder.must(matchPhraseQuery("params", condition.getParams()));
        }

        if(StringUtils.isNotEmpty(condition.getEmail())){
            boolQueryBuilder.must(matchPhraseQuery("email", condition.getEmail()));
        }

        if(StringUtils.isNotEmpty(condition.getPeerId())){
            boolQueryBuilder.must(matchPhraseQuery("peerId", condition.getPeerId()));
        }

        if(StringUtils.isNotEmpty(condition.getServerName())){
            boolQueryBuilder.must(termQuery("serverName.keyword", condition.getServerName()));
        }

        if(condition.getStatus() != null){
            boolQueryBuilder.must(termQuery("status", condition.getStatus()));
        }

        if(StringUtils.isNotEmpty(condition.getMethod())){
            boolQueryBuilder.must(matchPhraseQuery("method", condition.getMethod()));
        }

        if(condition.getStartTime() != null && condition.getEndTime() != null ){
            boolQueryBuilder.must(
                    rangeQuery("requestTime")
                    .gte(condition.getStartTime())
                    .lt(condition.getEndTime())
            );
        }

        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();

        return elasticsearchRestTemplate.queryForPage(searchQuery, ProcessLog.class);
    }

楼主要的是下面这样的???


/**
 * 多条件查询
 * @throws Exception
 */
@Test
public void searchMutil()throws Exception{
    SearchRequestBuilder srb=client.prepareSearch("film").setTypes("dongzuo");
    QueryBuilder queryBuilder=QueryBuilders.matchPhraseQuery("title", "战");
    QueryBuilder queryBuilder2=QueryBuilders.matchPhraseQuery("content", "星球");
    SearchResponse sr=srb.setQuery(QueryBuilders.boolQuery()
            .must(queryBuilder)
            .must(queryBuilder2))
        .execute()
        .actionGet(); 
    SearchHits hits=sr.getHits();
    for(SearchHit hit:hits){
        System.out.println(hit.getSourceAsString());
    }
}

我想要的时是一个字段 2个搜索关键字 , 类似数据库sql where url = "a" or "b"


不是你说的这种 where title= 站 and content = "星球"


title和content你可以改成一样的。。

这样会类似hashmap的key 会被重置的

上面使用了链式写法

.must(queryBuilder)

.must(queryBuilder2))

.execute()

.actionGet();

代码看起来短一点,应该还是两个查询吧

我试过2个must 但是我觉的他像 where url =a and url =b 不存在的情况呀

那我没办法了。。又转回到我第一次回答你的地方了。哈哈。。

真不行 我就用哪个办法, 但哪个是我的下下策

嗯 作为备用方法吧。看看其他人还有没有办法

{

  "query": {

    "bool": {

      "must": [{

        "match_phrase": {

          "name": "a"

        }

      }],

      "should": [{

        "match_phrase": {

          "city": "b"

        }

      },

      {

        "match_phrase": {

          "city": "c"

        }

      }],

      "minimum_should_match": 1

    }

  },

  "size": 5

}

或者你能帮我写出来这个java代码?

你可以试试这种方式,将must 改为 should 就行

思路大概就是这样


不行呀.我实际搜索/mma/mdmind_getAllStoryTask.action 出现的却是这些


代码对应的语句为:

你试试


为什么我这么写,单个字段查询的时候 会出出现其他属性 ,,,,


我传入的参数是 /mma/mdmind_getAllStoryTask.action


我debug 看的信息是走完for循环, 输出信息是 
{
  "bool" : {
    "should" : [
      {
        "term" : {
          "requestUri" : {
            "value" : "/mma/mdmind_getAllStoryTask.action",
            "boost" : 1.0
          }
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}

这样也不行

知道es 语句啦, 谁能帮我转换成 java代码{
    "query":{
        "bool":{
            "filter":[
                {
                    "bool":{
                        "must":[
                            {
                                "bool":{
                                    "should":[
                                        {
                                            "match_phrase":{
                                                "url":{
                                                    "query":"a"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool":{
                        "must":[
                            {
                                "bool":{
                                    "should":[
                                        {
                                            "match_phrase":{
                                                "url":{
                                                    "query":"b"
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    },
    "_source":{
        "include":[
            "*"
        ]
    }
}





这样单个查询也不对


兄弟,你写错了


{

 "size": 0,

 "from": 0,

 "query": {

  "bool": {

   "must": [

    {

     "term": {

      "user": {

       "value": "张三",

       "boost": 1

      }

     }

    },

    {

     "bool": {

      "should": [

       {

        "term": {

         "url": {

          "value": "/get/aa",

          "boost": 1

         }

        }

       },

       {

        "term": {

         "url": {

          "value": "/get/bb",

          "boost": 1

         }

        }

       }

      ],

      "adjust_pure_negative": true,

      "boost": 1

     }

    }

   ],

   "adjust_pure_negative": true,

   "boost": 1

  }

 },

 "aggs": {

  "url": {

   "terms": {

    "field": "url",

    "size": 10

   }

  }

 }

}

你这样其实也不行, 我试过

QueryBuilders.termsQuery("url.keyword","aaa","bbbb")

ES 版本7.X 有个多查询,java官网地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-multi-search.html?%