代码之家  ›  专栏  ›  技术社区  ›  JohnBigs

创建弹性搜索查询

  •  0
  • JohnBigs  · 技术社区  · 6 年前

    我的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)
    

    我在创建查询时遇到问题 personAddress additionalAddresses ... 有人能给我指个方向吗。。?谢谢!

    当前我的查询如下:

    {
      "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
                  }
                }
              }
            }
          }
        }
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Val    6 年前

    你可能需要 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"
              }
            }
          ]
        }
      }
    }