如何使用日光浴室查询创建字符串类型的 solr 文档

How to create solr document with String type using solarium query

我在 php 中通过日光浴室插件创建 solr 文档。

但是所有文档都存储 text_general 数据类型,除了 id 字段。 text_general 是 solr 系统中的默认数据类型。

我的疑问是为什么id字段默认只存储字符串类型。

并且如果有可能使用日光浴室插件添加 string 类型的文档。

我的代码部分在这里,

        public function updateQuery() {
            $update = $this->client2->createUpdate();

            // create a new document for the data
            $doc1 = $update->createDocument();
            // $doc1->id = 123;
            $doc1->name = 'value123';
            $doc1->price = 364;

            // and a second one
            $doc2 = $update->createDocument();
            // $doc2->id = 124;
            $doc2->name = 'value124';
            $doc2->price = 340;

            // add the documents and a commit command to the update query
            $update->addDocuments(array($doc1, $doc2));
            $update->addCommit();

            // this executes the query and returns the result
            $result = $this->client2->update($update);

            echo '<b>Update query executed</b><br/>';
            echo 'Query status: ' . $result->getStatus(). '<br/>';
            echo 'Query time: ' . $result->getQueryTime();
        }

上面代码的结果文档在这里,

{
  "responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "q":"*:*",
      "_":"1562736411330"}},
  "response":{"numFound":2,"start":0,"docs":[
      {
        "name":["value123"],
        "price":[364],
        "id":"873dfec0-4f9b-4d16-9579-a4d5be8fee85",
        "_version_":1638647891775979520},
      {
        "name":["value124"],
        "price":[340],
        "id":"7228e92d-5ee6-4a09-bf12-78e24bdfa52a",
        "_version_":1638647892102086656}]
  }}

这取决于您的 Solr 安装的字段类型 defined in the schema。它与您如何通过 Solarium 发送数据没有任何关系。

在无模式模式下,id 字段始终设置为字符串,因为无法对唯一字段进行标记化(好吧,它可以,但它会很奇怪,non-obvious 错误)。

在您的情况下,我建议将 price 字段定义为 integer/long 字段(如果它一直是整数),将 name 字段定义为字符串字段。请注意,string 字段只会在完全匹配的情况下生成匹配项,因此在您的情况下,您必须搜索 value124 并使用精确的大小写才能获得匹配项。

您还可以在显式定义字段时调整字段的multiValued 属性。这样你就只会得到 JSON 结构中的字符串,而不是包含字符串的数组。