【问题标题】:elasticsearch nested filter return empty resultelasticsearch嵌套过滤器返回空结果
【发布时间】:2015-03-03 11:13:59
【问题描述】:

我有这个映射:

  "post": {
    "model": "Post",
    "properties": {
      "id": {
        "type": "integer"
      },
      "title": {
        "type": "string",
        "analyzer": "custom_analyzer",
        "boost": 5
      },
      "description": {
        "type": "string",
        "analyzer": "custom_analyzer",
        "boost": 4
      },
      "condition": {
        "type": "integer",
        "index": "not_analyzed"
      },
      "categories": {
        "type": "string",
        "index": "not_analyzed"
      },
      "seller": {
        "type": "nested",
        "properties": {
          "id": {
            "type": "integer",
            "index": "not_analyzed"
          },
          "username": {
            "type": "string",
            "analyzer": "custom_analyzer",
            "boost": 1
          },
          "firstName": {
            "type": "string",
            "analyzer": "custom_analyzer",
            "boost": 3
          },
          "lastName": {
            "type": "string",
            "analyzer": "custom_analyzer",
            "boost": 2
          }
        }
      },
      "marketPrice": {
        "type": "float",
        "index": "not_analyzed"
      },
      "currentPrice": {
        "type": "float",
        "index": "not_analyzed"
      },
      "discount": {
        "type": "float",
        "index": "not_analyzed"
      },
      "commentsCount": {
        "type": "integer",
        "index": "not_analyzed"
      },
      "likesCount": {
        "type": "integer",
        "index": "not_analyzed"
      },
      "featured": {
        "type": "boolean",
        "index": "not_analyzed"
      },
      "bumped": {
        "type": "boolean",
        "index": "not_analyzed"
      },
      "created": {
        "type": "date",
        "index": "not_analyzed"
      },
      "modified": {
        "type": "date",
        "index": "not_analyzed"
      }
    }
  }

这个查询:

GET /develop/_search?search_type=dfs_query_then_fetch
{
  "query": {
    "filtered" : {
        "query": {
          "bool": {
            "must": [
              { "match": { "title": "post" }}
            ]
          }
        },
        "filter": {
          "bool": { 
            "must": [
              {"term": {
                "featured": 0
              }},
              { 
              "nested": {
                "path": "seller",
                "filter": {
                  "bool": {
                    "must": [
                      { "term": { "seller.firstName": "Test 3" } }
                    ]
                  }
                },
                "_cache" : true
              }}
            ]
          } 
        }
    }
  },
  "sort": [
    {
      "_score":{
        "order": "desc"
      }
    },{
      "created": {
        "order": "desc"
      }
    }
  ],
  "track_scores": true
}

我等待 25 个结果,因为我有 25 个帖子被索引。但我得到一个空集。如果我删除嵌套过滤器一切正常。我希望能够过滤嵌套对象

编辑:

在我的设置中:

    "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"
        ]
      }
    }

我在这里缺少什么。

谢谢

【问题讨论】:

  • 您能发布您对custom_analyzer 的定义吗?它的设置方式可能与您的问题有关。
  • 嘿@SloanAhrens 我用我的分析器更新问题
  • 我仍然无法建立一个像你这样的索引来测试它。看起来您也定义了一些自定义过滤器?你也可以发这些吗?一些示例文档也可能会有所帮助。如果无法模拟您的系统,很难告诉您问题出在哪里。
  • 我的怀疑是,在您当前的设置下,{ "term": { "seller.firstName": "Test 3" } } 必须是 { "term": { "seller.firstName": "test" } },或类似的东西。或者您将需要调整您的分析器/映射。但是如果不直接测试就很难确定。
  • @SloanAhrens 关于这个问题stackoverflow.com/questions/27493452/… 我发布了相同的数据和映射,但出于不同的原因,它是相同的

标签: search filter elasticsearch nested


【解决方案1】:

短版:试试这个(更新端点和索引名称后):

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 字段的文本分解为标记;所以"Test 3" 成为标记"test""3"。当您使用{ "term": { "seller.firstName": "Test 3" } } 时,您所说的是,找到一个文档,其中"seller.firstName" 的标记之一是"Test 3",并且没有任何文档是正确的(事实上,不能给出分析仪的设置方式)。您可以在该字段上使用"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

【讨论】:

  • 很好用,但 ES 不应该为我这样做吗?如何指定要在过滤器中使用的分析器?因为如果我必须对搜索文本进行分析,我自己可能会非常困难
  • 阅读我刚刚所做的编辑,看看它是否回答了您的问题。如果没有我会看看我是否可以更新它。我希望您会想要修改您的分析器,和/或使用 multi_field。
  • 感谢 Sloan,在您发布答案后,我认为 custom_analyzer 会拆分字段,但我希望它在查询中使用 custom_search_analyzer,因此在使用过滤器 ES 拆分时它自己的文本,所以我不需要自己拆分它并以这种方式构建查询。我还有其他带有“multi_match”的查询:{“query”:“post”,“fields”:[“title”,“description”],“analyzer”:“custom_search_analyzer”}我想要做的事情类似
  • 抱歉,我不确定我是否理解您的问题。你试过你的建议吗?也许值得提出另一个关于它的 StackOverflow 问题?
  • 是的,我尝试了,但我得到一个错误,因为我没有找到让过滤器使用自定义分析器的方法。我会尝试另一个问题。谢谢
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 2021-10-18
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多