首页 文章

弹性查询嵌套查询

提问于
浏览
0

步骤1:

使用下面的mapping.json在弹性搜索http://localhost:9200/shop上创建了一个索引

{
  "cloth" : 
  {
      "properties" : 
      {
          "name" : { "type" : "string", "index" : "analyzed" },
          "variation" : 
          {
            "type" : "nested", 
            "properties" : 
            { 
                "size" : 
                { 
                    "type" : "string", "index" : "not_analyzed"
                },
                "color" : 
                {
                    "type" : "string", "index" : "not_analyzed"
                }
            }
        }
    }
  }
}

GET:http://localhost:9200/shop/_mapping/cloth

HTTP / 1.1 200 OK Content-Type:application / json; charset = UTF-8内容长度:518

{ “店”:{ “映射”:{ “布”:{ “属性”:{ “布”:{ “属性”:{ “属性”:{ “属性”:{ “名称”:{ “属性”: { “索引”:{ “类型”: “串”}, “类型”:{ “类型”: “串”}}}, “变异”:{ “属性”:{ “属性”:{ “属性”: { “颜色”:{ “属性”:{ “索引”:{ “类型”: “串”}, “类型”:{ “类型”: “串”}}}, “尺寸”:{ “属性”: { “索引”:{ “类型”: “串”}, “类型”:{ “类型”: “串”}}}}}, “类型”:{ “类型”: “串”}}}}} }}, “姓名”:{ “类型”: “串”}, “变异”:{ “属性”:{ “颜色”:{ “类型”: “串”}, “尺寸”:{ “类型”: “串”}}}}}}}}

第2步:

用以下给出的数据插入数据.json http://localhost:9200/shop/cloth/?_create

{
"name" : "Test shirt",
"variation" : [
{ "size" : "XXL", "color" : "red" },
{ "size" : "XL", "color" : "black" }
]
}

第3步:

尝试使用给定的query.json进行搜索

http://localhost:9200/shop/cloth/_search

{
"query" : {
"nested" : {
"path" : "variation",
"query" : {
"bool" : {
"must" : [
{ "term" : { "variation.size" : "XXL" } },
{ "term" : { "variation.color" : "black" } }
]
}
}
}
}
}

遵循以下错误

HTTP / 1.1 400错误请求内容类型:application / json; charset = UTF-8内容长度:519

{"error":{"root_cause":[{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"shop","node":"6U9SA_SDRJKfw1bRxwH8ig","reason":{"type":"query_parsing_exception","reason":"[nested] nested object under path [variation] is not of nested type","index":"shop","line":4,"col":1}}]},"status":400}

嵌套查询的搜索方式是什么?有没有适当的方法将映射文件加载到搜索集群?

2 回答

  • 1

    我认为您没有使用 cloth 映射正确创建索引 . 这样做:

    # delete your index first
    curl -XDELETE localhost:9200/shop
    
    # create it properly
    curl -XPUT localhost:9200/shop -d '{
      "mappings": {
        "cloth": {
          "properties": {
            "name": {
              "type": "string",
              "index": "analyzed"
            },
            "variation": {
              "type": "nested",
              "properties": {
                "size": {
                  "type": "string",
                  "index": "not_analyzed"
                },
                "color": {
                  "type": "string",
                  "index": "not_analyzed"
                }
              }
            }
          }
        }
      }
    }'
    
  • 0
    {
        "query" : {
            "nested" : {
                "path" : "cloth.variation",
                "query" : {
                    "bool" : {
                        "must" : [
                            { "term" : { "cloth.variation.size" : "XXL" } },
                            { "term" : { "cloth.variation.color" : "black" } }
                        ]
                    }
                }
            }
        }
    }
    

相关问题