elasticsearch - Elastic Search - 使用 bool 值和 ids 进行 OR 过滤

标签 elasticsearch

我正在尝试搜索项目,其中一些可能是私有(private)的。

如果项目是私有(private)的,则只有项目所有者的 friend (数组 item.friends)可以看到该项目。 如果它不是私有(private)的,那么每个人都可以看到它。

所以我的逻辑是: 如果项目不是is_private (is_private=0)OR用户ID(在我的示例中为4)在数组item.friends<中,用户可以看到该项目。

我仍然没有得到任何结果。现在每个项目都设置为 is_private=1,所以我猜我的 ids 过滤器有问题。

有什么建议吗?

// ---- Mapping
{
    "item": {
        "properties": {
            "name": {
                "type": "string"
            },
            "description": {
                "type": "string"
            },
            "created": {
                "type": "date"
            },
            "location": {
                "properties": {
                    "location": {
                        "type": "geo_point"
                    }
                }
            },
            "is_proaccount": {
                "type": "integer"
            },
            "is_given_away": {
                "type": "integer"
            },
            "is_private": {
                "type": "integer"
            },
            "friends": {
                "type": "integer",
                "index_name": "friend"
            }
        }
    }
}

// ----- Example insert
{
    "name": "Test",
    "description": "Test",
    "created": "2012-02-20T12:21:30",
    "location": {
        "location": {
            "lat": "59.919914",
            "lon": "10.753414"
        }
    },
    "is_proaccount": "0",
    "is_given_away": "0",
    "is_private": 1,
    "friends": [
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10
    ]
}

// -----  Query
{
    "from": 0,
    "size": 30,
    "filter": {
        "or": [
            {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "is_private": 0
                            }
                        }
                    ]
                }
            },
            {
                "ids": {
                    "values": [
                        4
                    ],
                    "type": "friends"
                }
            }
        ]
    },
    "query": {
        "match_all": {}
    }
}

最佳答案

“ids”过滤器的含义可能并不像您想象的那样:它根据文档 ID(以及可选的文档类型)进行过滤。

参见http://www.elasticsearch.org/guide/reference/query-dsl/ids-filter.html

关于elasticsearch - Elastic Search - 使用 bool 值和 ids 进行 OR 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9391308/

相关文章:

elasticsearch - Logstash conf文件不起作用,语法有什么问题?

docker - 无法使用 docker-compose 连接到 kibana/elasticsearch

java - 带有嵌套数组的 Elasticsearch 或嵌套数组的路径是什么?

elasticsearch - 订单是否出现在 `should`影响查询结果上?

elasticsearch - 在elasticSearch中使用RestClient检索特定字段

elasticsearch - 通过第三个从一个索引到另一个索引进行聚合和过滤

mysql - 并行运行处理器时,我应该如何选择下一个要处理的项目?

amazon-web-services - 通过Rails中的rake任务在Elastic Search中更快地建立索引

elasticsearch - 创建索引时出现Elasticsearch错误

elasticsearch - 通过文本字段聚合和排序,并在Elasticsearch中连接其他文本字段