2017-08-06 60 views
0

我正在玩弹性搜索,并在dotnet中进行了以下控制器操作。无法获得前缀查询,以使用弹性搜索dotnet

但前缀查询不起作用。

[Entities(Name = "ListStreams")] 
    [ProducesResponseType((int)HttpStatusCode.NotFound)] 
    [ProducesResponseType(typeof(StreamEntity[]), (int)HttpStatusCode.OK)] 
    public async Task<IActionResult> ListStreams(
      [FromResourceProvider] ResourceProviderRouteData route) 
    { 
     var clientFactory = new ElasticServiceClientFacotry(); 
     var client = await clientFactory.CreateClientAsync(); 


     var documentSearchResult = await client.SearchAsync<StreamEntity>(
       k => k.Index("streams") 
       .Routing(route.Routing) 
       .Query(q=>q.Prefix(c=> c.Field("id").Value(route.ResourceId))) 
       , 
      HttpContext.RequestAborted); 
      return Ok(documentSearchResult.Documents); 


    } 

下面的数据项是在索引

[ 
    { 
    "name": "swl2x33p_vjv", 
    "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test/providers/EarthML.Streams/streams/swl2x33p_vjv", 
    "location": null, 
    "type": "EarthML.Streams/streams" 
    }, 
    { 
    "name": "gkljqg2j_ic0", 
    "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test/providers/EarthML.Streams/streams/gkljqg2j_ic0", 
    "location": null, 
    "type": "EarthML.Streams/streams" 
    } 
] 

route.ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test/providers/EarthML.Streams"

回答

0

我想通了,用分析仪是这个问题,我没有创建索引自己,而是依靠构建索引的自动创建。

只要我删除我的索引和创建它像下面

var descriptor = new CreateIndexDescriptor("streams") 
       .Mappings(ms => ms 
       .Map<StreamEntity>(m => m.AutoMap() 
       .Properties(props => props 
        .Keyword(s1 => s1.Name(p => p.Id).Norms(false)) 
        .Keyword(k=>k.Name(p=>p.Name).Norms(false)) 
        .Keyword(k=>k.Name(p=>p.Type).Norms(false)) 
        .Keyword(k => k.Name(p => p.Location).Norms(false)) 
       ))); 

那么前缀过滤器开始工作。

这也解释了为什么它不使用默认的映射工作: https://www.elastic.co/blog/strings-are-dead-long-live-strings

作为关键字字段将id.keyword而不是现在。