Elasticsearch could not search on string field

1.2k views Asked by At

I am trying to use NEST to create search query dynamically based on user's input. I want to add multiple filter in Filter with Term but string field searching is not possible and I cannot find any solution.

Code for example is that, this code try to search on string field an it is not working

var response = await _elasticClient.SearchAsync<CustomerAddressInfo>(p => p
            .Query(q => q
                .Bool(b => b
                    .Filter(f => f.Term(t => t.Field(p => p.AccountAddressId).Value(type.AccountAddressId)))
                )
            )
        );

And the other search simple is with integer field and it is working with success

var response = await _elasticClient.SearchAsync<CustomerAddressInfo>(p => p
            .Query(q => q
                .Bool(b => b
                    .Filter(f => f.Term(t => t.Field(p => p.CreateUnitId).Value(type.CreateUnitId)))
                )
            )
        );

But; if I search data on string field with Match keyword, again it is successfull on search

var response = await _elasticClient.SearchAsync<CustomerAddressInfo>(p => p
            .Query(q => q
                .Match(m => m
                    .Field(f => f.AccountAddressId)
                    .Query(type.AccountAddressId)
                )
            )
        );

And the question is, how can I give multiple search criteria with Match query method or how can I seach on string field by Term query method on elastic

2

There are 2 answers

1
ESCoder On

I am not familiar with NEST, but to search on multiple fields using match query or term query, you can refer following example :

Bool query is used to combine one or more clauses, to know more refer this

Avoid using the term query for text fields.

By default, Elasticsearch changes the values of text fields as part of analysis. This can make finding exact matches for text field values difficult.

To search text field values, use the match query instead.

Index Mapping

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "cost": {
        "type": "long"
      }
    }
  }
}

Index data:

{
    "name":"apple",
    "cost":"40"
}
{
    "name":"apple",
    "cost":"55"
}

Search Query: Multiple Search criteria with match

   {
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "apple" }},
        { "match": { "cost": 40 }}
      ]
    }
  }
}

Search on-field by term query

  {
  "query": {
    "bool" : {
      "must" :[
         {"term" : { "name" : "apple" }},
         {"term":  { "cost":40 }}
      ]
    }
  }
}

Search Result:

"hits": [
  {
    "_index": "my-index",
    "_type": "_doc",
    "_id": "3",
    "_score": 1.1823215,
    "_source": {
      "name": "apple",
      "cost": "40"
    }
  }
]
0
Amir On

Hey i do not get the whole requirements of yours. But if you want to add multiple condition on filter then you can do like below.

QueryContainer qSs = null;
foreach(var query in queries) // let say queries is list of yours search item
{
  qSs &= new TermQuery { Field = "your_field_name", Value = query  };
}

 var searchResults = await _elasticClient.SearchAsync<CustomerAddressInfo>(s => s
                .Query(q => q
                     .Bool(b => b.Filter(qSs)  )
                     )
                );