1

我正在尝试创建一个在弹性搜索索引上运行模糊搜索的函数。如果我完全按照索引中的拼写指定术语,我只会得到匹配项。如果我故意拼错该术语中的一个字母,例如

“博克”

,我想模糊搜索仍应返回相同的匹配项,但它不返回任何匹配项。同样,如果我用prefixQuery 或termQuery 替换fuzzyMatch,搜索只会在给出准确拼写的情况下返回结果

“鲍勃”

为什么是这样?我该如何解决?哪里有解释这些方法的文档?

这是我的代码...

public void searchResults(@PathParam("index_name") String index_name) throws IOException {
    RestHighLevelClient client = createHighLevelRestClient();
    int numberOfSearchHitsToReturn = 100; // defaults to 10
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.fuzzyQuery("firstname", "Bob"));
    sourceBuilder.from(0);
    sourceBuilder.size(numberOfSearchHitsToReturn);
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    SearchRequest searchRequest = new SearchRequest(index_name).source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    System.out.print(searchResponse);
    client.close();
}

这是 Postman 中 Get /index/_search 的结果...

{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "contacts",
                "_type": "_doc",
                "_id": "J1NDonABNQ4iHt4UOM4u",
                "_score": 1.0,
                "_source": {}
            },
            {
                "_index": "contacts",
                "_type": "_doc",
                "_id": "153",
                "_score": 1.0,
                "_source": {
                    "firstname": "Bob",
                    "home_city": "San Diego",
                    "home_address": "1029 Loring Street",
                    "home_zip": "92109",
                    "contact_id": "153",
                    "email": "bsmith@gmail.com",
                    "lastname": "Smith",
                    "home_state": "California",
                    "cell_phone": "6192542981"
                }
            },
            {
                "_index": "contacts",
                "_type": "_doc",
                "_id": "154",
                "_score": 1.0,
                "_source": {
                    "firstname": "Alice",
                    "home_city": "Paia",
                    "home_address": "581 Pili Loko Street",
                    "home_zip": "00012",
                    "contact_id": "154",
                    "email": "aHernes@gmail.com",
                    "lastname": "Hernes",
                    "home_state": "Hawaii",
                    "cell_phone": "8083829103"
                }
            }
        ]
    }
}
4

1 回答 1

0

我相信弹性会让你有点困惑。

3 个字母术语的 Fuzzines 为 1,因此您期望“Bob”返回是足够公平的。但是,我假设您使用默认过滤器“小写”的标准分析器。

因此计算出的“Boc”和“bob”之间的 Levenshtein 距离为2,这就是它不返回的原因。

试试小写输入词,我打赌“Bob”会被返回。

// no results
{
    "query": {
       "fuzzy" : { "firstname" : "Boc" }
    }
}
// "Bob" returned
{
    "query": {
       "fuzzy" : { "firstname" : "boc" }
    }
}

这有意义吗?

关于您的代码:

public void searchResults(@PathParam("index_name") String index_name) throws IOException {
    RestHighLevelClient client = createHighLevelRestClient();
    int numberOfSearchHitsToReturn = 100; // defaults to 10
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    // "Boc".toLowerCase() or simply "boc"
    sourceBuilder.query(QueryBuilders.fuzzyQuery("firstname", "Boc".toLowerCase()));
    sourceBuilder.from(0);
    sourceBuilder.size(numberOfSearchHitsToReturn);
    sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    SearchRequest searchRequest = new SearchRequest(index_name).source(sourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    System.out.print(searchResponse);
    client.close();
}
于 2020-03-04T12:15:46.970 回答