elasticsearch 用相关项填充Elasticsearch时出错,elasticsearch,laravel-5,elasticsearch,Laravel 5" /> elasticsearch 用相关项填充Elasticsearch时出错,elasticsearch,laravel-5,elasticsearch,Laravel 5" />

elasticsearch 用相关项填充Elasticsearch时出错

elasticsearch 用相关项填充Elasticsearch时出错,elasticsearch,laravel-5,elasticsearch,Laravel 5,在我的laravel 5.7应用程序中,我使用Elasticsearch,并使用批量功能用相关投票项目填充我的投票 问题是,我无法添加相关投票项目的数组,因为我得到错误: {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"object mapping for [vote_items] tried to parse field [null] as object, but found a concrete val

在我的laravel 5.7应用程序中,我使用Elasticsearch,并使用批量功能用相关投票项目填充我的投票

问题是,我无法添加相关投票项目的数组,因为我得到错误:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"object mapping for [vote_items] tried to parse field [null] as object, but found a concrete value"}],"type":"mapper_parsing_exception","reason":"object mapping for [vote_items] tried to parse field [null] as object, but found a concrete value"},"status":400}
at方法(错误行取消注释并标记):

如果我取消注释行:

//                    $relatedVoteItemsList[]= [ 'vote_item_name' => $nextVoteItem->name ];   // THIS LINE DOES NOT RAISE ERROR!
上面的注释2行,然后批量工作正常,但我的搜索条件不适用于投票项目

$elasticQuery = [
    "bool" => [
        'must' => [
            [
                "multi_match" => [
                    "query"  => $text,
                    "type"   => "cross_fields",
                    "fields" => [
                        "name^4",
                        "description",
                        "vote_items^2"
                    ]
                ],
            ],
        ],
    ]
];
我不知道哪种语法是有效的

更新#2: 查看提供的文档链接,我发现:

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "user": {
          "type": "nested" 
        }
      }
    }
  }
}
我想在保存数据时,我必须指出一些数据是嵌套的

我将批量功能重新制作为:

    foreach ($Votes as $nextVote) {

        if ($nextVote->status!= 'A') continue;   // only active votes must be saved in elasticsearch

        $voteCategory= $nextVote->voteCategory;
        if (empty($voteCategory)) continue;     // only votes with valid category must be saved in elasticsearch
        if ( !$voteCategory->active ) continue; // only votes with active category must be saved in elasticsearch

        $voteItems = VoteItem
            ::getByVote($nextVote->id)
            ->orderBy('ordering', 'asc')
            ->get();
        $relatedVoteItemsList= [];
        foreach ($voteItems as $nextVoteItem) {
            $relatedVoteItemsList[]= [ 'vote_item_name' => $nextVoteItem->name ]; // VALID STRUCTURE ?
        }

        $elastic->index([
            'index' => $elasticsearch_root_index,
            'type'  => $elasticsearch_type,
            'id'    => $nextVote->id,
            'body'  => [
                'id'          => $nextVote->id,
                'slug'        => $nextVote->slug,
                'name'        => $nextVote->name,
                'description' => $nextVote->description,
                'created_at'  => $nextVote->created_at,
                'vote_items'  => $relatedVoteItemsList,
                'category_id' => $voteCategory->id,
                'category'    => [
                    'name'         => $voteCategory->name,
                    'slug'         => $voteCategory->slug,
                    'created_at'   => $voteCategory->created_at,
                ],
            ]
        ]);
但看看地图:

http://localhost:9200/_mapping :
"select_vote": {
"mappings": {
"vote": {
"properties": {
"category": {
"properties": {
"created_at": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"slug": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"category_id": {
"type": "long"
},
"created_at": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"creator_id": {
"type": "long"
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"id": {
"type": "long"
},
"image": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"is_homepage": {
"type": "long"
},
"is_quiz": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ordering": {
"type": "long"
},
"slug": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"status": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"vote_category_id": {
"type": "long"
},
"vote_items": {
"properties": {
"is_correct": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"vote_item_id": {
"type": "long"
},
"vote_item_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"vote_items": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
}
我看不到标记为嵌套的投票项目,但查看示例,我支持它必须?。 哪一种是正确的方式来写入将投票项目标记为嵌套的数据


谢谢

似乎
投票项目
是嵌套类型字段。大概是这样的:

{
  "vote_items" : {
     "type": "nested",
     "properties": {
        "vote_item_name": {
           "type": "keyword"
        }
     }
  }
}
这就是以下工作的原因:

$relatedVoteItemsList[]=['vote\u item\u name'=>$nextVoteItem->name]

要查询嵌套字段,需要使用以下语法:

{
  "query": {
    "nested": {
      "path": "vote_items",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "vote_items.vote_item_name": "xyz"
              }
            }
          ]
        }
      }
    }
  }
}
请注意,
嵌套的
块具有
路径
(提到要查询的嵌套类型字段),
查询
(要在嵌套对象上运行的查询)。还请注意,字段名应为完全限定名,即
,在本例中为
投票项目。投票项目名称

注意:上面的查询是如何使用索引映射的字段查询嵌套字段的示例。请根据您的需要进行修改


有关如何查询的详细信息,请参见。

似乎
vote\u items
是嵌套类型字段。大概是这样的:

{
  "vote_items" : {
     "type": "nested",
     "properties": {
        "vote_item_name": {
           "type": "keyword"
        }
     }
  }
}
这就是以下工作的原因:

$relatedVoteItemsList[]=['vote\u item\u name'=>$nextVoteItem->name]

要查询嵌套字段,需要使用以下语法:

{
  "query": {
    "nested": {
      "path": "vote_items",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "vote_items.vote_item_name": "xyz"
              }
            }
          ]
        }
      }
    }
  }
}
请注意,
嵌套的
块具有
路径
(提到要查询的嵌套类型字段),
查询
(要在嵌套对象上运行的查询)。还请注意,字段名应为完全限定名,即
,在本例中为
投票项目。投票项目名称

