java - Elasticsearch 嵌套映射查询不起作用 - JAVA?

标签 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]下的[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

最佳答案

您需要像这样修复您的 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

关于java - Elasticsearch 嵌套映射查询不起作用 - JAVA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41955418/

相关文章:

java - 抛出以指示失败 "sanity check"的最佳内置 JRE 异常是什么?

java - 从 InputStreamReader 创建 JSONObject

java - 如何在 PuTTY 中保存并运行 Java 文件?

elasticsearch - 嵌套键/值对的弹性脚本聚合

elasticsearch - ElasticSearch嵌套NumericRangeQuery使用列表中的最小值进行比较

spring-boot - Elasticsearch 结果与搜索键不匹配

java - Spring Boot 2.5.0 对于 Jackson Kotlin 类支持,请将 "com.fasterxml.jackson.module: jackson-module-kotlin"添加到类路径

java - 如何为 Spring Boot 应用程序配置端口

c# - 如何配置客户端? AWS Elasticsearch 请求 C#

scala - 在 Scala 中是否有针对 Elasticsearch 的搜索词清理器的实现?