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

elasticsearch嵌套过滤器返回空结果

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

elasticsearch嵌套过滤器返回空结果

简短版本: 尝试此操作(更新端点和索引名称后):

curl -XPOST "http://localhost:9200/my_index/_search?search_type=dfs_query_then_fetch" -d'{   "query": {      "filtered": {         "query": { "bool": {    "must": [       {          "match": {  "title": "post"          }       }    ] }         },         "filter": { "bool": {    "must": [       {          "nested": {  "path": "seller",  "filter": {     "bool": {        "must": [{   "terms": {      "seller.firstName": [         "test",         "3"      ],      "execution": "and"   }}        ]     }  }          }       }    ] }         }      }   }}'

它对我有用,并且简化了您的设置。稍后,我将发布带有较长解释的编辑。

编辑:长版:

查询的问题是分析器与

term
查询中的过滤器结合在一起。您的分析器将
firstName
字段文本分解为标记;因此
"Test3"
成为令牌
"test"
"3"
。使用
{ "term": { "seller.firstName": "Test 3" }}
您要说的是时,找到一个文档,其中的标记之一
"seller.firstName"
"Test3"
,并且没有任何文档是正确的(实际上,无法给出分析仪的设置方式)。您可以
"index":"not_analyzed"
在该字段上使用,然后查询就可以使用,也可以使用
terms
上面显示的过滤器。这是我到达那里的方式:

我从您在注释中链接到的索引定义开始,并对其进行了一些简化以使其更具可读性,并且仍然保留以下基本问题:

curl -XDELETE "http://localhost:9200/my_index"curl -XPUT "http://localhost:9200/my_index" -d'{   "settings": {      "number_of_shards": 1,      "number_of_replicas": 0,      "analysis": {         "filter": { "snowball": { "type": "snowball", "language": "English" }, "english_stemmer": { "type": "stemmer", "language": "english" }, "english_possessive_stemmer": { "type": "stemmer", "language": "possessive_english" }, "stopwords": { "type": "stop",  "stopwords": [ "_english_" ] }, "worddelimiter": { "type": "word_delimiter" }         },         "tokenizer": { "nGram": { "type": "nGram", "min_gram": 3, "max_gram": 20 }         },         "analyzer": { "custom_analyzer": {    "type": "custom",    "tokenizer": "nGram",    "filter": [       "stopwords",       "asciifolding",       "lowercase",       "snowball",       "english_stemmer",       "english_possessive_stemmer",       "worddelimiter"    ] }, "custom_search_analyzer": {    "type": "custom",    "tokenizer": "standard",    "filter": [       "stopwords",       "asciifolding",       "lowercase",       "snowball",       "english_stemmer",       "english_possessive_stemmer",       "worddelimiter"    ] }         }      }   },   "mappings": {      "posts": {         "properties": { "title": {    "type": "string",    "analyzer": "custom_analyzer",    "boost": 5 }, "seller": {    "type": "nested",    "properties": {       "firstName": {          "type": "string",          "analyzer": "custom_analyzer",          "boost": 3       }    } }         }      }   }}'

然后我添加了一些测试文档:

curl -XPUT "http://localhost:9200/my_index/posts/1" -d'{"title": "post", "seller": {"firstName":"Test 1"}}'curl -XPUT "http://localhost:9200/my_index/posts/2" -d'{"title": "post", "seller": {"firstName":"Test 2"}}'curl -XPUT "http://localhost:9200/my_index/posts/3" -d'{"title": "post", "seller": {"firstName":"Test 3"}}'

然后运行查询的简化版本,其基本结构仍然完整,但是使用

terms
过滤器而不是
term
过滤器:

curl -XPOST "http://localhost:9200/my_index/_search?search_type=dfs_query_then_fetch" -d'{   "query": {      "filtered": {         "query": { "bool": {    "must": [       {          "match": {  "title": "post"          }       }    ] }         },         "filter": { "bool": {    "must": [       {          "nested": {  "path": "seller",  "filter": {     "bool": {        "must": [{   "terms": {      "seller.firstName": [         "test",         "3"      ],      "execution": "and"   }}        ]     }  }          }       }    ] }         }      }   }}'...{   "took": 5,   "timed_out": false,   "_shards": {      "total": 1,      "successful": 1,      "failed": 0   },   "hits": {      "total": 1,      "max_score": 6.085842,      "hits": [         { "_index": "my_index", "_type": "posts", "_id": "3", "_score": 6.085842, "_source": {    "title": "post",    "seller": {       "firstName": "Test 3"    } }         }      ]   }}

这似乎可以返回您想要的东西。

这是我使用的代码:

http://sense.qbox.io/gist/041dd929106d27ea606f48ce1f86076c52faec91



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

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

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