栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

包含异或的嵌套过滤器查询

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

包含异或的嵌套过滤器查询

您的描述含糊不清,但我尝试制作一个可运行的示例,该示例应在此处运行:https
:
//www.found.no/play/gist/8940202(也嵌入在下面)

这是我做的几件事:

  • 将过滤器放入

    filtered
    -query中。仅当您要过滤匹配时才使用顶层
    filter
    post_filter
    在Elasticsearch 1.0中重命名),而不要过滤方面。

  • 使用

    bool
    代替
    and
    or
    ,因为过滤器是可缓存的。此处更多信息:http : //www.elasticsearch.org/blog/all-about-elasticsearch-filter-bitsets/

  • 而最重要的,把

    nested
    里面
    bool
    ,所以逻辑得到正确的WRT。嵌套文档与父文档应该匹配什么。

  • must_not
    为您的最后一点添加了一个帐户。不知道您是否可以拥有两个名为name的子文档
    "Connectivity"
    ,但是如果可以的话,应该可以解决这个问题。如果您只有一个,可以删除
    must_not

您没有提供任何示例文档,所以我提出了一些我认为适合您的描述的文档。我认为您不需要两个级别的嵌套。

#!/bin/bashexport ELASTICSEARCH_ENDPOINT="http://localhost:9200"# Create indexescurl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{    "mappings": {        "type": { "properties": {     "kol_tags": {         "properties": {  "scored": {      "type": "nested",      "properties": {          "name": {   "type": "string",   "index": "not_analyzed"          }      }  }         }     } }        }    }}'# Index documentscurl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Core Grower","score":36},{"name":"Connectivity","score":42}]}}{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Connectivity","score":34},{"name":"Connectivity","score":42}]}}{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Core Grower","score":36}]}}{"index":{"_index":"play","_type":"type"}}{"kol_tags":{"scored":[{"name":"Connectivity","score":36}]}}'# Do searchescurl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '{    "query": {        "filtered": { "filter": {     "bool": {         "should": [  {      "nested": {          "path": "kol_tags.scored",          "filter": {   "bool": {       "must": [{    "term": {        "name": "Core Grower"    }},{    "range": {        "score": { "gte": 1, "lte": 100        }    }}       ]   }          }      }  },  {      "nested": {          "path": "kol_tags.scored",          "filter": {   "bool": {       "must": [{    "term": {        "name": "Connectivity"    }},{    "range": {        "score": { "gte": 35, "lte": 65        }    }}       ]   }          }      }  }         ],         "must_not": [  {      "nested": {          "path": "kol_tags.scored",          "filter": {   "bool": {       "must": [{    "term": {        "name": "Connectivity"    }},{    "not": {        "range": { "score": {     "gte": 35,     "lte": 65 }        }    }}       ]   }          }      }  }         ]     } }        }    }}'curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '{    "filter": {        "nested": { "path": "kol_tags.scored", "filter": {     "or": [         {  "and": [      {          "terms": {   "kol_tags.scored.name": [       "Core Grower"   ]          }      },      {          "range": {   "kol_tags.scored.score": {       "gte": 1,       "lte": 100   }          }      }  ]         },         {  "and": [      {          "terms": {   "kol_tags.scored.name": [       "Connectivity"   ]          }      },      {          "range": {   "kol_tags.scored.score": {       "gte": 35,       "lte": 65   }          }      }  ]         }     ] }        }    }}'


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号