需要同时使用$ select和$ filter实现一个弹性搜索查询 [英] Need to implement a Elastic search query with both $select and $filter

查看:149
本文介绍了需要同时使用$ select和$ filter实现一个弹性搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Odata查询: /ecommerceadmin/ODataService.svc/search?$select=status&$filter=id eq'test.com'和名称eq'abc'

I have a Odata Query : /ecommerceadmin/ODataService.svc/search?$select=status&$filter=id eq 'test.com'and name eq 'abc'

如何在弹性搜索中实现代码以相应地获取数据.应使用 SearchSourceBuilder 中的哪种方法?

How can I implement the code in elastic search to get the data accordingly.What method in SearchSourceBuilder should be used?

推荐答案

您可能可以轻松地通过一个match和一个term查询,像这样:

You can probably easily get away with one match and one term query like this:

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.QueryBuilders.*;

BoolQueryBuilder query = QueryBuilders.boolQuery()
    .must(QueryBuilders.matchQuery("name", "abc"))       // 1.
    .filter(QueryBuilders.termQuery("id", "test.com"));  // 2.

SearchResponse response = client.prepareSearch("ecommerce_index")
        .setTypes("ecommerce_type")
        .setQuery(query)
        .setFetchSource(new String[]{"status"}, null)    // 3.
        .setFrom(0)
        .setSize(10)
        .execute()
        .actionGet();

在上面的代码中:

  1. 负责name eq 'abc' $ filter
  2. 负责id eq 'test.com' $ filter
  3. 照顾$select=status部分
  1. takes care of the name eq 'abc' $filter
  2. takes care of the id eq 'test.com' $filter
  3. takes care of the $select=status part

这篇关于需要同时使用$ select和$ filter实现一个弹性搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