简体   繁体   English

Elasticsearch 过滤动态聚合字段

[英]Elasticsearch Filter Dynamically Aggregated Fields

I am trying to serach/filter results based on front-end selections in available acategories:我正在尝试根据可用类别中的前端选择来搜索/过滤结果:

I am able to get data in hits, and aggreagation as well, which gives me all available productCategory, Color and Brand.我能够获取命中数据和聚合数据,这为我提供了所有可用的产品类别、颜色和品牌。

Now when someone selects a brand, I want to be able to send that variable to my backend via UriParams现在,当有人选择一个品牌时,我希望能够通过 UriParams 将该变量发送到我的后端

'example.com/search?productBrand=Apple'

Now, for one filter, I am able to do it, and more but what if an user selects 1 Brand Filter, 2 Colors and 3 Product Categories.现在,对于一个过滤器,我可以做到,但如果用户选择 1 个品牌过滤器、2 个 Colors 和 3 个产品类别怎么办。 How am I able to do that in my back end?我怎么能在我的后端做到这一点?

This is my body object that I am sending via Axios using elastic-js这是我的身体 object,我使用 elastic-js 通过 Axios 发送

body: {
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "mainCategory.v.keyword": "HaSoftrdware"
          }
        }
      ],
      "must": [
        {
          "terms": {
            "cObj.n.keyword": [
              "Apple",
              "Microsoft"
            ]
          }
        }
      ]
    }
    },
    "from": from,
    "size": 6,
    "sort": [
      {
        "price": {
          "order": "asc"
        }
      }
    ],
    "aggs": {
      "mainCategory": {
        "terms": {
          "field": "mainCategory.n.keyword",
        }
      },
      "city": {
        "terms": {
          "field": "cObj.n.keyword",
        }
      },
      "make": {
        "terms": {
          "field": "carMake.n.keyword",
        }
      },
      "model": {
        "terms": {
          "field": "carModel.n.keyword",
        }
      },
      "gasoline": {
        "terms": {
          "field": "carGasoline.n.keyword",
        }
      },
      "adType": {
        "terms": {
          "field": "adType.keyword",
        }
      },
      "carTable": {
        "terms": {
          "field": "carTable.n.keyword",
        }
      }
    }
  }

Do I need to pre-define everything in my back-end or how is it archived.. this is getting over my head last 2-3 days I am struggling我是否需要预先定义后端中的所有内容或如何存档.. 过去 2-3 天这让我难以理解我正在苦苦挣扎

Cheers干杯

To solve your problem you can have querying engine (with visitors design patterns) that will generate proper query body that you need for searching.为了解决您的问题,您可以使用查询引擎(带有访问者设计模式)来生成搜索所需的适当查询正文。 Also Elastic clients in different languages can be used to achieve what you want.此外,可以使用不同语言的 Elastic 客户端来实现您想要的。 you can find elastic node.js client documentation here .您可以在此处找到弹性 node.js 客户端文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM