在不覆盖现有元素的情况下不能插入元素

时间:2019-12-17 10:30:56

标签: php elasticsearch

尝试通过Elasticsearch-PHP向现有索引插入元素,但是会覆盖该索引中的现有元素。我正在使用index(..)方法:

$params = [
            'index' => 'my-index',
            'type' => '_doc',
            'id' => $request['site_id']
        ];

$params['body'] = [
                'partnerId' => $request['site_id'],
                'lang' => $request['lang'],
                'info' => $request['info'],
                'options' => $request['options']
            ]

$client = ClientBuilder::create()->build();
$client->index($params);

如何通过elasticSearch PHP客户端发出PUT请求?

已更新,以提供有关我遇到的问题的更多信息。这是我在index(..)方法调用之前在Elasticsearch中所拥有的东西:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "partner-settings",
        "_type": "_doc",
        "_id": "1ru",
        "_score": 1.0,
        "_source": {
          "partnerId": 1,
          "lang": "ru",
          "loggedInMenu": {
            "pathPrefix": "/user",
            "sections": [
              {
                "title": "Баланс",
                "path": "/balance",
                "icon": "icon-balance"
              },
              {
                "title": "Уведомления",
                "path": "/notifications",
                "icon": "icon-notifications-bell"
              },
              {
                "title": "Бонусы",
                "path": "/bonuses",
                "icon": "icon-bonus"
              },
              {
                "title": "Личные Данные",
                "path": "/personal-details",
                "icon": "icon-profile"
              },
              {
                "title": "Верификация",
                "path": "/verification",
                "icon": "icon-docs"
              },
              {
                "title": "Снять со Счета",
                "path": "/withdraw",
                "icon": "icon-withdraw"
              },
              {
                "title": "История",
                "path": "/transaction-history",
                "icon": "icon-history"
              },
              {
                "title": "Лимиты",
                "path": "/limits",
                "icon": "icon-limit"
              },
              {
                "title": "Выйти",
                "logout": true,
                "icon": "icon-profile-logout"
              }
            ]
          },
          "validators": {
            "email-min-6": {
              "message": "Email should be at least 6 characters long",
              "rgx": "%5E.+%24"
            },
            "phone-number": {
              "message": "Please provide a valid phone number (6+ chars)",
              "rgx": "%5E%5B+%5D+%5B0-9%5D%7B1%2C%7D%5Cs%3F%5C%28%3F%5B0-9%5D%7B1%2C%7D%5C%29%3F%5Cs%3F%5B-%5Cs%5C0-9%5D%7B4%2C%7D%24"
            },
            "username-min-4": {
              "message": "Username should have length of at least 6 characters",
              "rgx": "%5E%5BA-Za-z0-9_-%5D%7B4%2C%7D%24"
            },
            "username-min-6": {
              "message": "Username should have length of at least 6 characters",
              "rgx": "%5E%5BA-Za-z0-9_-%5D%7B6%2C%7D%24"
            },
            "password-min-6": {
              "message": "Password should have length of at least 6 characters",
              "rgx": "%5BA-Za-z0-9%5D%7B6%2C%7D"
            },
            "email-rfc": {
              "message": "Email format is incorrect",
              "rgx": "(%3F%3A%5Ba-z0-9!%23%24%25%26'*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B(%3F%3A%5C%5C.%5Ba-z0-9!%23%24%25%26'*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B)*%7C%5C%22(%3F%3A%5B%5Cx01-%5Cx08%5Cx0b%5Cx0c%5Cx0e-%5Cx1f%5Cx21%5Cx23-%5Cx5b%5Cx5d-%5Cx7f%5D%7C%5C%5C%5B%5Cx01-%5Cx09%5Cx0b%5Cx0c%5Cx0e-%5Cx7f%5D)*%5C%22)%40(%3F%3A(%3F%3A%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%5C.)%2B%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%7C%5C%5B(%3F%3A(%3F%3A25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%5C.)%7B3%7D(%3F%3A25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F%7C%5Ba-z0-9-%5D*%5Ba-z0-9%5D%3A(%3F%3A%5B%5Cx01-%5Cx08%5Cx0b%5Cx0c%5Cx0e-%5Cx1f%5Cx21-%5Cx5a%5Cx53-%5Cx7f%5D%7C%5C%5C%5B%5Cx01-%5Cx09%5Cx0b%5Cx0c%5Cx0e-%5Cx7f%5D)%2B)%5C%5D)"
            },
            "first-name-min-3": {
              "message": "First name should contain at least 3 alphabetical characters",
              "rgx": "%5E%5BA-Za-z%5Cs%5D%7B3%2C%7D%24"
            },
            "last-name-min-3": {
              "message": "Last name should contain at least 3 alphabetical characters",
              "rgx": "%5E%5BA-Za-z%5Cs%5D%7B3%2C%7D%24"
            },
            "zip-code-min-4": {
              "message": "Zip code should be at least 4 characters long",
              "rgx": "%5E%5BA-Za-z0-9%5Cs%5D%7B4%2C%7D%24"
            }
          },
          "bonus": [

          ],
          "payments": [

          ],
          "metaTags": [

          ],
          "paymentIcons": [

          ]
        }
      },
      {
        "_index": "partner-settings",
        "_type": "_doc",
        "_id": "1en",
        "_score": 1.0,
        "_source": {
          "partnerId": 1,
          "lang": "en",
          "loggedInMenu": {
            "pathPrefix": "/user",
            "sections": [
              {
                "title": "My Balance",
                "path": "/balance",
                "icon": "icon-balance"
              },
              {
                "title": "Notifications",
                "path": "/notifications",
                "icon": "icon-notifications-bell"
              },
              {
                "title": "Bonuses",
                "path": "/bonuses",
                "icon": "icon-bonus"
              },
              {
                "title": "Personal Details",
                "path": "/personal-details",
                "icon": "icon-profile"
              },
              {
                "title": "Verification",
                "path": "/verification",
                "icon": "icon-docs"
              },
              {
                "title": "Withdraw",
                "path": "/withdraw",
                "icon": "icon-withdraw"
              },
              {
                "title": "History",
                "path": "/transaction-history",
                "icon": "icon-history"
              },
              {
                "title": "Limits",
                "path": "/limits",
                "icon": "icon-limit"
              },
              {
                "title": "Logout",
                "logout": true,
                "icon": "icon-profile-logout"
              }
            ]
          },          
          "validators": {
            "email-min-6": {
              "message": "Email should be at least 6 characters long",
              "rgx": "%5E.+%24"
            },
            "phone-number": {
              "message": "Please provide a valid phone number (6+ chars)",
              "rgx": "%5E%5B+%5D+%5B0-9%5D%7B1%2C%7D%5Cs%3F%5C%28%3F%5B0-9%5D%7B1%2C%7D%5C%29%3F%5Cs%3F%5B-%5Cs%5C0-9%5D%7B4%2C%7D%24"
            },
            "username-min-4": {
              "message": "Username should have length of at least 6 characters",
              "rgx": "%5E%5BA-Za-z0-9_-%5D%7B4%2C%7D%24"
            },
            "username-min-6": {
              "message": "Username should have length of at least 6 characters",
              "rgx": "%5E%5BA-Za-z0-9_-%5D%7B6%2C%7D%24"
            },
            "password-min-6": {
              "message": "Password should have length of at least 6 characters",
              "rgx": "%5BA-Za-z0-9%5D%7B6%2C%7D"
            },
            "email-rfc": {
              "message": "Email format is incorrect",
              "rgx": "(%3F%3A%5Ba-z0-9!%23%24%25%26'*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B(%3F%3A%5C%5C.%5Ba-z0-9!%23%24%25%26'*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B)*%7C%5C%22(%3F%3A%5B%5Cx01-%5Cx08%5Cx0b%5Cx0c%5Cx0e-%5Cx1f%5Cx21%5Cx23-%5Cx5b%5Cx5d-%5Cx7f%5D%7C%5C%5C%5B%5Cx01-%5Cx09%5Cx0b%5Cx0c%5Cx0e-%5Cx7f%5D)*%5C%22)%40(%3F%3A(%3F%3A%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%5C.)%2B%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%7C%5C%5B(%3F%3A(%3F%3A25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%5C.)%7B3%7D(%3F%3A25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F%7C%5Ba-z0-9-%5D*%5Ba-z0-9%5D%3A(%3F%3A%5B%5Cx01-%5Cx08%5Cx0b%5Cx0c%5Cx0e-%5Cx1f%5Cx21-%5Cx5a%5Cx53-%5Cx7f%5D%7C%5C%5C%5B%5Cx01-%5Cx09%5Cx0b%5Cx0c%5Cx0e-%5Cx7f%5D)%2B)%5C%5D)"
            },
            "first-name-min-3": {
              "message": "First name should contain at least 3 alphabetical characters",
              "rgx": "%5E%5BA-Za-z%5Cs%5D%7B3%2C%7D%24"
            },
            "last-name-min-3": {
              "message": "Last name should contain at least 3 alphabetical characters",
              "rgx": "%5E%5BA-Za-z%5Cs%5D%7B3%2C%7D%24"
            },
            "zip-code-min-4": {
              "message": "Zip code should be at least 4 characters long",
              "rgx": "%5E%5BA-Za-z0-9%5Cs%5D%7B4%2C%7D%24"
            }
          },
          "bonus": [

          ],
          "payments": [

          ],
          "metaTags": [

          ],
          "paymentIcons": [

          ]
        }
      },
      {
        "_index": "partner-settings",
        "_type": "_doc",
        "_id": "1am",
        "_score": 1.0,
        "_source": {
          "partnerId": 1,
          "lang": "am",
          "loggedInMenu": {
            "pathPrefix": "/user",
            "sections": [
              {
                "title": "Իմ Հաշվեկշիռը",
                "path": "/balance",
                "icon": "icon-balance"
              },
              {
                "title": "Ծանուցումներ",
                "path": "/notifications",
                "icon": "icon-notifications-bell"
              },
              {
                "title": "Բոնուսներ",
                "path": "/bonuses",
                "icon": "icon-bonus"
              },
              {
                "title": "Անձնական Տվյալներ",
                "path": "/personal-details",
                "icon": "icon-profile"
              },
              {
                "title": "Հաստատում",
                "path": "/verification",
                "icon": "icon-docs"
              },
              {
                "title": "Հանել Հաշվից",
                "path": "/withdraw",
                "icon": "icon-withdraw"
              },
              {
                "title": "Պատմություն",
                "path": "/transaction-history",
                "icon": "icon-history"
              },
              {
                "title": "Լիմիտներ",
                "path": "/limits",
                "icon": "icon-limit"
              },
              {
                "title": "Դուրս Գալ",
                "logout": true,
                "icon": "icon-profile-logout"
              }
            ]
          },          
          "validators": {
            "email-min-6": {
              "message": "Email should be at least 6 characters long",
              "rgx": "%5E.+%24"
            },
            "phone-number": {
              "message": "Please provide a valid phone number (6+ chars)",
              "rgx": "%5E%5B+%5D+%5B0-9%5D%7B1%2C%7D%5Cs%3F%5C%28%3F%5B0-9%5D%7B1%2C%7D%5C%29%3F%5Cs%3F%5B-%5Cs%5C0-9%5D%7B4%2C%7D%24"
            },
            "username-min-4": {
              "message": "Username should have length of at least 6 characters",
              "rgx": "%5E%5BA-Za-z0-9_-%5D%7B4%2C%7D%24"
            },
            "username-min-6": {
              "message": "Username should have length of at least 6 characters",
              "rgx": "%5E%5BA-Za-z0-9_-%5D%7B6%2C%7D%24"
            },
            "password-min-6": {
              "message": "Password should have length of at least 6 characters",
              "rgx": "%5BA-Za-z0-9%5D%7B6%2C%7D"
            },
            "email-rfc": {
              "message": "Email format is incorrect",
              "rgx": "(%3F%3A%5Ba-z0-9!%23%24%25%26'*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B(%3F%3A%5C%5C.%5Ba-z0-9!%23%24%25%26'*%2B%2F%3D%3F%5E_%60%7B%7C%7D~-%5D%2B)*%7C%5C%22(%3F%3A%5B%5Cx01-%5Cx08%5Cx0b%5Cx0c%5Cx0e-%5Cx1f%5Cx21%5Cx23-%5Cx5b%5Cx5d-%5Cx7f%5D%7C%5C%5C%5B%5Cx01-%5Cx09%5Cx0b%5Cx0c%5Cx0e-%5Cx7f%5D)*%5C%22)%40(%3F%3A(%3F%3A%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%5C.)%2B%5Ba-z0-9%5D(%3F%3A%5Ba-z0-9-%5D*%5Ba-z0-9%5D)%3F%7C%5C%5B(%3F%3A(%3F%3A25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F)%5C.)%7B3%7D(%3F%3A25%5B0-5%5D%7C2%5B0-4%5D%5B0-9%5D%7C%5B01%5D%3F%5B0-9%5D%5B0-9%5D%3F%7C%5Ba-z0-9-%5D*%5Ba-z0-9%5D%3A(%3F%3A%5B%5Cx01-%5Cx08%5Cx0b%5Cx0c%5Cx0e-%5Cx1f%5Cx21-%5Cx5a%5Cx53-%5Cx7f%5D%7C%5C%5C%5B%5Cx01-%5Cx09%5Cx0b%5Cx0c%5Cx0e-%5Cx7f%5D)%2B)%5C%5D)"
            },
            "first-name-min-3": {
              "message": "First name should contain at least 3 alphabetical characters",
              "rgx": "%5E%5BA-Za-z%5Cs%5D%7B3%2C%7D%24"
            },
            "last-name-min-3": {
              "message": "Last name should contain at least 3 alphabetical characters",
              "rgx": "%5E%5BA-Za-z%5Cs%5D%7B3%2C%7D%24"
            },
            "zip-code-min-4": {
              "message": "Zip code should be at least 4 characters long",
              "rgx": "%5E%5BA-Za-z0-9%5Cs%5D%7B4%2C%7D%24"
            }
          },          
          "bonus": [

          ],
          "payments": [

          ],
          "metaTags": [

          ],
          "paymentIcons": [

          ]
        }
      }
    ]
  }
}

