创建弹性搜索查询

时间:2018-07-25 12:06:47

标签: elasticsearch elasticsearch-5 elastic4s

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

1 个答案:

答案 0 :(得分:0)

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