0
votes

In relation to the question: Elasticsearch/Nest - using MatchPhrase with OnFieldsWithBoost

I have an index which looks something like this:

{
    "_index": "myIndex",
    "_type": "Page",
    "_id": "119",
    "_score": 0.104187615,
    "_source": {
        "dataBaseId": 119,
        "category": "InfoPage",
        "type": "Page",
        "metaTitle": "myMeta",
        "metaDescription": "Description",
        "rawText": "my search text"
    }
}

My code looks like this:

var result = ElasticClient.Search<SearchReportDocument>(s => s
            .Index("myIndex")
            .Type("Page")
            .Size(10)
            .Query(q =>
                q.MultiMatch(m => m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1)).Type(TextQueryType.PhrasePrefix).Query(searchQuery))
                )
            );

I would like to extend it to only return results which has "category" equal to InfoPage.

2

2 Answers

1
votes

Use TermQuery

Field:"category"

Value:"infopage"


example:

 List<QueryContainer> shoudQuery = new List<QueryContainer>();
List<QueryContainer> mustQuery = new List<QueryContainer>();


shoudQuery.Add(new MultiMatchQuery()
    {
        //your Query
    });
mustQuery.Add(new termQuery()
    {
       Field = "category",
       value= "infopage",    
    });


QueryContainer queryContainer = new BoolQuery
        {
             Should = shoudQuery.ToArray(),
             Must = mustQuery.ToArray(),
             MinimumShouldMatch = 1,

         };


var result = Client.Search(s => s.Size(resultSize).Query(q => queryContainer)
0
votes

By the structure of your index I assume that you have a Page class , so you can find the pages with that category as I show below:

 var result= client.Search<Page>(sd => sd.Index("myIndex")
                                  .From(0)
                                  .Size(10).Query(q =>
                                  q.MultiMatch( m =>
                                  m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1))
                                  .Type(TextQueryType.PhrasePrefix)
                                  .Query(searchQuery))
                                  &&  q.Term("category", "infopage")));

Now, if you SearchReportDocumentclass has the same fields of Page, also you can find the pages this way:

  var result= client.Search<SearchReportDocument>(sd => sd.Index("myIndex").Type("Page")
                                  .From(0)
                                  .Size(10).Query(q =>
                                  q.MultiMatch( m =>
                                  m.OnFieldsWithBoost(f => f.Add(b => b.MetaTitle, 5).Add(b => b.RawText, 1))
                                  .Type(TextQueryType.PhrasePrefix)
                                  .Query(searchQuery))
                                  &&  q.Term("category", "infopage")));