elasticsearch Elasticsearch子字段查询
数据:
elasticsearch Elasticsearch子字段查询,
elasticsearch,
elasticsearch,数据: { "name": "John", "surname": "Brown", "email": "johnbrown@gmail.com", "address": [ { "id": "1", "city": "London"
{
"name": "John",
"surname": "Brown",
"email": "johnbrown@gmail.com",
"address": [
{
"id": "1",
"city": "London"
}
]
}
此查询正在运行
GET db
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*Brown* OR *@gmail.com*",
"fields": [
"surname",
"email",
"address.city"
]
}
}
],
"filter": [
{
"bool": {
"filter": [
{
"regexp": {
"name": {
"value": ".*John.*"
}
}
}
]
}
}
]
}
}
}
"query_string": {
"query": "*Brown*",
"fields": [
"surname",
"email",
"address.city"
]
}
但是,当我像这样更改查询字符串时,它不起作用。(不起作用的意思是返回0个结果。)
这个查询也可以运行
GET db
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "*Brown* OR *@gmail.com*",
"fields": [
"surname",
"email",
"address.city"
]
}
}
],
"filter": [
{
"bool": {
"filter": [
{
"regexp": {
"name": {
"value": ".*John.*"
}
}
}
]
}
}
]
}
}
}
"query_string": {
"query": "*Brown*",
"fields": [
"surname",
"email",
"address.city"
]
}
据我所知,它无法在
address.city
中找到值。但是为什么呢?根据下面的评论,OP创建了地址
为嵌套
类型的索引
添加带有索引映射、搜索查询和搜索结果的工作示例
索引映射:
{
"mappings": {
"properties": {
"address": {
"type": "nested"
}
}
}
}
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "*London*",
"fields": [
"surname",
"email"
]
}
},
{
"nested": {
"path": "address",
"query": {
"query_string": {
"query": "*London*",
"fields": [
"address.city"
]
}
}
}
}
],
"minimum_should_match": 1
}
}
}
"hits": [
{
"_index": "64908538",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "John",
"surname": "Brown",
"email": "johnbrown@gmail.com",
"address": [
{
"id": "1",
"city": "London"
}
]
}
}
]
搜索查询:
{
"mappings": {
"properties": {
"address": {
"type": "nested"
}
}
}
}
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "*London*",
"fields": [
"surname",
"email"
]
}
},
{
"nested": {
"path": "address",
"query": {
"query_string": {
"query": "*London*",
"fields": [
"address.city"
]
}
}
}
}
],
"minimum_should_match": 1
}
}
}
"hits": [
{
"_index": "64908538",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "John",
"surname": "Brown",
"email": "johnbrown@gmail.com",
"address": [
{
"id": "1",
"city": "London"
}
]
}
}
]
搜索结果:
{
"mappings": {
"properties": {
"address": {
"type": "nested"
}
}
}
}
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "*London*",
"fields": [
"surname",
"email"
]
}
},
{
"nested": {
"path": "address",
"query": {
"query_string": {
"query": "*London*",
"fields": [
"address.city"
]
}
}
}
}
],
"minimum_should_match": 1
}
}
}
"hits": [
{
"_index": "64908538",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "John",
"surname": "Brown",
"email": "johnbrown@gmail.com",
"address": [
{
"id": "1",
"city": "London"
}
]
}
}
]
我应该为“地址”数据建立索引吗?你做过这样的交易吗@Bhavya@MuhammetCanTONBUL在为文档编制索引之前是否创建了索引映射?是的,它是嵌套索引的。我的索引:
“地址”:{“类型”:“嵌套”,“属性”:{
,kibana电子商务示例索引:“产品”:{“属性”:{
。这里的类型嵌套错误吗@Bhavya@MuhammetCanTONBUL请查看我的更新答案,并让我知道这是否解决了您的问题?如果地址
为嵌套
类型,则您需要使用嵌套查询