0

What methods should I use in order for my query to return hits with at least 2 keywords in the text from an input phrase.

For example, if the input "hello friend" I want the return results to contain documents where "hello" and "friend" somewhere in the text. If the input "hello good friend" I want results where 2 of 3 keyword in the text. Or at least results with best combinations be on top. If I use code like one below I get results where "hello" or "friend" but not both.

        var searchResults = client.Search<Thread>(s => s
        .Type("threads")
        .From(0)
        .Size(100)
        .Query(q => q
            .Match(qs => qs
                .OnField(p => p.Posttext)
                .Query("hello friend")
                )
            )
            .Highlight(h => h
            .OnFields(
                f => f.OnField("posttext").PreTags("<b>").PostTags("</b>").FragmentSize(150)
                )
            )
        );

I can get results I want by code like this one but it is not flexible because phrase can be with arbitrary number of words.

        var searchResults = client.Search<Thread>(s => s
        .Type("threads")
        .From(0)
        .Size(100)
        .Query(q => q
            .Match(qs => qs
                .OnField(p => p.Posttext)
                .Query("hello")
                )
                &&
                q.Match(qs => qs
                .OnField(p => p.Posttext)
                .Query("friend")
                )
            )
            .Highlight(h => h
            .OnFields(
                f => f.OnField("posttext").PreTags("<b>").PostTags("</b>").FragmentSize(150)
                )
            )
        );

I think I am missing something. Please help.

Thanks in advance.

Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
Igor K.
  • 915
  • 2
  • 12
  • 22

2 Answers2

0

you need to use phrase query..

within the match you need specify the type as phrase ..

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-phrase

IF you go through the article above i guess you can find a direction to your question..

PS: I am aware of elasticsearch for javascript...

Anirudh Modi
  • 1,809
  • 12
  • 9
0

I found that adding .Operator(Operator.And) to Match query works in my situation. But I need to investigate more on phrase search.

Igor K.
  • 915
  • 2
  • 12
  • 22
  • Ok..and operator in a way works like phrase but they mean that thr document must have both the terms..however, if using or operator..it will do for anyone term.. – Anirudh Modi Dec 25 '15 at 20:12