How to search nested ElasticSearch objects using NEST

I am trying to search for nested elasticsearch objects using a C # NEST client. My name is the index people

and my type person

is where the field is car

nested

.

This is my class:

using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebApplication5
{
public class person
{
    public int id { get; set; }
    public String fname  { get; set; }
    public String mname { get; set; }
    public String lname { get; set; }
    public String houseno { get; set; }
    [ElasticProperty(Type = FieldType.Nested)]
    public IList<NestedType> car { get; set; } 
    public class NestedType
    {
        public String carname { get; set; }
        public int car_no { get; set; }
        public String color { get; set; }
    }
}

} 

      

My web application now looks something like this:

using Elasticsearch.Net;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Configuration;
using Nest;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication5
{
public partial class WebForm1 : System.Web.UI.Page
{
    Stopwatch stopwatch = new Stopwatch();
    Uri node;
    ConnectionSettings settings;
    ElasticClient client;
    IList<person> list;
    protected void Page_Load(object sender, EventArgs e)
    {
        node = new Uri("http://localhost:9200");
        settings = new ConnectionSettings(node, defaultIndex:   "people");
        client = new ElasticClient(settings);
        list = new List<person>();

    }

    public IList<person> Search(ref long totalResult, int from,  int size, string searchKeyword)
    {
        list.Clear();
        stopwatch.Start();

        try
        {

            var result = client.Search<person> /*   */


  stopwatch.Start();
  totalResult = result.Total;
  list = result.Hits.Select(t => t.Source).ToList<person>();
  }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }
 return 
            list;
    }
 protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        long totalResult = 0;
        IList<person> List = new List<person>();
        List = Search(ref totalResult, 0, 1000,TextBox1.Text);
        Label1.Text = "" + List.Count + " result(s)"+ " in "+ stopwatch.Elapsed.TotalSeconds+"seconds";
        GridView1.DataSource = List;
        GridView1.DataBind();
        List.Clear();

    }
  }
}

      

Here searchKeyword

is the word I need to look for.
I was typing it into the search box (web form) in the browser.
SearchKeyword can contain any value that needs to be matched to the value of any field in my table person

.
If searchKeyword matches a subdocument, then the exact subdocument must be returned.
I don't know if there is something wrong with my subquery, or I really can't figure out how to use the subquery to do this. Every time I run the application and look for a value that is actually present in the table, I get zero results. Please help me figure out how to write query

for search in nested fields and help me with spell checkclass

... Part of the request is marked as / * * /. Can anyone help me resolve this?

UPDATE This is my index mapping:

POST- people/person/_mapping
{
"person":{
"properties":{
  "car": {
    "type": "nested"
    }
  }
 }
}

      

And my entries look like this:

POST-people/person
{
"id":1, 
"fname":"aditi",  
"mname":"ananya", 
"lname":"xyz", 
"houseno":"abc",
"car":
 [
  {
   "carname":"fiat",
   "carno":1234,
   "color":"white"
  },

  {
   "carname":"maruti",
   "carno":5678,
   "color":"silver"
  }
 ]
}


POST-people/person
{
"id":2, 
"fname":"robin",  
"mname":"kumar", 
"lname":"sharma", 
"houseno":"efg",
"car":
  [
   {
      "carname":"audi",
      "carno":4321,
      "color":"black"
   },

   {
      "carname":"honda",
      "carno":8765,
      "color":"red"
   },

   {
      "carname":"merecedez",
      "carno":0101,
      "color":"purple"
   }
 ]
}

      

This means I have 2 entries.

SECOND UPDATE
I tried this query and it works great. Although this is not my last question.

POST-people/person/_search
{
"_source":false,
"query": {
"filtered": {
  "query": {"match_all": {}},
  "filter": {
    "nested": {
      "path": "car",
      "query":{
        "filtered": {
          "query": { "match_all": {}},
          "filter": {
            "and": [
              {"term": {"car.carname":"mercedez"}},
              {"term": {"car.color":"purple"}}
            ]
          }
        }
      },
"inner_hits":{}
    }
  }
 }
}
}

      

THIRD UPDATE
So now, in this case, my final request should be:

{"_source":false,
"query": {
 "filtered": {
  "query": {"match_all": {}},
  "filter": {
    "nested": {
      "path": "car",
      "filter": {
        "term": {
          "car.carname": "audi"
        }
      }, 
      "inner_hits" : {}
    }
   }
  }
 }
}

      

