[英]Elastic Search multiple AND + OR query
My document schema is as follows.我的文档架构如下。
Tweet鸣叫
Id
Title
Body
Privacy -> Values can only be ["Me", "Anyone", "Team"]
UserId
Now I only want to retrieve all tweets with the Title containing "chicken sandwich" that has the privacy of "Anyone" or "Team".现在我只想检索标题包含“鸡肉三明治”且具有“任何人”或“团队”隐私的所有推文。 However, within that same query, I only want Tweets that have a privacy of "Me" to be returned if the UserId of that Tweet is "1456".
但是,在同一个查询中,如果该推文的 UserId 是“1456”,我只希望返回具有“我”隐私的推文。
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match": {
"title": {
"query": "chicken_sandwhich"
}
}
}
]
}
}
],
"should": [
{
"bool": {
"should": [
{
"term": {
"privacy": {
"value": "Anyone"
}
}
},
{
"term": {
"privacy": {
"value": "Team"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"privacy": {
"value": "Me"
}
}
},
{
"term": {
"userId.keyword": {
"value": "my_user_id"
}
}
}
]
}
}
]
}
}
}
How can I structure that query?如何构建该查询? I have been struggling and just need help on what the type of query I am looking for.
我一直在苦苦挣扎,只需要关于我正在寻找的查询类型的帮助。
It looks like you need two must
s wrapped within one should
query:看起来您需要两个
must
包裹在一个should
查询中:
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"Title": "chicken sandwich"
}
},
{
"terms": {
"Privacy.keyword": [
"Anyone",
"Team"
]
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"Privacy.keyword": {
"value": "Me"
}
}
},
{
"term": {
"UserId": {
"value": 1456
}
}
}
]
}
}
]
}
}
}
You might also want to consider query_string
.您可能还想考虑
query_string
。 which would let you write your query in a straightforward manner, like:这将让您以简单的方式编写查询,例如:
Title:"chicken sandwich" AND (
Privacy:(Anyone OR Team)
OR (Privacy:Me AND UserId:1456)
)
or in the actual format:或以实际格式:
{
"query": {
"query_string": {
"query": "Title:\"chicken sandwich\" AND (Privacy:(Anyone OR Team) OR (Privacy:Me AND UserId:1456))",
}
}
}
See documentation here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html请参阅此处的文档: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.