这就是我的追求:

    {
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "partner-settings",
        "_type": "_doc",
        "_id": "1en",
        "_score": 1.0,
        "_source": {
          "partnerId": "1",
          "lang": "en",
          "info": "Information about site exclusion",
          "options": {
            "60": "60 minutes",
            "1w": "1 week",
            "5y": "5 years"
          }
        }
      },
      {
        "_index": "partner-settings",
        "_type": "_doc",
        "_id": "1am",
        "_score": 1.0,
        "_source": {
          "partnerId": "1",
          "lang": "am",
          "info": "Information about site exclusion am",
          "options": {
            "60": "60 րոպե",
            "1w": "1 շաբաթ",
            "5y": "5 տարի"
          }
        }
      },
      {
        "_index": "partner-settings",
        "_type": "_doc",
        "_id": "1ru",
        "_score": 1.0,
        "_source": {
          "partnerId": "1",
          "lang": "ru",
          "info": "Information about site exclusion ru",
          "options": {
            "60": "60 минут",
            "1w": "1 неделя",
            "5y": "5 года"
          }
        }
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

您需要更改所使用的id的值,即$request['site_id']

如果您没有下一个具有相同ID的文档,则会覆盖具有该ID的现有文档。

更新:

实际上,您遇到的问题是您没有使用正确的操作。由于您不是在重新索引整个文档,而只是在重新索引整个文档,因此您不应该使用index操作,而应该使用update one。因此,您可以这样做:

$params = [
            'index' => 'my-index',
            'type' => '_doc',
            'id' => $request['site_id']
];

$params['body'] = [
        'doc_as_upsert' => true,
        'doc' => [
                'partnerId' => $request['site_id'],
                'lang' => $request['lang'],
                'info' => $request['info'],
                'options' => $request['options']
        ]
]

$client = ClientBuilder::create()->build();
$client->update($params);