Skip to content

Commit

Permalink
Add option name completion field
Browse files Browse the repository at this point in the history
Example query:
```
{
  "from": 0,
  "size": 0,
  "suggest": {
      "my-term": {
          "text": "ngixn",
          "completion": {
              "field": "option_name_completion",
              "skip_duplicates": true,
              "size": 10000,
              "fuzzy": {
                  "fuzziness": 1
              }
          }
      }
  }
}
```
  • Loading branch information
adisbladis committed Jun 9, 2020
1 parent d6803ef commit 34d836a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions scripts/import-channel
Expand Up @@ -148,6 +148,12 @@ MAPPING = {
},
},
},
"option_name_completion": {
"analyzer": "nixOptionName",
"search_analyzer": "nixOptionName",
"preserve_position_increments": False,
"type": "completion"
},
"option_description": {"type": "text"},
"option_type": {"type": "keyword"},
"option_default": {"type": "text"},
Expand All @@ -157,6 +163,28 @@ MAPPING = {
}


def parse_option_name_completion(name: str):
results = []
for i in range(len(name.split('.'))):
tokens = name.split(".")[i:]

result = []
for idx, token in enumerate(tokens, start=1):
try:
prev = result[-1]
except IndexError:
prev = ""

if idx == len(tokens):
suffix = ""
else:
suffix = "."

result.append(prev + token + suffix)
results.extend(result)
return results


def get_last_evaluation(channel):
logger.debug(f"Retriving last evaluation for {channel} channel")

Expand Down Expand Up @@ -309,6 +337,7 @@ def get_options(evaluation):
yield dict(
type="option",
option_name=name,
option_name_completion=parse_option_name_completion(name),
option_description=option.get("description"),
option_type=option.get("type"),
option_default=str(option.get("default")),
Expand Down

0 comments on commit 34d836a

Please sign in to comment.