0

I creat this function:

            public static ISearchResponse<Immo> searchImmoByCustomerField(Nest.ElasticClient esClient, string esIndex, string esIndex2, int from, int take, string qField, string nmqField, string qTerm, string nmqTerm)
            {
                var result = esClient.Search<Immo>(s => s
                        .AllIndices()
                            .Query(q => q
                                .Indices(i => i
                                    .Indices(new[] { esIndex, esIndex2 })
                                    .Query(iq => iq.Term(qField, qTerm))
                                    .NoMatchQuery(iq => iq.Term(nmqField, nmqTerm))
                                )
                            )
                 );
                return result;
            }

The function looking in 2 Indices for a search term. Visual Studio show me the message:

"Deprecated. You can specify _index on the query to target specific indices"

But how can i do that?

1
  • Do you need the NoMatchQuery? Because the deprecation comes from Elasticsearch 5 itself, deprecating the index query Commented Aug 4, 2017 at 14:41

1 Answer 1

3

The indices query is deprecated so it will still work currently, but the deprecation serves as a warning that it'll likely be removed in a future major version.

You can achieve the same functionality as the indices query with

var esIndex = "index-1";
var esIndex2 = "index-2";
var qField ="query-field";
var qTerm = "query-term";
var nmqField = "no-match-query-field";
var nmqTerm = "no-match-query-term";

client.Search<Immo>(s => s
    .AllIndices()
    .Query(q => (q
        .Term(t => t
            .Field(qField)
            .Value(qTerm)

        ) && +q
        .Terms(t => t
            .Field("_index")
            .Terms(new[] { esIndex, esIndex2 })
        )) || (q
        .Term(t => t
            .Field(nmqField)
            .Value(nmqTerm)
        ) && !q
        .Terms(t => t
            .Field("_index")
            .Terms(new[] { esIndex, esIndex2 })
        ))
    )
);

which produces the following query JSON

POST http://localhost:9200/_all/immo/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "term": {
                  "query-field": {
                    "value": "query-term"
                  }
                }
              }
            ],
            "filter": [
              {
                "terms": {
                  "_index": [
                    "index-1",
                    "index-2"
                  ]
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must": [
              {
                "term": {
                  "no-match-query-field": {
                    "value": "no-match-query-term"
                  }
                }
              }
            ],
            "must_not": [
              {
                "terms": {
                  "_index": [
                    "index-1",
                    "index-2"
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}
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.