Elasticsearch查找输入词和所有同义词

时间:2016-09-02 13:47:11

标签: php laravel elasticsearch

使用elasticsearch我尝试按单词"滑雪"。

找到所有项目

我的映射(PHP数组):

"properties" => [
    "title" => [
        "type" => "string",
        "boost" => 1.0,
        "analyzer" => "autocomplete"
    ]
]

设定:

"settings"=> [
    "analysis" => [
        "analyzer" => [
            "autocomplete" => [
                "type" => "custom",
                "tokenizer" => "standard",
                "filter" => ["lowercase", "trim", "synonym", "porter_stem"],
                "char_filter" => ["html_strip"]
            ]
        ],
        "filter" => [
            "synonym" => [
                "type" => "synonym",
                "synonyms_path" => "analysis/synonyms.txt"
            ]
        ]
    ]
]

搜索查询:

[
    "index" => "articles",
    "body" =>  [
        "query" =>  [
            "filtered" =>  [
                "query" =>  [
                    "bool" =>  [
                        "must" =>  [
                            "indices" =>  [
                                "indices" =>  ["articles"],
                                "query" =>  [
                                    "bool" =>  [
                                        "should" =>  [
                                            "multi_match" =>  [
                                                "query" => "skiing",
                                                "fields" => ["title"]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ],
        "sort" =>  [
            "_score" =>  [
                "order" => "desc"
            ]
        ]
    ],
    "size" => 10,
    "from" => 0,
    "search_type" => "dfs_query_then_fetch",
    "explain" => true
];

在sysnonyms.txt中有ski =>黄原。

我希望得到所有项目"滑雪" (因为它是输入词)," ski" (通过 porter_stem tokenizer)然后" xanthic" (通过同义词文件)。但只能用#34; xanthic"。

来获得结果

拜托,告诉我为什么?我如何配置索引?

2 个答案:

答案 0 :(得分:1)

在同义词文件中,你需要有" ski,xanthic"。你现在拥有它的方式就是用xanthic取代滑雪,但你想保留两者。我认为您需要重新索引数据以查看更改。

答案 1 :(得分:0)

Thanx,但这是decision。我改变了映射:

"properties" => [
    "title" => [
        "type" => "string",
        "boost" => 1.5,
        "analyzer" => "standard",
        "fields" => [
            "english" => [
                "type" => "string",
                "analyzer" => "standard",
                "search_analyzer" => "english",
                "boost" => 1.0
            ],
            "synonym" => [
                "type" => "string",
                "analyzer" => "standard",
                "search_analyzer" => "synonym",
                "boost" => 0.5
            ]
        ]
    ]
]

设定:

"settings"=> [
    "analysis" => [
        "analyzer" => [
            "synonym" => [
                "type" => "custom",
                "tokenizer" => "standard",
                "filter" => ["lowercase", "trim", "synonym"],
                "char_filter" => ["html_strip"]
            ]
        ],
        "filter" => [
            "synonym" => [
                "type" => "synonym",
                "synonyms_path" => "analysis/synonyms.txt"
            ]
        ]
    ]
]