public void should_parse_free_text_and_fields() { // given string text = "ducks yellow author:chorizo green date:2018 blue"; SearchQueryParser searchQueryParser = CreateParser(); // when ParsedQueryResult queryResult = searchQueryParser.ParseQuery(text); // then queryResult.OriginalText.ShouldBe(text); queryResult.TextWithoutFields.ShouldBe("ducks yellow green blue"); queryResult.Fields.Count().ShouldBe(2); }
public void SearchQueryParser_SyntaxCheck(string query, bool success) { string result = ""; try { result = parser.ParseQuery(query, f => f, v => v, (m, a) => ""); Assert.True(success, $"Query '{query}' was supposed to fail!"); } catch (Exception ex) { Assert.False(success, $"Query '{query}' should not have failed: Error: {ex.Message}"); return; //Don't bother with the rest of the checks, it failed (and was suppsoed to) } if (string.IsNullOrWhiteSpace(query)) { Assert.True(string.IsNullOrWhiteSpace(result)); } else { Assert.Equal(query, result); } }
public void should_parse_fields_with_quotes() { // given string text = "some text tags:\"make,brexit,great,again\""; SearchQueryParser searchQueryParser = CreateParser(); // when ParsedQueryResult queryResult = searchQueryParser.ParseQuery(text); // then queryResult.OriginalText.ShouldBe(text); queryResult.TextWithoutFields.ShouldBe("some text"); queryResult.Fields.Count().ShouldBe(1); var tagsField = queryResult.Fields.First(x => x.Name == "tags"); tagsField.ShouldNotBeNull(); tagsField.Value.ShouldBe("\"make,brexit,great,again\""); }
public void should_parse_multiple_fields() { // given string text = "author:donald date:now title:headache some text"; SearchQueryParser searchQueryParser = CreateParser(); // when ParsedQueryResult queryResult = searchQueryParser.ParseQuery(text); // then queryResult.OriginalText.ShouldBe(text); queryResult.TextWithoutFields.ShouldBe("some text"); queryResult.Fields.Count().ShouldBe(3); var tagsField = queryResult.Fields.First(x => x.Name == "title"); tagsField.ShouldNotBeNull(); tagsField.Value.ShouldBe("headache"); }
public async Task <IEnumerable <SearchablePage> > Find(string query) { var p = new SearchQueryParser(); ParsedQueryResult queryResult = p.ParseQuery(query); using (var session = _documentStore.QuerySession()) { var martenQuery = session.Query <SearchablePage>().Where(x => true); if (!string.IsNullOrEmpty(queryResult.TextWithoutFields)) { martenQuery = martenQuery.Where(x => x.PlainTextSearch(queryResult.TextWithoutFields)); } string title = queryResult.GetFieldValue("title"); if (!string.IsNullOrEmpty(title)) { martenQuery = martenQuery.Where(x => x.Title.PlainTextSearch(title)); } string tags = queryResult.GetFieldValue("tags"); if (!string.IsNullOrEmpty(tags)) { martenQuery = martenQuery.Where(x => x.PlainTextSearch(tags)); } string pageId = queryResult.GetFieldValue("pageId"); if (!string.IsNullOrEmpty(pageId)) { martenQuery = martenQuery.Where(x => x.PageId.PlainTextSearch(pageId)); } string author = queryResult.GetFieldValue("author"); if (!string.IsNullOrEmpty(pageId)) { martenQuery = martenQuery.Where(x => x.Author.PlainTextSearch(author)); } return(await martenQuery.ToListAsync()); } }