简体   繁体   English

ElasticSearch NEST搜索多个类型和所有字段

[英]ElasticSearch NEST Search Multiple Types & All Fields

Using ElasticSearch NEST, I am having trouble getting expected results back from my queries. 使用ElasticSearch NEST,我无法从查询中获得预期的结果。 My index/type layout is as follows: 我的索引/类型布局如下:

  • theatres (index) 剧院(指数)
    • event (types) 事件(类型)
    • theatre 剧院
    • promotion 提升
    • generic content 通用内容

Each of those types have their own fields, and I am using NEST's Index() method to index the data. 每种类型都有自己的字段,我使用NEST的Index()方法来索引数据。 I can verify that it's being indexed properly by: 我可以通过以下方式验证它是否正确编入索引:

  • Looking at http://localhost:9200/theatres/_mapping http://localhost:9200/theatres/_mapping
  • Using the Head plugin to view data 使用Head插件查看数据

For reference, here is my client configuration: 供参考,这是我的客户端配置:

// TODO: Put settings in config
var node = new Uri("http://localhost:9200");
var connSettings = new ConnectionSettings(node);
connSettings.SetDefaultIndex("theatres");
connSettings.ThrowOnElasticsearchServerExceptions();

var client = new ElasticClient(connSettings);

The Query 查询

Now, for the query, I want to search all types and all fields within the index. 现在,对于查询,我想搜索索引中的所有类型和所有字段。 Using the Head plugin, I am able to generate the query and get the expected results: 使用Head插件,我能够生成查询并获得预期的结果: 在此输入图像描述

Using that query that it generated, I tried the following NEST query: 使用它生成的查询,我尝试了以下NEST查询:

var query = "waukesha"; // This would be passed in

var resp = client.Search<dynamic>(s => s
   .From(0)
   .Take(10)
   .Query(qry => qry
       .Bool(b => b
       .Must(m => m
           .QueryString(qs => qs
               .DefaultField("_all")
               .Query(query))))));

However, this gives me a different result. 但是,这给了我不同的结果。 Is NEST doing something behind the scenes that I'm not aware of? NEST是否在幕后做了我不知道的事情? Or is this not supported? 或者这不受支持?

Your query is missing .AllTypes() 您的查询丢失.AllTypes()

You can also specify multiple types using .Types("type1", "type1") 您还可以使用.Types("type1", "type1")指定多种类型

So: 所以:

var query = "waukesha"; // This would be passed in

var resp = client.Search<dynamic>(s => s
   .AllTypes()
   .From(0)
   .Take(10)
   .Query(qry => qry
       .Bool(b => b
       .Must(m => m
           .QueryString(qs => qs
               .DefaultField("_all")
               .Query(query))))));

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

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