1

I have an sql table with columns Name, Category, Location. I am using Elastic Search with NEST. my query like this:

var result = client.Search<Models.Search.Poll>(s => s.Query(q => q.Fuzzy(f => f.OnField(p => p.Name).Value(query))))));

So if there is a record with name = "We are here" and user search "are" , it returns result.

Now I need to add two more parameters category and location to this query:

so I made it like this:

var result = client.Search<Models.Search.Poll>(s => s.Query(q => q.Fuzzy(f => f.OnField(p => p.Name).Value(query).OnField(r => r.Category).Value(category))));

but it is not working with query field now. but it works with category now. here is what I get when I type name but dont select category:

StatusCode: OK, 
    Method: POST, 
    Url: http://server.abc.com:9200/pollit-dev/polls/_search, 
    Request: {
  "query": {
    "fuzzy": {
      "category": {
        "value": "Select a Category"
      }
    }
  }
}, 
    Response: {"took":2892,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

I tried this one well:

var result = client.Search<Models.Search.Poll>(s => s.MatchAll().Query(q => q.Term(p => p.Name, query) || q.Term(p => p.Category,category) || q.Term(p => p.Location, Location)

but no luck so far.

Regards, Asif Hameed

1 Answer 1

1

You have multiple options for that. First one is almost like yours but you have to use a Bool condition there.

var result = client.Search<Models.Search.Poll>(s => s
      .MatchAll()
      .Query(q => q
         .Bool(b => b
           .Must(m => m.Term(p => p.Name, query) || m.Term(p => p.Category,category) || m.Term(p => p.Location, Location))
         )
       )
   );

Now another option is to use queryContainers. Something like this:

var result = _Instance.Search<Models.Search.Poll>(q => q
                .Query(qq =>
                {
                    QueryContainer termQuery = null;
                    QueryContainer locationQuery = null;
                    QueryContainer categoryQuery = null;

                    termQuery = qq.Term(p => p.Name, query);
                    categoryQuery = qq.Term(p => p.Category,category);
                    locationQuery = qq.Term(p => p.Location, Location);

                    return termQuery || categoryQuery || locationQuery;
                    })
           );

You can also elaborate the QueryContainers and add multiple search parameters there. Hope this helps you. Great day!

Sign up to request clarification or add additional context in comments.

1 Comment

This only works if there is at least one exact match if I am right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.