elasticsearch中NestedQueryBuilder的使用

 

{
    "blackList":[
        {
            "inTime":1568719526137,
            "relationKey":2,
            "relationValue":"957588FF-28B6-4388-B7FB",
            "outTime":1571311526137,
            "status":1
        },
        {
            "inTime":1568773917483,
            "relationKey":3,
            "relationValue":"b60407c80d7805db",
            "outTime":4102329600000,
            "status":1
        }
    ],
    "status":1,
    "source":1,
    "createTime":1568719526137,
    "updateTime":1568719526137,
    "timestamp":1568719526693
}

上面一条数据,现在要实现一个子查询,要同时满足"relationKey":3,"relationValue":"b60407c80d7805db"才能被查询到

如果是用ES命令行查询的话需要这样写:

GET tmp_list/_search
{
  "query": {
    "nested": {
      "path": "blackList",
      "query": {
        "bool": {
          "must": [
            {
              "term":{
                "blackList.relationKey": {
                  "value": "3"
                }
              }
            },
            {
              "term": {
                "blackList.relationValue": {
                  "value": "b60407c80d7805db"
                }
              }
            }
          ]
        }
      }
    }
  }
}

如果在代码中该怎样实现这个功能了?下面是我的踩坑记录。。。。。。

if (null != query.getRelationValue()) {
     NestedQueryBuilder nestedQuery = new NestedQueryBuilder("blackList", new TermQueryBuilder("black.relationValue",query.getRelationValue()), ScoreMode.None);
     boolQueryBuilder.must(nestedQuery);
 }
 if (null != query.getRelationKey()) {
     NestedQueryBuilder nestedQuery = new NestedQueryBuilder("blackList", new TermQueryBuilder("blackList.relationKey",query.getRelationKey()), ScoreMode.None);
     boolQueryBuilder.must(nestedQuery);
 }

发现结果虽然是可以查询出来,但是有一个问题就是,查询条件不唯一,假设我把relationKey换成2也可以查询出来同样的数据,后来发现原来是这样写系统会把该代码解析成了或的关系,因为他们不在同一个NestedQueryBuilder里面,只要该节点(List)下面同时含有"relationKey":3和"relationValue":"b60407c80d7805db"(不一定在同一个对象里面),就可以被查询出来。

修改后的代码如下:

 BoolQueryBuilder nestedBoolQueryBuilder = QueryBuilders.boolQuery();
 if (null != query.getRelationValue()) {
     nestedBoolQueryBuilder.must(QueryBuilders.matchQuery("blackList.relationValue", query.getRelationValue()));
   }
if (null != queryBlackListPageBo.getRelationKey()) {                       
     nestedBoolQueryBuilder.must(QueryBuilders.matchQuery("blackList.relationKey", query.getRelationKey()));
   }
 NestedQueryBuilder nestedQueryBuilder = QueryBuilders.nestedQuery("blackList", nestedBoolQueryBuilder, ScoreMode.None);
 boolQueryBuilder.must(nestedQueryBuilder);

