Elasticsearch嵌套映射查询无法正常工作 - JAVA?

时间:2017-01-31 10:37:41

标签: java elasticsearch

我已将我的一个字段设置为嵌套类型。 我按照此文档https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-joining-queries.html#java-query-dsl-nested-query

进行了跟踪

以下是摘录

"price":{  
           "type":"nested",
           "properties":{  
              "activity_price":{  
                 "type":"double"
              },
              "multimedia_price":{  
                 "type":"double"
              },
              "transportation_price":{  
                 "type":"double"
              }
           }
        }

执行查询时

QueryBuilders.nestedQuery("price", QueryBuilders.boolQuery()
        .must(QueryBuilders.matchQuery("price.activity_price", price)),
            ScoreMode.Max);

我得到路径[price]下的[嵌套]嵌套对象不是嵌套类型。

我正在使用Elasticsearch 5.1.2

我创建索引,映射和填充数据的三个文件: - mapping.json

{  
   "settings":{  
      "number_of_shards":1,
      "number_of_replicas":0
   },
   "mappings":{  
      "test_type_table":{  
         "price":{  
            "type":"nested",
            "properties":{  
               "activity_price":{  
                  "type":"double"
               },
               "multimedia_price":{  
                  "type":"double"
               },
               "transportation_price":{  
                  "type":"double"
               }
            }
         }
      }
   }
}

data.json

{ "index" : { "_index" : "test_index", "_type" : "test_type_table", "_id" : "1" } }
{"price": [{"activity_price":"100.00","multimedia_price":"10","transporation_price":"10"}]}

setup.json

curl -XPOST http://localhost:9200/test_index -d @mapping.json
curl -s -XPOST http://localhost:9200/_bulk --data-binary @data.json

1 个答案:

答案 0 :(得分:1)

您需要像这样修复mapping.json文件:

{  
   "settings":{  
      "number_of_shards":1,
      "number_of_replicas":0
   },
   "mappings":{  
      "test_type_table":{  
        "properties": {                  <--- this is missing
         "price":{  
            "type":"nested",
            "properties":{  
               "activity_price":{  
                  "type":"double"
               },
               "multimedia_price":{  
                  "type":"double"
               },
               "transportation_price":{  
                  "type":"double"
               }
            }
         }
        }
      }
   }
}

然后,您可以使用PUT而不是POST

重新创建索引
# first delete your index
curl -XDELETE http://localhost:9200/test_index

# recreate your index using PUT
curl -XPUT http://localhost:9200/test_index -d @mapping.json