如何将 ElasticSearch 查询参数(DSL 查询)用于多种类型?

发布于 2024-12-01 05:10:43 字数 2550 浏览 1 评论 0原文

过去几个月我一直在使用 ElasticSearch,但当我必须传递复杂的查询时,仍然发现它很复杂。

我想运行必须搜索多个“类型”的查询,并且必须使用自己的“过滤器”搜索每种类型,但需要组合“搜索结果”

例如:

我需要搜索“用户类型” ”文档是我的朋友,同时我必须根据提供的关键字搜索我喜欢的“对象类型”文档。

OR

同时具有“AND”和“NOT”子句的查询

示例查询:

$options['query'] = array(
        'query' => array(
            'filtered' => array(
                'query' => array(
                    'query_string' => array(
                        'default_field' => 'name',
                        'query' => $this->search_term . '*',
                    ),
                ),
                'filter' => array(
                    'and' => array(
                        array(
                            'term' => array(
                                'access_id' => 2,
                            ),
                        ),
                    ),

                    'not' => array(
                        array(
                            'term' => array(
                                'follower' => 32,
                            ),
                        ),

                        array(
                            'term' => array(
                                'fan' => 36,
                            ),
                        ),
                    ),
                ),
            ),
        ),
    );

因为此查询旨在搜索 access_id = 2 的用户,但不得具有 id 32 的关注者和 id 36 的粉丝,

但这不是工作..

编辑:修改后的查询

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "not": {
              "filter": {
                "and": [
                  {
                    "query": {
                      "query_string": {
                        "default_field": "fan",
                        "query": "*510*"
                      }
                    }
                  },
                  {
                    "query": {
                      "query_string": {
                        "default_field": "follower",
                        "query": "*510*"
                      }
                    }
                  }
                ]
              }
            }
          },
          {
            "term": {
              "access_id": 2
            }
          }
        ]
      },
      "query": {
        "field": {
          "name": "xyz*"
        }
      }
    }
  }
}

现在运行此查询后,我得到两个结果,一个带有关注者:“34,518” &风扇:“510”,第二个风扇:“34”,但它不应该只是结果中的第二个。

有什么想法吗?

I have been working with the ElasticSearch from last few months, but still find it complicated when I have to pass an complicated query.

I want to run the query which will have to search the multiple "types" and each type has to be searched with its own "filters", but need to have combined "searched results"

For example:

I need to search the "user type" document which are my friends and on the same time i have to search the "object type" document which I like, according to the keyword provided.

OR

The query that has both the "AND" and "NOT" clause

Example query:

$options['query'] = array(
        'query' => array(
            'filtered' => array(
                'query' => array(
                    'query_string' => array(
                        'default_field' => 'name',
                        'query' => $this->search_term . '*',
                    ),
                ),
                'filter' => array(
                    'and' => array(
                        array(
                            'term' => array(
                                'access_id' => 2,
                            ),
                        ),
                    ),

                    'not' => array(
                        array(
                            'term' => array(
                                'follower' => 32,
                            ),
                        ),

                        array(
                            'term' => array(
                                'fan' => 36,
                            ),
                        ),
                    ),
                ),
            ),
        ),
    );

as this query is meant to search the user with access_id = 2, but must not have the follower of id 32 and fan of id 36

but this is not working..

Edit: Modified query

{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "not": {
              "filter": {
                "and": [
                  {
                    "query": {
                      "query_string": {
                        "default_field": "fan",
                        "query": "*510*"
                      }
                    }
                  },
                  {
                    "query": {
                      "query_string": {
                        "default_field": "follower",
                        "query": "*510*"
                      }
                    }
                  }
                ]
              }
            }
          },
          {
            "term": {
              "access_id": 2
            }
          }
        ]
      },
      "query": {
        "field": {
          "name": "xyz*"
        }
      }
    }
  }
}

now after running this query, i am getting two results, one with follower: "34,518" & fan: "510" and second with fan:"34", but isn't it supposed to be only the second one in the result.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡间太子 2024-12-08 05:10:43

您可能想查看我本月提供的演示文稿的幻灯片,其中解释了查询 DSL 工作原理的基础知识:

亲密关系 - ElasticSearch 查询 DSL 解释

您的查询的问题在于您的过滤器嵌套不正确。 andnot 过滤器处于同一级别,但 not 过滤器应位于 and 下:

curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1'  -d '
{
   "query" : {
      "filtered" : {
         "filter" : {
            "and" : [
               {
                  "not" : {
                     "filter" : {
                        "and" : [
                           {
                              "term" : {
                                 "fan" : 36
                              }
                           },
                           {
                              "term" : {
                                 "follower" : 32
                              }
                           }
                        ]
                     }
                  }
               },
               {
                  "term" : {
                     "access_id" : 2
                  }
               }
            ]
         },
         "query" : {
            "field" : {
               "name" : "keywords to search"
            }
         }
      }
   }
}
'

You may want to look at the slides of a presentation that I gave this month, which explains the basics of how the query DSL works:

Terms of endearment - the ElasticSearch Query DSL explained

The problem with your query is that your filters are nested incorrectly. The and and not filters are at the same level, but the not filter should be under and:

curl -XGET 'http://127.0.0.1:9200/_all/_search?pretty=1'  -d '
{
   "query" : {
      "filtered" : {
         "filter" : {
            "and" : [
               {
                  "not" : {
                     "filter" : {
                        "and" : [
                           {
                              "term" : {
                                 "fan" : 36
                              }
                           },
                           {
                              "term" : {
                                 "follower" : 32
                              }
                           }
                        ]
                     }
                  }
               },
               {
                  "term" : {
                     "access_id" : 2
                  }
               }
            ]
         },
         "query" : {
            "field" : {
               "name" : "keywords to search"
            }
         }
      }
   }
}
'
忆梦 2024-12-08 05:10:43

我刚刚用“BOOL”尝试了

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "access_id": 2
          }
        },
        {
          "wildcard": {
            "name": "xyz*"
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "follower": "*510*"
          }
        },
        {
          "wildcard": {
            "fan": "*510*"
          }
        }
      ]
    }
  }
}

它给出了正确的答案。

但我不确定它应该这样使用吗?

I just tried it with the "BOOL"

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "access_id": 2
          }
        },
        {
          "wildcard": {
            "name": "xyz*"
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "follower": "*510*"
          }
        },
        {
          "wildcard": {
            "fan": "*510*"
          }
        }
      ]
    }
  }
}

It gives the correct answer.

but I'm not sure should it be used like this ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文