无法获得前缀查询以使用弹性搜索dotnet

时间:2017-08-06 19:48:41

标签: elasticsearch

我正在使用弹性搜索并在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"

1 个答案:

答案 0 :(得分: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