创建弹性搜索查询

creating elastic search query

我的 elasticsearch 中有这个 json 文档:

{
  "personId": "5b564b6a0c000b622a55",
  "name": "Jake Harper",
  "country": "US",
  "socialSecurityNumber": 7634904,
  "personAddress": {
    "city": "Los Angeles",
    "street": "Sunset BLVD",
    "streetNumber": 149,
  },
  "additionalAddresses": [
    {
      "addressType": "office",
      "additionalAddress": {
        "city": "Santa Monica",
        "street": "3rd street",
        "streetNumber": 13
      }
    },
    {
      "addressType": "property",
      "additionalAddress": {
        "city": "mxkwUcc branch city",
        "street": "mxkwUcc BLVD",
        "streetNumber": 255
      }
    }
  ]
}

我想创建一个弹性查询来帮助我通过以下方式找到人:

personId
socialSecurityNumber
personAddress(all fields)
additionalAddresses(all fields in th array docs)

我在使用 personAddressadditionalAddresses 创建查询时遇到了问题... 谁能在这里给我一些指导..?谢谢!

目前我的查询看起来像:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "personId": "5b564b6a0c000b622a"
          }
        },
        {
          "match": {
            "name": "Harper"
          }
        }
      ]
    }
  }
}

我正在使用多个查询,因为我会得到一个术语输入,我想检查它是否属于上述任何字段。

我的映射:

{
  "peopledb": {
    "mappings": {
      "person": {
        "properties": {
          "additionalAddresses": {
            "properties": {
              "additionalAddress": {
                "properties": {
                  "city": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "street": {
                    "type": "text",
                    "fields": {
                      "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                      }
                    }
                  },
                  "streetNumber": {
                    "type": "long"
                  },
                  "zipCode": {
                    "type": "long"
                  }
                }
              },
              "addressType": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "country": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "personAddress": {
            "properties": {
              "city": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "street": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "streetNumber": {
                "type": "long"
              },
              "zipCode": {
                "type": "long"
              }
            }
          },
          "personId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

您可能需要使 additionalAddresses 类型嵌套,但首先让我们看看 multi_match 是否能让您更进一步:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "personId": "5b564b6a0c000b622a"
          }
        },
        {
          "match": {
            "name": "Harper"
          }
        },
        {
          "match": {
            "personAddress.city": "Los"
          }
        },
        {
          "multi_match": {
            "fields": ["additionalAddresses.additionalAddress.city", "additionalAddresses.additionalAddress.street", "additionalAddresses.additionalAddress.streetNumber"],
            "query": "123 Main Street"
          }
        }
      ]
    }
  }
}