社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Elasticsearch

ElasticSearch多字段搜索语法错误

mstdmstd • 7 年前 • 2595 次点击  

我看着 ElasticSearch应做、不应做和专业提示 作者:Itamar Syn Hershko

https://www.youtube.com/watch?v=c9O5_a50aOQ

我在下图的几个字段中看到多个条件:

https://imgur.com/a/17zAZ4w

我试着加入 我的Laravel 5.7应用程序(带有ElasticSearch/ElasticSearch插件),如以下代码所示:

$elasticQuery = [
    "bool" => [
        'must'   => [
            'multi_match' => [
                'query'  => $text,
                'fields' => ['name^4', 'description']
            ],
        ],
        "should" => [
            'term' => [
                "category_id" => 1,
            ]
        ]
    ]
];

但我得到了错误:

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}

但当我使用一个简单的条件时:

        $elasticQuery = [
            'multi_match' => [
                'query' => $text,
                'fields' => ['name^4', 'description'],
            ],
        ];

我得到了一个有效的结果:

[hits] => Array
    (
        [total] => 1
        [max_score] => 7.4126062
        [hits] => Array
            (
                [0] => Array
                    (
                        [_index] => select_vote
                        [_type] => vote
                        [_id] => 16
                        [_score] => 7.4126062
                        [_source] => Array
                            (
                                [id] => 16
                                [slug] => in-the-film-babe-what-type-of-animal-was-babe
                                [name] => In the film Babe, what type of animal was Babe?
                                [description] => Babe is a 1995 A...
                                [created_at] => 2018-11-10 09:14:15
                                [category_id] => 2
                                [category] => Array
                                    (
                                        [name] => Movie&Cartoons
                                        [slug] => movie-cartoons
                                        [created_at] => 2018-11-10 09:14:12
                                    )

                            )

                    )

            )

    )

这是多请求的有效格式吗?

修改块2: 做一些搜索,我找到了工作:

$elasticQuery = [
    "bool" => [
        'should' => [


                [
                        "multi_match" => [
                            "query"  => $text,
                            "type"   => "cross_fields",
                            "fields" => [
                                "name^4",
                                "description"
                            ]
                        ]
                ],

                [
                    'match' => [
                        'category_id' => [
                            'query' => 1,
                        ]
                    ]
                ],

                [
                    'match' => [
                        'category_id' => [
                            'query' => 3,
                        ]
                    ]
                ],

        ]
    ]
];

当我需要按文本字段和上面示例中的类别(1和3)数组进行搜索时,它可以工作,但看起来像是“或”条件,但我需要进行类似“和”的限制,使用SQL术语…

哪种方式正确,才能做出“和”这样的限制?

谢谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30458
文章 [ 1 ]  |  最新文章 7 年前