0

我对弹性搜索很陌生,也许你可以帮助我:

所以,我有 symfony 和 Elasticsearch (FOSElasticaBundle)。

如果我在标题中搜索一个完整的单词 - 一切正常(例如,如果我输入“hello”,我将得到“hello”world 所在的结果)。

但问题是,我也希望能够搜索单词的一部分,例如,如果我输入“hel”,我想得到“hel”是单词一部分的结果。

我的 config.yml

fos_elastica:
    clients:
        default: { host: localhost, port: 9200 }
    indexes:
        app:
            client: default
            settings:
                index:
                    analysis:
                        analyzer:
                            custom_search_analyzer:
                                type: custom
                                tokenizer: standard
                                filter   : [standard, lowercase, asciifolding]
                            custom_index_analyzer:
                                type: custom
                                tokenizer: standard
                                filter   : [standard, lowercase, asciifolding, custom_filter]
                        filter:
                            custom_filter:
                                type: edgeNGram
                                side: front
                                min_gram: 1
                                max_gram: 20
            types:
                book:
                    mappings:
                        Author:
                        isbn:
                        title: { analyzer: custom_search_analyzer, analyzer: custom_index_analyzer, filter: custom_filter, type: string }
                    persistence:
                        driver: orm
                        model: VienasVienas\Bundle\BooksBundle\Entity\Book
                        provider:
                        listener:
                        finder:

我的php函数

public function indexAction(Request $request)
{
    $finder = $this->get('fos_elastica.finder.app.book');
    $searchTerm = $request->query->get('q');

    $searchQuery = new \Elastica\Query\QueryString();
    $searchQuery->setParam('query', $searchTerm);
    $searchQuery->setDefaultOperator('AND');

    $books = $finder->find($searchQuery);
    return array(
        'entities' => $books
    );
}

任何人都可以帮忙吗?

4

1 回答 1

1

所以我只是忘记为字段添加 setParam ......

    $searchQuery->setParam('fields', array(
        'Author',
        'title',
        'isbn',
    ));

现在一切正常!

于 2015-05-09T09:53:09.357 回答