Search using term on Next 7.6.1 not returning any result

Hi,

I inserted some data to an index. To search on that, when I use match all, I can see my data.

var temp = _elasticClient.SearchAsync<MyDocument>(s => s.MatchAll(), cancellationToken);

But when I search using term, i dont get any hit.

    var result = await _elasticClient.SearchAsync<MyDocument>(s => s
        .Query(q => q
            .Term(p => p.Id, "aa22")), cancellationToken);

Any help to point out my mistake will be much appreciated.

Kind regards,

Shahed

What does the mapping look like in the inferred index for MyDocument?

A common reason for a search not returning the results expected is because of a difference in how the input for a field at document index time is analyzed, compared to how the query input for a field is analyzed at query time.

In this case, a term query does not undergo analysis; that is, the query is looking for the exact term "aa22" in the id field. If the id field is mapped as a "text" field, the document field will be analyzed at document index time, so the terms that result from analysis and are stored in the inverted index for the id field may not be a match for the term "aa22". Take a look at the documentation on analysis for more information.

Hi, thank you for the explanation. It does make sense and I will read the analysis documentation.
MyDocument looks like this

    public class MyDocument
    {
        public string Id { get; set; }
        public string DocNumber { get; set;}
    }

I did not do any manual mapping. So the filed Id will be mapped as text.
When I write my query with QueryString instead of term, then I get the expected result.

var searchQuery = Query<MyDocument >.QueryString(c => c
                .Query("aa22")
                .DefaultField(p => p.Id)
                .DefaultOperator(Operator.And));

            var response = _elasticClient.Search<MyDocument >(s => s
                .Query(x => x.Bool(q => q.Must(searchQuery))));

I guess that confirms what you mentioned.

In my search, for some text field, I need strict exact match and for some other text fields, I need partial to complete match. Can you please suggest how can I achieve that indexing and mapping using code?

How to map fields to enable the kinds of full-text search that users will perform is a large topic requiring an understanding of the problem domain.

As some general advice, if Id is only ever going to be search for exact matches, map it as a keyword field mapping. For string fields that would require full-text search, map them as text field mappings; text field mappings will use the standard analyzer by default, but you can configure different analysis to suit your needs. Check out the documentation for the client that covers both mapping and analysis.

Thank you so much for the suggestions. I went through the analyzer and rest of the documentation and have a better idea about this now. What you suggested makes complete sense. Thank you. Really appreciate.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.