Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
<img src="//i.stack.imgur.com/RUiNP.png" height="16" width="18" alt="" class="sponsor tag img">elasticsearch Elasticsearch子字段查询_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch Elasticsearch子字段查询,elasticsearch,elasticsearch" /> elasticsearch Elasticsearch子字段查询,elasticsearch,elasticsearch" />

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请查看我的更新答案,并让我知道这是否解决了您的问题?如果
地址
嵌套
类型,则您需要使用嵌套查询