这样把两个查询条件放到一个NestedQueryBuilder里面,系统就会解析成并且的关系,这样查询出来的结果就是自己想要的了。

  • 9
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: Python可以与Elasticsearch进行交互,通过Elasticsearch的Python客户端API,您可以轻松地使用Python从Elasticsearch检索和索引数据。 以下是使用Python Elasticsearch客户端API的基本步骤: 1.安装Python Elasticsearch客户端API 在终端或命令提示符下,运行以下命令安装Python Elasticsearch客户端API: ``` pip install elasticsearch ``` 2.建立与Elasticsearch的连接 在Python脚本,您可以使用以下代码创建到Elasticsearch的连接: ``` from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) ``` 其,`host`和`port`参数指定Elasticsearch集群的主机和端口号。如果您的Elasticsearch集群有多个节点,可以指定多个主机和端口号,以逗号分隔。 3.创建索引 在Elasticsearch,索引是一个包含一组相关文档的逻辑命名空间。您可以使用以下代码在Elasticsearch创建索引: ``` es.indices.create(index='my_index') ``` 4.添加文档 要将文档添加到Elasticsearch,请使用以下代码: ``` doc = {'title': 'My document', 'content': 'This is my first document.'} es.index(index='my_index', doc_type='my_type', body=doc) ``` 其,`index`参数指定要将文档添加到的索引,`doc_type`参数指定文档类型,`body`参数指定文档内容。 5.搜索文档 要从Elasticsearch搜索文档,请使用以下代码: ``` query = {'query': {'match': {'title': 'document'}}} res = es.search(index='my_index', body=query) ``` 其,`query`参数指定要执行的查询,`res`变量包含搜索结果。 这是使用Python Elasticsearch客户端API的基本步骤。您可以使用其他API方法进行更高级的操作,例如更新文档、删除文档和聚合查询。请参阅Elasticsearch Python客户端API文档以获取更多信息。 ### 回答2: Elasticsearch是一个开源的分布式搜索和分析引擎,建立在Apache Lucene之上。它使用JSON格式进行数据的存储和索引,通过RESTful API进行数据的检索和查询。Elasticsearch的核心概念包括索引、类型、文档和字段。 在Python使用Elasticsearch可以通过安装`elasticsearch`库来实现。首先,我们需要连接到Elasticsearch集群,可以使用`Elasticsearch`类进行连接。例如: ```python from elasticsearch import Elasticsearch es = Elasticsearch([{"host": "localhost", "port": 9200}]) ``` 连接成功后,我们可以创建索引并添加文档。索引类似于数据库的表,用于存储和管理文档。例如,我们创建一个名为`my-index`的索引,并在其添加一个文档: ```python es.indices.create(index="my-index") document = { "title": "Python Elasticsearch", "content": "Elasticsearch is a powerful search engine." } es.index(index="my-index", doc_type="_doc", body=document) ``` 接下来,我们可以执行各种查询操作。例如,通过`search`方法可以执行全文搜索: ```python query = { "query": { "match": { "content": "search engine" } } } results = es.search(index="my-index", doc_type="_doc", body=query) for hit in results["hits"]["hits"]: print(hit["_source"]) ``` 此外,还可以使用`get`方法根据ID获取单个文档,使用`delete`方法删除文档,使用`update`方法更新文档等等。 总之,PythonElasticsearch库提供了简单易用的API来与Elasticsearch进行交互。通过它,我们能够方便地创建索引、添加、查询和更新文档,实现全文搜索和数据分析等功能。 ### 回答3: Elasticsearch是一个开源的分布式搜索和分析引擎,它基于Lucene库开发而成。Python提供了Elasticsearch的官方客户端库,使得在Python使用Elasticsearch变得非常方便。 使用Pythonelasticsearch库,我们可以创建一个Elasticsearch连接实例,并指定要连接的集群。我们可以使用连接实例来执行各种操作,如索引数据、搜索数据、删除数据等。 在使用Elasticsearch之前,首先要安装Elasticsearch并启动它的服务。然后,我们需要在Python安装elasticsearch库,我们可以使用pip这个包管理器来安装它。安装完成后,在我们的Python脚本引入elasticsearch库。 首先,我们需要创建一个连接实例。通过指定主机和端口,我们可以连接到Elasticsearch集群。我们还可以设置其他的参数,如认证信息、连接超时等。 然后,我们可以使用连接实例来进行各种操作。例如,我们可以使用索引方法来创建一个新的索引。我们可以指定索引的名称、类型和文档的数据。要更新或删除文档,我们可以使用相关的方法。 与索引相关的操作还包括搜索和聚合。我们可以使用搜索方法对索引的数据进行全文搜索,还可以使用聚合方法对数据进行分析和统计。 另外,我们还可以使用Elasticsearch提供的一些高级功能,如建立索引别名、设置分片和副本、执行批量操作等。 总之,Pythonelasticsearch库提供了一个便捷的方式来与Elasticsearch进行交互,我们可以通过这个库来索引、搜索和分析我们的数据。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值