简短版本: 尝试此操作(更新端点和索引名称后):
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