How do I write this request in .net?
Here "_source" and "inner_hits" are important to me because I only want to return the corresponding subdocument and nothing else (for example, I only want to return the matched subdocument and not other subdocuments).
So, can you please help me write a relevant search term for this?
Moreover, here I match in the field car.carname

, but I want my application to match all other subfields of the field car

, like car.carno

and car.color

, and even for all other top-level fields like id

, fname

etc.

FOURTH UPDATE
Here I wrote a .net search for my final query, which I mentioned in the THIRD UPDATE section (please take a look at my third update).
Could you please check if this is correct? The relevant final request in .net I wrote:

(s => s
 .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("audi"))))))));  

      

This is what I wrote while researching your queries (Thanx a lot for this :)). Check it out and tell me if there is anything wrong. But yes, I still cannot get any results.

+3


source to share


1 answer


Try using a subquery.

Let me index some data for testing purposes:

client.Index(new Person
{
    Id = 1, 
    Car = new List<NestedType> {new NestedType {Carname = "car1"}}
});
client.Index(new Person
{
    Id = 2,
    Car = new List<NestedType> {new NestedType {Carname = "car1"}, new NestedType {Carname = "car2"}}
});
client.Index(new Person
{
    Id = 3,
    Car = new List<NestedType> { new NestedType {Carname = "car2"}}
});

client.Refresh();

      

Now try this nested query:

var searchResponse = client.Search<Person>(s =>
    s.Query(q => q
        .Nested(n => n
            .Path(p => p.Car)
            .Query(qq => qq.Match(m => m.OnField(f => f.Car.First().Carname).Query("car2"))))));

      

Search results:

{
   "took": 10,
   "timed_out": false,
   "_shards": {..},
   "hits": {
      "total": 2,
      "max_score": 1.4054651,
      "hits": [
         {
            "_index": "my_index",
            "_type": "person",
            "_id": "2",
            "_score": 1.4054651,
            "_source": {
               "id": 2,
               "car": [
                  {
                     "carname": "car1",
                     "carNo": 0
                  },
                  {
                     "carname": "car2",
                     "carNo": 0
                  }
               ]
            }
         },
         {
            "_index": "my_index",
            "_type": "person",
            "_id": "3",
            "_score": 1,
            "_source": {
               "id": 3,
               "car": [
                  {
                     "carname": "car2",
                     "carNo": 0
                  }
               ]
            }
         }
      ]
   }
}

      

Hope this helps you.

UPDATE

Including your formatting:



var searchResponse = client.Search<Person>(s =>
    s.Query(q => q
        .Nested(n => n
            .Path(p => p.Car)
            .Query(qq => qq.Match(m => m.OnField(f => f.Car.First().Carname).Query("car2"))))));

      

UPDATE2

Based on your update, try this query:

var searchResponse = client.Search<Person>(s => s
    .Query(query => query.Filtered(filtered => filtered
        .Query(q => q.MatchAll())
        .Filter(f => f.Nested(nf => nf
            .Path(p => p.Car)
            .Filter(filter => filter
                .And(
                    f1 => f1.Term(t => t.Car.First().Carname, "audi"),
                    f2 => f2.Term(t => t.Car.First().Color, "purple"))) 
            )))));

      

which creates this query for elasticsearch search:

{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "filter": {
            "and": {
              "filters": [
                {
                  "term": {
                    "car.carname": "audi"
                  }
                },
                {
                  "term": {
                    "car.color": "purple"
                  }
                }
              ]
            }
          },
          "path": "car"
        }
      }
    }
  }
}

      

Update3

With internal hits:

var searchResponse = client.Search<Person>(s => s
    .Source(false)
    .Query(query => query.Filtered(filtered => filtered
        .Query(q => q.MatchAll())
        .Filter(f => f.Nested(nf => nf
            .InnerHits()
            .Path(p => p.Car)
            .Filter(filter => filter
                .And(
                    f1 => f1.Term(t => t.Car.First().Carname, "audi"),
                    f2 => f2.Term(t => t.Car.First().Color, "purple")))
            )))));

      

Note: there was a bug in elasticsearch 1.5.0 regarding internal shocks and filter. Take a look. Let me know if you need help getting internal calls.

+3


source







All Articles