栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ElasticSearch NEST词条查询未返回任何结果

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ElasticSearch NEST词条查询未返回任何结果

听起来您可能没有在为文档建立索引 之前
将类型的映射显式地放入索引中,所以Elasticsearch已基于所看到文档中字段的默认映射来推断该映射。例如,给定以下类型

[ElasticType(Name = "importFile")]public class importFile{    [ElasticProperty(Store = false, Index = FieldIndexOption.NotAnalyzed)]    public string FileName { get; set; }    [ElasticProperty(Store = false, Index = FieldIndexOption.NotAnalyzed)]    public string GroupId { get; set; }    [ElasticProperty(Store = true, Index = FieldIndexOption.Analyzed)]    public string FilePath { get; set; }}

如果我们按以下方式索引一些文档

void Main(){    var settings = new ConnectionSettings(new Uri("http://localhost:9200"));     var client = new ElasticClient(settings);    client.Index<importFile>(        new importFile{ FileName = "Group-1.uhh", FilePath = "", GroupId = "0ae1206d0644eabd82ae490e612732df" + "5da2cd141fdee70dc64207f86c96094"        },        index => index .Index("reviewer-bdd-test-index") .Type("importFile") .Refresh());    client.Index<importFile>(        new importFile        { FileName = "group-1.uhh", FilePath = "", GroupId = "0ae1206d0644eabd82ae490e612732df" + "5da2cd141fdee70dc64207f86c96094"        },        index => index .Index("reviewer-bdd-test-index") .Type("importFile") .Refresh());    var results = client.Search<importFile>(s => s     .Index("reviewer-bdd-test-index")     .Type("importFile")     .Query(q => q        .Filtered(fq => fq  .Filter(f => f      .Term(p => p.FileName, "Group-1.uhh")  )         )     ) );    Console.WriteLine(string.Format("{0} {1}", results.RequestInformation.RequestMethod, results.RequestInformation.RequestUrl));    Console.WriteLine(Encoding.UTF8.GetString(results.RequestInformation.Request));     Console.WriteLine("Matching document count: {0}", results.documents.Count());}

在控制台中输出以下内容

POST http://localhost:9200/reviewer-bdd-test-index/importFile/_search{  "query": {    "filtered": {      "filter": {        "term": {          "fileName": "Group-1.uhh"        }      }    }  }}Matching document count: 0

我们没有匹配的文件。在Elasticsearch中检查映射

curl -XGET "http://localhost:9200/reviewer-bdd-test-index/_mapping"

我们看到类型的映射

importFile

{   "reviewer-bdd-test-index": {      "mappings": {         "importFile": { "properties": {    "fileName": {       "type": "string"    },    "groupId": {       "type": "string"    } }         }      }   }}

这不是我们所期望的;

fileName
和两者
groupId
都应有
"index":"not_analyzed"
filePath
甚至不在映射中。这两个都是因为Elasticsearch已根据传递的文档推断了映射-
fileName
groupId
已映射为字符串类型并且将使用标准分析器进行分析,并且 我相信
filePath
尚未映射,因为两个看到的文档都为空字段的字符串值,因此应用于该字段的
标准分析器 不会为倒排索引生成任何标记,因此该字段不包含在映射中。

因此,为了确保一切正常,我们需要在索引任何文档之前向索引添加一个映射

void Main(){    var settings = new ConnectionSettings(new Uri("http://localhost:9200"));    var client = new ElasticClient(settings);    // Add the mapping for importFile to the index    client.CreateIndex(indexSelector => indexSelector        .Index("reviewer-bdd-test-index")        .AddMapping<importFile>(mapping => mapping .MapFromAttributes()        )    );    // ... Same as above after this point}

导致

POST http://localhost:9200/reviewer-bdd-test-index/importFile/_search{  "query": {    "filtered": {      "filter": {        "term": {          "fileName": "Group-1.uhh"        }      }    }  }}Matching document count: 1

成功! 我们有一个匹配的文件。检查Elasticsearch中的映射会产生我们期望的结果

{   "reviewer-bdd-test-index": {      "mappings": {         "importFile": { "properties": {    "fileName": {       "type": "string",       "index": "not_analyzed"    },    "filePath": {       "type": "string",       "store": true    },    "groupId": {       "type": "string",       "index": "not_analyzed"    } }         }      }   }}

此外,可以将属性映射替换为流畅的映射

var indexResult = client.CreateIndex(indexDescriptor => indexDescriptor    .Index("reviewer-bdd-test-index")    .AddMapping<importFile>(mapping => mapping        .Type("importFile")        .MapFromAttributes()        .Properties(properties => properties .String(s => s     .Name(file => file.FileName)     .Store(false)     .Index(FieldIndexOption.NotAnalyzed)) .String(s => s     .Name(file => file.GroupId)     .Store(false)     .Index(FieldIndexOption.NotAnalyzed)) .String(s => s     .Name(file => file.FilePath)     .Store(true))        )    ));

属性映射或流畅映射都可以在这一点上完成,但是有些事情只能通过流畅映射来实现,例如
multi_fields



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/382489.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号