简体   繁体   English

Nest Client在Elasticsearch中进行相等查询

[英]Equal query in Elasticsearch by Nest client

public class User 
{
    public string Email { get; set; }
}

client.Index(new User { Email ="test@test.te" });

Query in Linq C# for example : 例如,在Linq C#中进行查询:

rep.Where(user=>user.Email=="test@test.te");

That works correctly. 可以正常工作。

I use same query in Nest: 我在Nest中使用相同的查询:

client.Search<Post>(q => q
.Query(qu => qu
.Term(te=>te.OnField("email").Value("test@test.te"))));

Document result count is zero!! 文件结果计数为零!

But : 但是:

client.Search<Post>(q => q
.Query(qu => qu
.Term(te=>te.OnField("email").Value("test"))));

Document result count is 1 - why? 文件结果计数为1-为什么?

How I can make equal-query in ElasticSearch? 如何在ElasticSearch中进行均等查询?

It's all because analyzers. 都是因为分析仪。 Your document is parsed to terms while indexing, which means that elasticsearch stores something like an array of strings ["test", "test", "te"] in row with your document. 您的文档在建立索引时会被解析为术语,这意味着elasticsearch在您的文档行中存储类似于字符串数组["test", "test", "te"]的内容。 Depending on what anayzer is configured (I guess it's standard one), you may get different terms decomposition. 根据分析仪的配置(我猜这是标准的 ),您可能会得到不同的分解术语。 On the other side, your term request is not analyzed; 另一方面,不分析您的任期要求; that's why first request returns nothing - there's no such string as "test@test.te" in index strings ["test", "test", "te"] , but there's a "test" string, so you get results for the second one. 这就是为什么第一个请求什么都不返回的原因-索引字符串["test", "test", "te"]没有“ test@test.te”这样的字符串,但是有一个“ test”字符串,因此您可以获得第二个。 In your case you should use query_string query , but beware of the fact that such queries are analyzed too. 在您的情况下,应使用query_string query ,但请注意,也要分析此类查询。 It means, that if you index two documents, say {"Email":"test@test.te"} and {"Email":"test@gmail.com"} , without any flags query {"query":{"query_string":{"default_field":"Email","query":"test@test.te"}}} will return both documents because both of them contain "test" string in the index. 这意味着,如果您为两个文档建立索引,请说{"Email":"test@test.te"}{"Email":"test@gmail.com"} ,而无需任何标志查询{"query":{"query_string":{"default_field":"Email","query":"test@test.te"}}}将返回两个文档,因为它们都在索引中包含"test"字符串。 To avoid this, use something like {"default_field":"Email","query":"test@test.te", "default_operator":"AND"}}} - default default operator is "OR" . 为了避免这种情况,请使用类似{"default_field":"Email","query":"test@test.te", "default_operator":"AND"}}} -默认默认运算符为"OR"

Speaking of NEST, use query like 说到NEST,请使用类似的查询

client.Search<Post>(q => q
    .Query(qu => qu
    .QueryString(qs=>qs
        .OnField(x=>x.Email).Query("test@test.te").Operator(Operator.and)
        )
    )
);

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

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