0

我以为我已经找到了 Elasticsearch,但我怀疑我没能理解某些东西,因此出现了这个问题:

我正在索引具有大量字段的产品,但有问题的是:

{
  "show_in_catalogue": {
    "type": "boolean",
    "index": "no"
  },
  "prices": {
    "type": "object",
    "dynamic": false,
    "properties": {
      "site_id": {
        "type": "integer",
        "index": "no"
      },
      "currency": {
        "type": "string",
        "index": "not_analyzed"
      },
      "value": {
        "type": "float"
      },
      "gross_tax": {
        "type": "integer",
        "index": "no"
      }
    }
  }
}

我正在尝试返回“show_in_catalogue”为真的所有文档,并且 site_id 为 1 的价格:

{
  "filter": {
    "term": {
      "prices.site_id": "1",
      "show_in_catalogue": true
    }
  },
  "query": {
    "match_all": {}
  }
}

这将返回零结果。我还尝试了一个带有两个单独术语的“和”过滤器 - 不走运。

如果我没有过滤器,则返回的其中一个文档的子集如下所示:

{
  "prices": [
    {
      "site_id": 1,
      "currency": "GBP",
      "value": 595,
      "gross_tax": 1
    },
    {
      "site_id": 2,
      "currency": "USD",
      "value": 745,
      "gross_tax": 0
    }
  ]
}

我希望我可以在这里省略这么多文档;我不相信这是偶然的,但我当然不能确定。

我是否错过了重要的知识,或者我做了一些非常厚实的事情?无论哪种方式,我都会感谢专家在这一点上的知识。谢谢!

编辑

在 JT 的建议下,我还尝试重新索引文档以便prices.site_id被索引 - 没有变化。还尝试了下面的 bool/must 过滤器无济于事。

澄清一下,我使用空查询的原因是 Web 界面可能会提供查询字符串,但相同的代码用于简单地过滤所有产品。因此我留在了查询中,但是是空的,因为那是 Elastica 似乎在没有查询字符串的情况下产生的。

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "show_in_catalogue": true
              }
            },
            {
              "term": {
                "prices.site_id": 1
              }
            }
          ]
        }
      }
    }
  }
}
4

1 回答 1

2

您将 site_id 设置为 {"index": "no"}。这告诉 ElasticSearch 从索引中排除该字段,从而无法查询或过滤该字段。数据仍将被存储。同样,您可以将字段设置为仅在索引中且可搜索,但不可存储。

我也是 ElasticSearch 的新手,不能总是理解这些问题!我实际上对您的查询感到困惑。如果您要“仅过滤”,则不需要查询。我不明白的是您在术语过滤器中使用了两个字段。我从来没有这样做过。我猜它充当OR?此外,如果没有匹配项,它似乎会返回所有内容。如果您想要一个过滤了该查询结果的查询,那么您需要使用

-d '{
   "query": {
      "filtered": {
         "query": {},
         "filter": {}
      }
   }
}'

如果您只想应用过滤器,那么过滤器应该可以在没有任何“查询”的情况下工作

-d '{
   "filter": {
      "bool": {
         "must": [
            {
               "term": {
                  "show_in_catalogue": true
               }
            },
            {
               "term": {
                  "prices.site_id": 1
               }
            }
         ]
      }
   }
}'
于 2013-10-10T19:29:04.833 回答