marcelsud/elastic-search-plugin

Sylius的Elasticsearch集成。

安装: 1

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 44

类型:sylius-bundle


README

Sylius的Elasticsearch。 Linux上的构建状态

用法

  1. 安装它

    $ composer require sylius/elastic-search-plugin
  2. 安装Elasticsearch服务器

    $ brew install elasticsearch@5.0
  3. 运行Elasticsearch服务器

    $ elasticsearch
  4. 将以下包添加到AppKernel.php

     new \ONGR\ElasticsearchBundle\ONGRElasticsearchBundle(),
     new \SimpleBus\SymfonyBridge\SimpleBusCommandBusBundle(),
     new \SimpleBus\SymfonyBridge\SimpleBusEventBusBundle(),
     new \ONGR\FilterManagerBundle\ONGRFilterManagerBundle(),
     new \Sylius\ElasticSearchPlugin\SyliusElasticSearchPlugin(),
  5. 创建/设置数据库

    $ bin/console ongr:es:index:create
    $ bin/console do:da:cr
    $ bin/console do:sch:cr
    $ bin/console syl:fix:lo

    如果创建Elasticsearch索引出现问题,请运行以下命令

    $ bin/console ongr:es:index:drop --force
    $ bin/console ongr:es:index:create
  6. app/config/config.yml中导入配置文件以进行默认过滤器集配置

    imports:
       - { resource: "@SyliusElasticSearchPlugin/Resources/config/app/config.yml" }

    更多配置请参阅ONGRFilterManager

  7. app/config/config.yml中配置ONGR包

    ongr_elasticsearch:
        managers:
            default:
                index:
                    index_name: sylius
                mappings:
                    SyliusElasticSearchPlugin: {}
    
    sylius_elastic_search:
        attribute_whitelist: ['MUG_COLLECTION_CODE', 'MUG_MATERIAL_CODE'] #Only attibutes with these codes will be indexed
  8. 导入路由文件

       sylius_search:
           resource: "@SyliusElasticSearchPlugin/Resources/config/routing.yml"
  9. 示例请求/响应

搜索时需要传递channel参数。要激活过滤器,需要在参数(查询、请求、属性)中传递请求的字段,请参阅配置部分中的参考。

例如

    /shop-api/taxon-products/mugs?channel=WEB_DE&price=2000;3000

将激活taxon_slugprice_rangechannel过滤器。

请求

    /shop-api/taxon-products/mugs?channel=WEB_GB

响应

{
    "items": [
        {
            "code": "LOGAN_MUG_CODE",
            "name": "Logan Mug",
            "slug": "logan-mug",
            "taxons": [
                {
                    "code": "MUG",
                    "slug": "mugs",
                    "position": 0,
                    "images": [],
                    "description": "@string@"
                }
            ],
            "variants": [
                {
                    "code": "LOGAN_MUG_CODE",
                    "name": "Logan Mug",
                    "price": {
                        "current": 1999,
                        "currency": "GBP"
                    },
                    "images": []
                }
            ],
            "attributes": [
                {
                    "code": "MUG_COLLECTION_CODE",
                    "name": "Mug collection",
                    "value": "HOLIDAY COLLECTION"
                },
                {
                    "code": "MUG_MATERIAL_CODE",
                    "name": "Mug material",
                    "value": "Wood"
                }
            ],
            "images": [],
            "channelCode": "WEB_GB",
            "localeCode": "en_GB",
            "mainTaxon": {
                "code": "MUG",
                "slug": "mugs",
                "images": [],
                "description": "@string@"
            }
        },
        {
            "code": "LOGAN_MUG_CODE",
            "name": "Logan Becher",
            "slug": "logan-becher",
            "taxons": [
                {
                    "code": "MUG",
                    "slug": "mugs",
                    "position": 0,
                    "images": [],
                    "description": "@string@"
                }
            ],
            "variants": [
                {
                    "code": "LOGAN_MUG_CODE",
                    "name": "Logan Becher",
                    "price": {
                        "current": 1999,
                        "currency": "GBP"
                    },
                    "images": []
                }
            ],
            "attributes": [
                {
                    "code": "MUG_COLLECTION_CODE",
                    "name": "Mug collection",
                    "value": "FEIERTAGSKOLLEKTION"
                },
                {
                    "code": "MUG_MATERIAL_CODE",
                    "name": "Mug material",
                    "value": "Holz"
                }
            ],
            "images": [],
            "channelCode": "WEB_GB",
            "localeCode": "de_DE",
            "mainTaxon": {
                "code": "MUG",
                "slug": "mugs",
                "images": [],
                "description": "@string@"
            }
        }
    ],
    "filters": {
        "channel": {
            "state": {
                "active": false,
                "urlParameters": [],
                "name": "channel",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": {
                "taxon_slug": "mugs"
            },
            "name": "channel",
            "choices": [
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "channel": "WEB_GB"
                    },
                    "label": "WEB_GB",
                    "count": 2
                }
            ]
        },
        "taxon_slug": {
            "state": {
                "active": true,
                "value": "mugs",
                "urlParameters": {
                    "taxon_slug": "mugs"
                },
                "name": "taxon_slug",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": [],
            "name": "taxon_slug",
            "choices": [
                {
                    "active": true,
                    "default": false,
                    "urlParameters": [],
                    "label": "mugs",
                    "count": 2
                },
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "t-shirts"
                    },
                    "label": "t-shirts",
                    "count": 2
                }
            ]
        },
        "price_range": {
            "state": {
                "active": false,
                "urlParameters": [],
                "name": "price_range",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": {
                "taxon_slug": "mugs"
            },
            "name": "price_range",
            "minBounds": 1999,
            "maxBounds": 1999
        },
        "locale": {
            "state": {
                "active": false,
                "urlParameters": [],
                "name": "locale",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": {
                "taxon_slug": "mugs"
            },
            "name": "locale",
            "choices": [
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "locale_code": "de_DE"
                    },
                    "label": "de_DE",
                    "count": 1
                },
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "locale_code": "en_GB"
                    },
                    "label": "en_GB",
                    "count": 1
                }
            ]
        },
        "attribute_values": {
            "state": {
                "active": false,
                "urlParameters": [],
                "name": "attribute_values",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": {
                "taxon_slug": "mugs"
            },
            "name": "attribute_values",
            "choices": [
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "attributeValues": [
                            "FEIERTAGSKOLLEKTION"
                        ]
                    },
                    "label": "FEIERTAGSKOLLEKTION",
                    "count": 1
                },
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "attributeValues": [
                            "HOLIDAY COLLECTION"
                        ]
                    },
                    "label": "HOLIDAY COLLECTION",
                    "count": 1
                },
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "attributeValues": [
                            "Holz"
                        ]
                    },
                    "label": "Holz",
                    "count": 1
                },
                {
                    "active": false,
                    "default": false,
                    "urlParameters": {
                        "taxon_slug": "mugs",
                        "attributeValues": [
                            "Wood"
                        ]
                    },
                    "label": "Wood",
                    "count": 1
                }
            ]
        },
        "paginator": {
            "state": {
                "active": false,
                "value": 1,
                "urlParameters": [],
                "name": "paginator",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": {
                "taxon_slug": "mugs"
            },
            "name": "paginator",
            "currentPage": 1,
            "totalItems": 2,
            "maxPages": 10,
            "itemsPerPage": 10,
            "numPages": 1,
            "options": []
        },
        "search": {
            "state": {
                "active": false,
                "urlParameters": [],
                "name": "search",
                "options": []
            },
            "tags": [],
            "urlParameters": {
                "taxon_slug": "mugs"
            },
            "resetUrlParameters": {
                "taxon_slug": "mugs"
            },
            "name": "search"
        }
    }
}
  1. 按属性过滤

您需要使用属性查询参数,它是一个关联数组,键是属性名称,值是属性值的数组。例如

$this->client->request('GET', '/shop-api/products', ['attributes' => ['Mug material' => ['Wood']]], [], ['ACCEPT' => 'application/json']);

此过滤器还将聚合所有属性值,并按属性名称对它们进行分组。这是此请求的聚合响应

  "attributes":{
      "state":{
        "active":true,
        "value":{
          "Mug material":[
            "Wood"
          ]
        },
        "urlParameters":{
          "attributes":{
            "Mug material":[
              "Wood"
            ]
          }
        },
        "name":"attributes",
        "options":[

        ]
      },
      "tags":[

      ],
      "urlParameters":{
        "attributes":{
          "Mug material":[
            "Wood"
          ]
        }
      },
      "resetUrlParameters":[

      ],
      "name":"attributes",
      "items":[
        {
          "tags":[

          ],
          "urlParameters":[

          ],
          "resetUrlParameters":[

          ],
          "name":"Mug collection",
          "choices":{
            "HOLIDAY COLLECTION":{
              "active":false,
              "default":false,
              "urlParameters":{
                "attributes":{
                  "Mug material":[
                    "Wood"
                  ],
                  "Mug collection":[
                    "HOLIDAY COLLECTION"
                  ]
                }
              },
              "label":"HOLIDAY COLLECTION",
              "count":1
            }
          }
        },
        {
          "tags":[

          ],
          "urlParameters":[

          ],
          "resetUrlParameters":[

          ],
          "name":"Mug material",
          "choices":{
            "Holz":{
              "active":false,
              "default":false,
              "urlParameters":{
                "attributes":{
                  "Mug material":[
                    "Wood",
                    "Holz"
                  ]
                }
              },
              "label":"Holz",
              "count":1
            },
            "Wood":{
              "active":true,
              "default":false,
              "urlParameters":{
                "attributes":{
                  "Mug material":[

                  ]
                }
              },
              "label":"Wood",
              "count":1
            }
          }
        }
      ]
    }

