简体   繁体   English

Elastic search 2.0 搜索类似查询

[英]Elastic search 2.0 search like query

This is my model:这是我的 model:

[ElasticsearchType(Name = "projectmodel")]
public class ProjectModel
{          
    public string name { get; set; }
    public string description { get; set; }
    [Nested]
    [JsonProperty("taskmodels")]
    public List<TaskModel> taskmodels { get; set; }
}

public class TaskModel
{       
    public string title { get; set; }
    public string description { get; set; }
}

I use the following code for search inside the main object and nested object.我使用以下代码在主 object 和嵌套 object 内进行搜索。

        var searchResults = client.Search<ProjectModel>(
            body => body.Query(
                query => query.Bool(
                    bq => bq.Should(
                        q => q.Match(p => p.Field(f => f.name).Boost(6).Query(keyword)),                            
                        q => q.Match(p => p.Field(f => f.description).Boost(6).Query(keyword)),
                            sh => sh.Nested(n => n.Path(p => p.taskmodels).Query(nq => nq.Match(
                                m => m.Query(keyword).Field("taskmodels.description")

                                )
                            )
                            ),
                            sh => sh.Nested(n => n.Path(p => p.taskmodels).Query(nq => nq.Match(
                                m => m.Query(keyword).Field("taskmodels.title")

                                )
                            )
                            )
                        )

                    )
                ).Size(MAX_RESULT)
            );

This searches the object without any issue.这会毫无问题地搜索 object。 But I need to enter the exact word for searchText to get the result.但我需要为 searchText 输入确切的单词才能得到结果。

As an example: name - "Science and technology"举个例子:名字——“科技”

If I search with technology, It returns the record.如果我用技术搜索,它会返回记录。 But If I search with techno, It did not return the record.但是如果我用 techno 搜索,它没有返回记录。 How can I fix this issue?我该如何解决这个问题?

You can add match_phrase_prefix query on on your field.您可以在您的字段上添加match_phrase_prefix查询。

Match_phrase_prefix will take last token in your searchquery and do a phrase prefix match on it. Match_phrase_prefix 将在您的搜索查询中获取最后一个标记并对其进行短语前缀匹配。 Order of tokens is important.令牌的顺序很重要。 If you want to search anywhere in the text, then you will need to create n-grams and edge_grams of tokens如果要搜索文本中的任何位置,则需要创建n-grams和 edge_grams of tokens

var searchResults = _elasticClient.Search<ProjectModel>(
            body => body.Query(
                query => query.Bool(
                    bq => bq.Should(
                        q=> q.MatchPhrasePrefix(p=>p.Field(f=>f.name).Query(keyword)) --> note
                        q => q.Match(p => p.Field(f => f.name).Boost(6).Query(keyword)),
                        q => q.MatchPhrasePrefix(p => p.Field(f => f.description).Boost(6).Query(keyword)),
                            sh => sh.Nested(n => n.Path(p => p.taskmodels).Query(nq => nq.Match(
                                m => m.Query(keyword).Field("taskmodels.description")

                                )
                            )
                            ),
                            sh => sh.Nested(n => n.Path(p => p.taskmodels).Query(nq => nq.Match(
                                m => m.Query(keyword).Field("taskmodels.title")

                                )
                            )
                            )
                        )

                    )
                ).Size(MAX_RESULT)
            );

Different between match, matchphrase and matchphraseprefix match、matchphrase 和 matchphraseprefix 之间的区别

Suppose there is a document with field "description": "science and technology" This text is broken in separate tokens ["science","and","technology"] and stred in inverted index.假设有一个文档的字段为“description”:“science and technology”,这个文本被分成单独的标记[“science”,“and”,“technology”],并以倒排索引排列。

If you want to look for "science technology", you can use match query.如果要查找“科技”,可以使用匹配查询。 For match order of words don't matter, so you will also get document when you search for "technology science".由于单词的匹配顺序无关紧要,因此您在搜索“技术科学”时也会得到文档。 It just matches tokens.它只匹配令牌。

If order matters for you then use match_phrase "science and technology" only will return the document.如果订单对您很重要,则仅使用 match_phrase "science and technology" 将返回文档。

If you want to search for partial sentence "science and techno" then use match_phrase_prefix.如果要搜索部分句子“科学和技术”,请使用 match_phrase_prefix。 Prefix match is only performed on last token so you cannot search for "scie and techno".前缀匹配仅在最后一个标记上执行,因此您无法搜索“scie and techno”。 For this there are other options like edge-ngrams and ngrams为此,还有其他选项,例如 edge-ngrams 和 ngrams

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM