Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel elastic搜索不提供包含或类似匹配_Php_Laravel_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Laravel 5_Laravel Scout - Fatal编程技术网 elasticsearch,laravel-5,laravel-scout,Php,Laravel,elasticsearch,Laravel 5,Laravel Scout" /> elasticsearch,laravel-5,laravel-scout,Php,Laravel,elasticsearch,Laravel 5,Laravel Scout" />

Php Laravel elastic搜索不提供包含或类似匹配

Php Laravel elastic搜索不提供包含或类似匹配,php,laravel,elasticsearch,laravel-5,laravel-scout,Php,Laravel,elasticsearch,Laravel 5,Laravel Scout,我想为我所有的laravel搜索查询实现elasticsearch。我使用brew安装了最新的Laravel和最新的elasticsearch curlhttp://localhost:9200/ { "name" : "_SFvSGk", "cluster_name" : "elasticsearch_an398690", "cluster_uuid" : "xBi3aTDaTkmA6dtzhpOrwg", "version" : { "number" : "6.5.4

我想为我所有的laravel搜索查询实现elasticsearch。我使用brew安装了最新的Laravel和最新的elasticsearch

curlhttp://localhost:9200/

{
  "name" : "_SFvSGk",
  "cluster_name" : "elasticsearch_an398690",
  "cluster_uuid" : "xBi3aTDaTkmA6dtzhpOrwg",
  "version" : {
    "number" : "6.5.4",
    "build_flavor" : "oss",
    "build_type" : "tar",
    "build_hash" : "d2ef93d",
    "build_date" : "2018-12-17T21:17:40.758843Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}
这里我使用的是
babenkoivan/scout elasticsearch驱动程序

型号

namespace App;

use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    use Searchable;

    /**
     * @var string
     */
    protected $indexConfigurator = CustomerIndexConfigurator::class;

    /**
     * @var array
     */
    protected $searchRules = [
        CustomerSearchRule::class
    ];

    /**
     * @var array
     */
    protected $mapping = [
        'properties' => [
            'text' => [
                'type' => 'text',
                'fields' => [
                    'ref_num' => [
                        'type' => 'keyword',
                    ]
                ]
            ],
        ]
    ];
}
namespace App;

use ScoutElastic\SearchRule;

class CustomerSearchRule extends SearchRule
{
    /**
     * @inheritdoc
     */
    public function buildHighlightPayload()
    {
        return [
            'fields' => [
                'ref_num' => [
                    'type' => 'plain'
                ]
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function buildQueryPayload()
    {
        $query = $this->builder->query;

        return [
                [
                    'match' => [
                        'ref_num' => [
                            'query' => $query,
                            'boost' => 2
                        ]
                    ]
                ]
        ];
    }
}
namespace App;

use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;

class CustomerIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    /**
     * @var array
     */
    protected $settings = [
        //
    ];
}
搜索规则

namespace App;

use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    use Searchable;

    /**
     * @var string
     */
    protected $indexConfigurator = CustomerIndexConfigurator::class;

    /**
     * @var array
     */
    protected $searchRules = [
        CustomerSearchRule::class
    ];

    /**
     * @var array
     */
    protected $mapping = [
        'properties' => [
            'text' => [
                'type' => 'text',
                'fields' => [
                    'ref_num' => [
                        'type' => 'keyword',
                    ]
                ]
            ],
        ]
    ];
}
namespace App;

use ScoutElastic\SearchRule;

class CustomerSearchRule extends SearchRule
{
    /**
     * @inheritdoc
     */
    public function buildHighlightPayload()
    {
        return [
            'fields' => [
                'ref_num' => [
                    'type' => 'plain'
                ]
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function buildQueryPayload()
    {
        $query = $this->builder->query;

        return [
                [
                    'match' => [
                        'ref_num' => [
                            'query' => $query,
                            'boost' => 2
                        ]
                    ]
                ]
        ];
    }
}
namespace App;

use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;

class CustomerIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    /**
     * @var array
     */
    protected $settings = [
        //
    ];
}
配置程序

namespace App;

use ScoutElastic\Searchable;
use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    use Searchable;

    /**
     * @var string
     */
    protected $indexConfigurator = CustomerIndexConfigurator::class;

    /**
     * @var array
     */
    protected $searchRules = [
        CustomerSearchRule::class
    ];

    /**
     * @var array
     */
    protected $mapping = [
        'properties' => [
            'text' => [
                'type' => 'text',
                'fields' => [
                    'ref_num' => [
                        'type' => 'keyword',
                    ]
                ]
            ],
        ]
    ];
}
namespace App;

use ScoutElastic\SearchRule;

class CustomerSearchRule extends SearchRule
{
    /**
     * @inheritdoc
     */
    public function buildHighlightPayload()
    {
        return [
            'fields' => [
                'ref_num' => [
                    'type' => 'plain'
                ]
            ]
        ];
    }

    /**
     * @inheritdoc
     */
    public function buildQueryPayload()
    {
        $query = $this->builder->query;

        return [
                [
                    'match' => [
                        'ref_num' => [
                            'query' => $query,
                            'boost' => 2
                        ]
                    ]
                ]
        ];
    }
}
namespace App;

use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;

class CustomerIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    /**
     * @var array
     */
    protected $settings = [
        //
    ];
}
我有一个记录,
ref_num
I50263
。因此,当我搜索
I50
相似的
相似的查询时,应该会得到这个记录。我尝试了下面的所有搜索,但我只得到了完整单词
I50263
的结果

return Customer::search('I50')->get();
// no record
return Customer::search('I50263')->get();
// got record
return Customer::searchRaw([
  'query' => [
     'bool' => [
        'must' => [
            'match' => [
                'ref_num' => 'I502'
            ]
        ]
     ]
  ]
]);
// no record
return Customer::searchRaw([
  'query' => [
      'bool' => [
          'must' => [
             "match_phrase" => [
                "ref_num" => [
                   "query" => "I50",
                   "boost" => 1
                 ]
              ]
           ]
        ]
     ]
 ]);
 // no record

已尝试将字段类型设置为
text

如我所见,您的
ref\u num
字段属于
关键字类型。执行(如
match
match\u phrase
)不会给出任何结果。对于
关键字
-s,您应该使用。也许会对你有帮助

实例 映射 添加示例文档 全文
匹配
搜索
文本
类型字段 按整数值 结果:找到文档

按值的前缀 结果:未找到文档

术语级
前缀
搜索
关键字
类型字段 结果:找到文档

如您所见,在示例中,我使用了这样的子字段(
raw
ref\u num
的子字段,但类型不同)。在Elasticsearch中,它被称为
字段
,您可以在

您可以简单地将查询与任何其他字段上的任何其他查询一起使用

如果您希望在
文本
类型字段上获得相同的结果,则必须正确准备索引。例如,您可以使用自定义分析器,将单词拆分为n-gram标记

默认情况下,任何分析器都不会拆分单词,因此在您的情况下,索引中只有一个标记:

POST /_analyze
{
  "analyzer": "standard",
  "text": "I50263"
}
结果:

{
  "tokens": [
    {
      "token": "i50263",
      "start_offset": 0,
      "end_offset": 6,
      "type": "<ALPHANUM>",
      "position": 0
    }
  ]
}
{
“代币”:[
{
“令牌”:“i50263”,
“起始偏移量”:0,
“端部偏移”:6,
“类型”:“,
“位置”:0
}
]
}

对于全文搜索,Elasticsearch基于索引中的标记。如果标记与搜索词中的标记不匹配,则表示不匹配。

请在回答中详细说明一下?如果我想进行全文搜索,应该是什么类型?如果我想在多个字段(如ref_num、name、email)中匹配查询,该怎么办?尝试了术语查询,但没有成功
curl-X POST“localhost:9200/_search”-H'内容类型:application/json'-d'{“query”:{“term”:{“ref_num”:“I50263”}}}
@devo请再次检查我的答案。我描述了更多。