简体   繁体   中英

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. My index name is people and my type name is 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. 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.
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 :

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.
Also, i want to do partial searching like %xyz% .
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.

Did you try using Query instead of OnField method?

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)))))))); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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