【问题标题】:Elasticsearch nested mapping query not working- JAVA?Elasticsearch 嵌套映射查询不起作用 - JAVA?
【发布时间】:2017-06-16 18:17:20
【问题描述】:

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

下面是sn-p

"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] 下的 [nested] 嵌套对象不是嵌套类型。

我使用的是 Elasticsearch 5.1.2

我有三个文件来创建索引、映射和填充数据:- ma​​pping.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

【问题讨论】:

  • 你能展示一下运行curl -XGET localhost:9200/your_index时得到的结果吗?
  • 嘿 Val,请在下面找到 curl 命令的响应:- "mappings":{ "test_type_table":{ "properties":{ "price":{ "properties":{ "activity_price" :{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256 } } }, "multimedia_price":{ "type":"text", “字段”:{ “关键字”:{ “类型”:“关键字”,“ignore_above”:256 } } },“运输价格”:{ “类型”:“文本”,“字段”:{ “关键字”:{ "type":"keyword", "ignore_above":256 } } } } }, "title":{ "type":"text", "fields":{ "keyword":{ "type":"keyword", "ignore_above":256 } } }} }}
  • 你去,如果你仔细看price字段不是nested,那么你在创建索引时一定做错了。您需要擦除它并使用您的映射重新创建它。
  • 嘿 Val,我已经编辑了帖子的内容,并添加了我在创建索引时使用的文件的内容。请看一看。谢谢
  • 在 ES 5 中,为了创建索引,您必须使用 -XPUT 而不是 -XPOST,您的索引可能不是使用您的第一个命令创建的,但是仅在发送第二个批量命令时

标签: java elasticsearch


【解决方案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

【讨论】:

  • 非常感谢瓦尔。那真是个愚蠢的错误。
  • 很高兴它有帮助!
猜你喜欢
  • 2022-01-22
  • 2015-09-09
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 2018-08-20
  • 2014-09-26
  • 1970-01-01
相关资源
最近更新 更多