简体   繁体   English

使用C#Nest Client在Elasticsearch文档的所有字段中搜索输入关键字

[英]Searching for an input keyword in all fields of an elasticsearch document using c# nest client

I have a nested elasticsearch document and I want to search within all the fields of that document ie I want to search in both the top-level and the nested fields. 我有一个嵌套的elasticsearch文件,我想也就是我想在顶级和嵌套字段都来搜索所有该文档的领域内进行搜索。 My index name is people and my type name is person . 我的索引名是people ,类型名是person
My documents look like this : 我的文档如下所示:

{
"id": 1,
"fname": "elizabeth",
"mname": "nicolas",
"lname": "thomas",
"houseno": "beijing",
"car": [
          {
             "carname": "audi",
             "carno": 4444,
             "color": "black"
          },
          {
             "carname": "mercedez",
             "carno": 5555,
             "color": "pink"
          }
      ]
}   

Then i have the following query in .net which actually searches for an user input keyword in the elasticsearch documents. 然后我在.net有以下查询,该查询实际上在elasticsearch文档中搜索user input keyword Basically, I want to search in each and every field of a document. 基本上,我想搜索文档的每个字段。 And I use inner_hits in my query so that i can return only the matching nested document. 而且我在查询中使用inner_hits ,以便仅返回匹配的嵌套文档。
I have designed my query as : 我将查询设计为:

var result = client.Search<person>
                (s => s
                .From(from)
                .Size(size)
                .Source(false)
                .Query(query => query.Filtered(filtered => filtered
                .Query(q => q.MatchAll())
                .Filter(f => f.Nested(nf => nf
                .InnerHits()
                .Path(p => p.car)
                .Query(qq => qq.Match(m => m.OnField(g =>  g.car.First().carname).Query(searchKeyword))))))));  

And my corresponding JSON query which i use in the head plugin is : 我在头插件中使用的对应的JSON查询是:

POST-people/person/_search:
{
"_source":false,
"query": {
  "filtered": {
    "query": {"match_all": {}},
      "filter": {
      "nested": {
      "path": "car",
      "filter": {
        "term": {
          "car.carname": "searchKeyword"
        }
      }, 
      "inner_hits" : {}
    }
   }
  }
 }
}  

But i wanted to search in all the fields(id,fname,mname,lname,houseno,carname,carno,color) and not just in a single field eg in carname as i have done in my above query. 但是我想在所有字段(id,fname,mname,lname,houseno,carname,carno,color)中进行搜索,而不仅是在单个字段中进行搜索,例如在carname中,就像我在上面的查询中所做的那样。
Also, i want to do partial searching like %xyz% . 另外,我想像%xyz%一样进行partial searching
How can i do these ? 我该怎么办?
Can anyone help me modify this query so that i can use this single query to search within all the fields as well as do partial searching? 谁能帮助我修改此查询,以便我可以使用此单个查询在所有字段中进行搜索以及进行部分搜索?
I'm new to elasticsearch as well as .net,so I would be thankful for any help. 我是Elasticsearch和.net的新手,所以我会很感激。

Did you try using Query instead of OnField method? 您是否尝试使用Query而不是OnField方法?

I mean, having your query this way: 我的意思是,您的查询方式如下:

var result = client.Search<person>
            (s => s
            .From(from)
            .Size(size)
            .Source(false)
            .Query(query => query.Filtered(filtered => filtered
            .Query(q => q.MatchAll())
            .Filter(f => f.Nested(nf => nf
            .InnerHits()
            .Path(p => p.car)
            .Query(qq => qq.Match(m => m.Query(searchKeyword)))))))); 

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

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