注意:上面的查询是如何使用索引映射的字段查询嵌套字段的示例。请根据您的需要进行修改

有关如何查询的详细信息,请参阅。

您是否看到此插件? 它有索引和映射示例,如:

protected $mappingProperties = array(
   'title' => array(
        'type' => 'string',
        'analyzer' => 'standard'
    )
);
如果要根据映射属性设置模型的类型映射,可以使用:

Book::putMapping($ignoreConflicts=true)

你看到这个插件了吗? 它有索引和映射示例,如:

protected $mappingProperties = array(
   'title' => array(
        'type' => 'string',
        'analyzer' => 'standard'
    )
);
如果要根据映射属性设置模型的类型映射,可以使用:


Book::putMapping($ignoreConflicts=true)

感谢您的反馈!请查看更新的#2:查看映射很明显,映射是由elasticsearch根据输入数据格式动态创建的,即。默认情况下,如果字段包含json对象,它将映射到
对象
数据类型,而不是
嵌套
。如果您希望一个字段是嵌套的数据类型,那么在索引数据之前,您必须明确定义字段和数据类型。感谢您的反馈!当我使用“cviebrock/laravel elasticsearch”时,你能给我一个如何在我的laravel 5应用程序中创建映射的提示吗?“^3.2”我在网上搜索并找到了一些类似的文章,但我不确定如何使用cviebrock/laravel elasticsearch库创建映射?抱歉,我不知道该库的概念,但映射创建是一次性的过程。即使将来需要添加新字段,也可能不会太频繁。要创建映射,您只需使用所需的json从应用程序到elastic集群进行REST调用即可。感谢您的反馈!请查看更新的#2:查看映射很明显,映射是由elasticsearch根据输入数据格式动态创建的,即。默认情况下,如果字段包含json对象,它将映射到
对象
数据类型,而不是
嵌套
。如果您希望一个字段是嵌套的数据类型,那么在索引数据之前,您必须明确定义字段和数据类型。感谢您的反馈!当我使用“cviebrock/laravel elasticsearch”时,你能给我一个如何在我的laravel 5应用程序中创建映射的提示吗?“^3.2”我在网上搜索并找到了一些类似的文章,但我不确定如何使用cviebrock/laravel elasticsearch库创建映射?抱歉,我不知道该库的概念,但映射创建是一次性的过程。即使将来需要添加新字段,也可能不会太频繁。要创建映射,只需使用所需的json从应用程序调用REST到elastic集群。