Elastic search 2.0 类似查询的搜索 [英] Elastic search 2.0 search like query
问题描述
这是我的模型:
[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.
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)
);
这可以毫无问题地搜索对象.但我需要输入 searchText 的确切单词才能得到结果.
This searches the object without any issue. But I need to enter the exact word for searchText to get the result.
例如:名称-科学与技术"
As an example: name - "Science and technology"
如果我用技术搜索,它会返回记录.但是如果我用 techno 搜索,它没有返回记录.我该如何解决这个问题?
If I search with technology, It returns the record. But If I search with techno, It did not return the record. How can I fix this issue?
推荐答案
您可以添加 match_phrase_prefix 查询您的字段.
You can add match_phrase_prefix query on on your field.
Match_phrase_prefix 将获取搜索查询中的最后一个标记并对其进行短语前缀匹配.令牌的顺序很重要.如果要搜索文本中的任何位置,则需要创建 n-grams 和 edge_grams of tokens
Match_phrase_prefix will take last token in your searchquery and do a phrase prefix match on it. 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
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
假设有一个字段为description"的文档:science and technology"这段文字被分成单独的标记 ["science" ,"and","technology"] 并在倒排索引中串起来.
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.
如果要查找科技",可以使用匹配查询.因为单词的匹配顺序无关紧要,所以当你搜索技术科学"时你也会得到文档.它只是匹配令牌.
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.
如果订单对您很重要,则使用 match_phrase "science and technology" only 将返回文档.
If order matters for you then use match_phrase "science and technology" only will return the document.
如果您想搜索部分句子science and techno",请使用 match_phrase_prefix .前缀匹配仅在最后一个标记上执行,因此您无法搜索scie and techno".为此,还有其他选项,如 edge-ngrams 和 ngrams
If you want to search for partial sentence "science and techno" then use match_phrase_prefix . Prefix match is only performed on last token so you cannot search for "scie and techno". For this there are other options like edge-ngrams and ngrams
这篇关于Elastic search 2.0 类似查询的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!