您可以将过滤器组合起来,例如,如果您想按特定区域过滤产品,可以添加另一个查询参数

带有区域的示例请求

$this->client->request('GET', '/shop-api/products', ['attributes' => ['Mug material' => ['Wood']], 'locale' => 'en_GB'], [], ['ACCEPT' => 'application/json']);

此请求的聚合响应

  "attributes":{  
         "state":{  
            "active":true,
            "value":{  
               "Mug material":[  
                  "Wood"
               ]
            },
            "urlParameters":{  
               "attributes":{  
                  "Mug material":[  
                     "Wood"
                  ]
               }
            },
            "name":"attributes",
            "options":[  

            ]
         },
         "tags":[  

         ],
         "urlParameters":{  
            "locale":"en_GB",
            "attributes":{  
               "Mug material":[  
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{  
            "locale":"en_GB"
         },
         "name":"attributes",
         "items":[  
            {  
               "tags":[  

               ],
               "urlParameters":[  

               ],
               "resetUrlParameters":{  
                  "locale":"en_GB"
               },
               "name":"Mug collection",
               "choices":{  
                  "HOLIDAY COLLECTION":{  
                     "active":false,
                     "default":false,
                     "urlParameters":{  
                        "locale":"en_GB",
                        "attributes":{  
                           "Mug material":[  
                              "Wood"
                           ],
                           "Mug collection":[  
                              "HOLIDAY COLLECTION"
                           ]
                        }
                     },
                     "label":"HOLIDAY COLLECTION",
                     "count":1
                  }
               }
            },
            {  
               "tags":[  

               ],
               "urlParameters":[  

               ],
               "resetUrlParameters":{  
                  "locale":"en_GB"
               },
               "name":"Mug material",
               "choices":{  
                  "Wood":{  
                     "active":true,
                     "default":false,
                     "urlParameters":{  
                        "locale":"en_GB",
                        "attributes":{  
                           "Mug material":[  

                           ]
                        }
                     },
                     "label":"Wood",
                     "count":1
                  }
               }
            }
         ]
      }

整个响应

{
   "items":[
      {
         "code":"LOGAN_MUG_CODE",
         "name":"Logan Mug",
         "slug":"logan-mug",
         "taxons":{
            "main":"MUG",
            "others":[
               "MUG",
               "CATEGORY",
               "BRAND"
            ]
         },
         "variants":[
            {
               "code":"LOGAN_MUG_CODE",
               "name":"Logan Mug",
               "price":{
                  "current":1999,
                  "currency":"GBP"
               },
               "images":[

               ]
            }
         ],
         "attributes":[
            {
               "code":"MUG_COLLECTION_CODE",
               "name":"Mug collection",
               "value":"HOLIDAY COLLECTION"
            },
            {
               "code":"MUG_MATERIAL_CODE",
               "name":"Mug material",
               "value":"Wood"
            }
         ],
         "images":[

         ],
         "channelCode":"WEB_GB",
         "localeCode":"en_GB"
      }
   ],
   "filters":{
      "channel":{
         "state":{
            "active":false,
            "urlParameters":[

            ],
            "name":"channel",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "name":"channel",
         "choices":[
            {
               "active":false,
               "default":false,
               "urlParameters":{
                  "locale":"en_GB",
                  "attributes":{
                     "Mug material":[
                        "Wood"
                     ]
                  },
                  "channel":"WEB_GB"
               },
               "label":"WEB_GB",
               "count":1
            }
         ]
      },
      "taxon_code":{
         "state":{
            "active":false,
            "urlParameters":[

            ],
            "name":"taxon_code",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "name":"taxon_code",
         "choices":[
            {
               "active":false,
               "default":false,
               "urlParameters":{
                  "locale":"en_GB",
                  "attributes":{
                     "Mug material":[
                        "Wood"
                     ]
                  },
                  "taxonCode":"BRAND"
               },
               "label":"BRAND",
               "count":1
            },
            {
               "active":false,
               "default":false,
               "urlParameters":{
                  "locale":"en_GB",
                  "attributes":{
                     "Mug material":[
                        "Wood"
                     ]
                  },
                  "taxonCode":"CATEGORY"
               },
               "label":"CATEGORY",
               "count":1
            },
            {
               "active":false,
               "default":false,
               "urlParameters":{
                  "locale":"en_GB",
                  "attributes":{
                     "Mug material":[
                        "Wood"
                     ]
                  },
                  "taxonCode":"MUG"
               },
               "label":"MUG",
               "count":1
            }
         ]
      },
      "price_range":{
         "state":{
            "active":false,
            "urlParameters":[

            ],
            "name":"price_range",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "name":"price_range",
         "minBounds":1999,
         "maxBounds":2999
      },
      "locale":{
         "state":{
            "active":true,
            "value":"en_GB",
            "urlParameters":{
               "locale":"en_GB"
            },
            "name":"locale",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "name":"locale",
         "choices":[
            {
               "active":true,
               "default":false,
               "urlParameters":{
                  "attributes":{
                     "Mug material":[
                        "Wood"
                     ]
                  }
               },
               "label":"en_GB",
               "count":1
            }
         ]
      },
      "paginator":{
         "state":{
            "active":false,
            "value":1,
            "urlParameters":[

            ],
            "name":"paginator",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "name":"paginator",
         "currentPage":1,
         "totalItems":1,
         "maxPages":10,
         "itemsPerPage":10,
         "numPages":1,
         "options":[

         ]
      },
      "search":{
         "state":{
            "active":false,
            "urlParameters":[

            ],
            "name":"search",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "name":"search"
      },
      "attributes":{
         "state":{
            "active":true,
            "value":{
               "Mug material":[
                  "Wood"
               ]
            },
            "urlParameters":{
               "attributes":{
                  "Mug material":[
                     "Wood"
                  ]
               }
            },
            "name":"attributes",
            "options":[

            ]
         },
         "tags":[

         ],
         "urlParameters":{
            "locale":"en_GB",
            "attributes":{
               "Mug material":[
                  "Wood"
               ]
            }
         },
         "resetUrlParameters":{
            "locale":"en_GB"
         },
         "name":"attributes",
         "items":[
            {
               "tags":[

               ],
               "urlParameters":[

               ],
               "resetUrlParameters":{
                  "locale":"en_GB"
               },
               "name":"Mug collection",
               "choices":{
                  "HOLIDAY COLLECTION":{
                     "active":false,
                     "default":false,
                     "urlParameters":{
                        "locale":"en_GB",
                        "attributes":{
                           "Mug material":[
                              "Wood"
                           ],
                           "Mug collection":[
                              "HOLIDAY COLLECTION"
                           ]
                        }
                     },
                     "label":"HOLIDAY COLLECTION",
                     "count":1
                  }
               }
            },
            {
               "tags":[

               ],
               "urlParameters":[

               ],
               "resetUrlParameters":{
                  "locale":"en_GB"
               },
               "name":"Mug material",
               "choices":{
                  "Wood":{
                     "active":true,
                     "default":false,
                     "urlParameters":{
                        "locale":"en_GB",
                        "attributes":{
                           "Mug material":[

                           ]
                        }
                     },
                     "label":"Wood",
                     "count":1
                  }
               }
            }
         ]
      }
   }
}
  1. 排序
  • 按名称升序

        /shop-api/products?channel=WEB_DE&sort[name]=asc
    
  • 按价格降序

        /shop-api/products?channel=WEB_DE&sort[price]=desc
    
  • 按属性ATTRIBUTE_CODE升序

        /shop-api/products?channel=WEB_DE&sort[attributes][ATTRIBUTE_CODE]=asc
    
  • 按价格升序,然后按名称降序

        /shop-api/products?channel=WEB_DE&sort[price]=asc&sort[name]=desc
    
  1. 按属性过滤
  • 按属性名称和值

        /shop-api/products?channel=WEB_DE&attributes[Attribute name]=value
    
  • 按属性代码和值

        /shop-api/products?channel=WEB_DE&attributesByCode[ATTRIBUTE_CODE]=value
    
  1. 重新索引Elasticsearch

当前实现不支持在实体更新时更新Elasticsearch。为了保持最新状态,请运行以下命令

```
    bin/console sylius:elastic-search:update-product-index
```

如果您想重新创建索引,请运行(这将删除它)

```
    bin/console sylius:elastic-search:reset-product-index
```