0

I have Es index with multiple type and each type caters it's own filter parameters. Now we are building a Global Search on Es for multiple type and I am bit confused how to use type specific where clause to be included in NEST. Elastic Search -> Type 1 (where x=1) -> Type 2 (where y=1)

Now we are building a search query

var result = client.Search<ISearchDto>(s => s
                .From(from)
                .Size(PageSize)
                .Types(lstTypes)
                .Query(q => q.QueryString(qs => qs.Query(query)))
                );

*lstTypes will have Type 1 and Type 2

Now how can i add the where clause for all type 1 items with x=1 and for all type 2 items with y=1 in NEST.

Hope the question is clear, any help on this will be highly appreciated.

1 Answer 1

1

You can query on the _type meta field in much the same way as you query on any other field. To perform different queries based on type within one search query, you can use a bool query with multiple clauses

client.Search<ISearchDto>(s => s
    .From(from)
    .Size(pageSize)
    .Type(Types.Type(typeof(FirstSearchDto), typeof(SecondSearchDto)))
    .Query(q => q
        .Bool(b => b
            .Should(sh => sh
                .Bool(bb => bb
                    .Filter(
                        fi => fi.Term("_type", "firstSearchDto"),
                        fi => fi.Term(f => f.X, 1)
                    )
                ), sh => sh
                .Bool(bb => bb
                    .Filter(
                        fi => fi.Term("_type", "secondSearchDto"),
                        fi => fi.Term(f => f.Y, 1)
                    )
                )
            )
        )
    )
);

We have a bool query with 2 should clauses; each should clause is a bool query with the conjunction of 2 filter clauses, one for _type and the other for the property to be queried for each type, respectively.

NEST supports operator overloading so this query can be written more succinctly with

client.Search<ISearchDto>(s => s
    .From(from)
    .Size(pageSize)
    .Type(Types.Type(typeof(FirstSearchDto), typeof(SecondSearchDto)))
    .Query(q => (+q
        .Term("_type", "firstSearchDto") && +q
        .Term(f => f.X, 1)) || (+q
        .Term("_type", "secondSearchDto") && +q
        .Term(f => f.Y, 1))
    )
);

Both produce the following query

{
  "from": 0,
  "size": 20,
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_type": {
                    "value": "firstSearchDto"
                  }
                }
              },
              {
                "term": {
                  "x": {
                    "value": 1
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "filter": [
              {
                "term": {
                  "_type": {
                    "value": "secondSearchDto"
                  }
                }
              },
              {
                "term": {
                  "y": {
                    "value": 1
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.