首页 > 解决方案 > 如何在搜索查询中加入文档

问题描述

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "product_index",
        "_type": "product",
        "_id": "1115",
        "_score": 0.2876821,
        "_source": {
          "isactive": true,
          "in_use": false,
          "brand_name": "Adidas",
          "sku_id": "56456487987987",
          "long_description": "this is long description",
          "key_feature": [
            {
              "id": 1148,
              "key_feature": "sport wear"
            },
            {
              "id": 1147,
              "key_feature": "Cotton shirt"
            },
            {
              "id": 1146,
              "key_feature": "White and blue"
            }
          ],
          "isdeleted": false,
          "created_by": null,
          "brand_id": 5,
          "search_terms": [
            {
              "label": "white shirt",
              "value": 9
            }
          ]
          "color_id": 2,
          "specific_keywords": "",
          "item_list": [
            {
              "item_id": 1114,
              "product_id": 1115,
              "isactive": true,
              "id": 9,
              "isdeleted": false
            },
            {
              "item_id": 1113,
              "product_id": 1115,
              "isactive": true,
              "id": 10,
              "isdeleted": false
            }
          ],
          "upc_code": "",
          "display_size": "L",
          "name": "New White shirt",
          "updated_by": null,
          "id": 1115,
          "updated_date": "2020-03-25T08:24:37.644571+00:00",
          "color_name": "blue",
          "created_date": "2020-03-25T08:11:14.966673+00:00",
          "category": [
            {
              "parent_category_id": 78,
              "sub_sub_category": null,
              "sub_category": null,
              "sub_category_id": null,
              "sub_sub_category_id": null,
              "parent_category": "new Shirt Cate",
              "id": 1151
            }
          ]
        }
      },
      {
        "_index": "product_index",
        "_type": "product",
        "_id": "1113",
        "_score": 0.2876821,
        "_source": {
          "isactive": true,
          "in_use": false,
          "sku_id": "1456456488",
          "brand_name": "Adidas",
          "long_description": "",
          "key_feature": [
            {
              "id": 1142,
              "key_feature": "Cotton"
            },
            {
              "id": 1141,
              "key_feature": "Office Use"
            },
            {
              "id": 1140,
              "key_feature": "Black formal"
            }
          ],
          "isdeleted": false,
          "created_by": null,
          "brand_id": 5,
          "search_terms": [

          ]
          "color_id": 1,
          "specific_keywords": "",
          "item_list": [

          ],
          "display_size": "L",
          "upc_code": "",
          "name": "New Cotton formal shirt black",
          "updated_by": null,
          "id": 1113,
          "updated_date": "2020-03-25T06:48:30.903041+00:00",
          "created_date": "2020-03-25T06:48:29.943043+00:00",
          "color_name": "black",
          "category": [
            {
              "sub_sub_category": null,
              "parent_category_id": 54,
              "sub_category": null,
              "sub_category_id": null,
              "sub_sub_category_id": null,
              "parent_category": "MEN'S CLOTHING",
              "id": 1149
            }
          ]
        }
      },
      {
        "_index": "product_index",
        "_type": "product",
        "_id": "1114",
        "_score": 0.2876821,
        "_source": {
          "isactive": true,
          "in_use": false,
          "sku_id": "145645648811",
          "brand_name": "Adidas",
          "long_description": "",
          "key_feature": [
            {
              "id": 1145,
              "key_feature": "Cotton"
            },
            {
              "id": 1144,
              "key_feature": "Office Use"
            },
            {
              "id": 1143,
              "key_feature": "Black formal"
            }
          ],
          "isdeleted": false,
          "created_by": null,
          "brand_id": 5,
          "search_terms": [

          ],
          "color_id": 1,
          "specific_keywords": "",
          "item_list": [

          ],
          "display_size": "L",
          "upc_code": "",
          "updated_by": null,
          "name": "New Cotton Casual shirt black",
          "id": 1114,
          "created_date": "2020-03-25T07:13:26.233675+00:00",
          "color_name": "black",
          "updated_date": "2020-03-25T07:13:27.229363+00:00",
          "category": [
            {
              "sub_sub_category": null,
              "parent_category_id": 54,
              "sub_category": null,
              "sub_category_id": null,
              "sub_sub_category_id": null,
              "parent_category": "MEN'S CLOTHING",
              "id": 1150
            }
          ]
        }
      }
    ]
  }
}

我的要求是附加所有相关文档,其中包含基于 item_id 在 item_list 中指定的特定键值字段。在上面的结果文档 id 1115 中有 item_list,其中包含 item_id 1114 和 1113。因此特定字段附加在文档 1115 中。弹性搜索中的搜索查询应该是什么?

标签: pythonelasticsearchdjango-rest-framework

解决方案


你不能加入 Elasticsearch,为了实现你的目标,你可以做两件事:

  1. 在 item_id 1115 中复制 item_id 1114 和 1113 的信息(当然在所有其他文档中)。
  2. 在应用程序级别加入,因此在此查询之后,您可以提取 item_id 1114 和 1113 并运行另外两个查询以获取有关此项目的信息。然后在应用程序级别加入所有 json。

推荐阅读