Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MB-40604: Support IPRange query #1735

Merged
merged 2 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions search/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@ func ParseQuery(input []byte) (Query, error) {
}
return &rv, nil
}

_, hasCIDR := tmp["cidr"]
if hasCIDR {
var rv IPRangeQuery
err := json.Unmarshal(input, &rv)
if err != nil {
return nil, err
}
return &rv, nil
}

return nil, fmt.Errorf("unknown query type")
}

Expand Down
8 changes: 8 additions & 0 deletions search/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ func TestParseQuery(t *testing.T) {
input: []byte(`{"bool": true}`),
output: NewBoolFieldQuery(true),
},
{
input: []byte(`{"field": "x", "cidr": "1.2.3.0/4"}`),
output: func() Query {
q := NewIPRangeQuery("1.2.3.0/4")
q.SetField("x")
return q
}(),
},
{
input: []byte(`{"madeitup":"queryhere"}`),
output: nil,
Expand Down
44 changes: 44 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1889,3 +1889,47 @@ func TestHightlightingWithHTMLCharacterFilter(t *testing.T) {
gotFragment, expectedFragment)
}
}

func TestIPRangeQuery(t *testing.T) {
idxMapping := NewIndexMapping()
im := NewIPFieldMapping()
dmap := mapping.NewDocumentMapping()
dmap.AddFieldMappingsAt("ip_content", im)
idxMapping.DefaultMapping = dmap

tmpIndexPath := createTmpIndexPath(t)
defer cleanupTmpIndexPath(t, tmpIndexPath)

idx, err := New(tmpIndexPath, idxMapping)
if err != nil {
t.Fatal(err)
}

defer func() {
err = idx.Close()
if err != nil {
t.Fatal(err)
}
}()

ipContent := "1.2.3.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small doubt, do you think we can use a dummy (but a more realistic) IP address over here as an example?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack.

if err = idx.Index("doc", map[string]string{
"ip_content": ipContent,
}); err != nil {
t.Fatal(err)
}

q := query.NewIPRangeQuery("1.2.3.0/5")
q.SetField("ip_content")
sr := NewSearchRequest(q)

searchResults, err := idx.Search(sr)
if err != nil {
t.Fatal(err)
}

if len(searchResults.Hits) != 1 ||
searchResults.Hits[0].ID != "doc" {
t.Fatal("Expected the 1 result - doc")
}
}