首页 > 解决方案 > elasticsearch7.x 索引孙文档

问题描述

最近在研究elasticsearch 7.x中的祖孙文档。我自己建了一个索引,往里面插入了数据(见下面的代码),但是发现好像查不到数据,是不是我做错了什么?谢谢

映射:</p>

PUT /map
{
  "mappings": {
    "properties": {
      "country": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "countryName": {
            "type": "text"
          }
        }
      },
      "province": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "provinceName": {
            "type": "text"
          }
        }
      },
      "city": {
        "properties": {
          "id": {
            "type": "integer"
          },
          "cityName": {
            "type": "text"
          },
          "attractions": {
            "type": "text"
          }
        }
      },
      "my_join_field": {
        "type": "join",
        "relations": {
          "country": "province",  
          "province": "city" 
        }
      }
    }
  }
}

文件:

PUT map/_doc/1?refresh
{
  "country.id": "1",
  "country.countryName": "US",
  "relationship": {
    "name": "country" 
  }
}

PUT map/_doc/2?refresh
{
  "country.id": "2",
  "country.countryName": "CHINA",
  "relationship": {
    "name": "country" 
  }
}


PUT map/_doc/3?routing=1&refresh 
{
  "province.id": "3",
  "province.provinceName": "losangeles",
  "relationship": {
    "name": "province", 
    "parent": "1" 
  }
}

PUT map/_doc/4?routing=1&refresh 
{
  "province.id": "4",
  "province.provinceName": "chicago",
  "relationship": {
    "name": "province", 
    "parent": "1" 
  }
}

PUT map/_doc/5?routing=2&refresh 
{
  "province.id": "5",
  "province.provinceName": "Anhui",
  "relationship": {
    "name": "province", 
    "parent": "2" 
  }
}
PUT map/_doc/6?routing=2&refresh 
{
  "province.id": "6",
  "province.provinceName": "Shanghai",
  "relationship": {
    "name": "province", 
    "parent": "2" 
  }
}


PUT map/_doc/7?routing=2&refresh 
{
  "city.id": "7",
  "city.cityName": "suzhou",
  "city.attractions":"huangcangyu",
  "relationship": {
    "name": "city", 
    "parent": "5" 
  }
}
PUT map/_doc/8?routing=2&refresh 
{
  "city.id": "8",
  "city.cityName": "maanshan",
  "city.attractions":"caishiji",
  "relationship": {
    "name": "city", 
    "parent": "5" 
  }
}

查询DSL:</p>


GET /map/_search
{
  "query": {
    "has_child": {
      "type": "province",
      "query": {
        "has_child": {
          "type": "city",
          "query": {
            "match": {
              "city.cityName": "maanshan"
            }
          }
        }
      }
    }
  }
}

上面的查询 DSL 没有结果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

标签: elasticsearch

解决方案


推荐阅读