How to use a variable in place of search query in ES cli tool?

Discussion related to "Everything" 1.5 Alpha.
Post Reply
gaberiel__
Posts: 3
Joined: Sun Jan 08, 2023 7:54 pm

How to use a variable in place of search query in ES cli tool?

Post by gaberiel__ »

I am trying to figure this out but failled badly.
These two examples work:

Code: Select all

ES.exe -n 5 -p user 1 documents spreadsheet
ES.exe -n 5 -p ""user 1 documents spreadsheet""
This returns no results

Code: Select all

ES.exe -n 5 -p "user 1 documents spreadsheet"
Neither do any of these:

Code: Select all

$string = "user 1 documents spreadsheet"

ES.exe -n 5 -p $string
ES.exe -n 5 -p "$string"
ES.exe -n 5 -p ""$string""
I am trying to build a simple powershell module that uses ES command line tool as its backend engine, I am in a desperate need of a way to use a variable in place of a search query. Is this at all possible?
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: How to use a variable in place of search query in ES cli tool?

Post by void »

I think you need to split the string first, please try:

$x="user 1 documents spreadsheet"
$x=$x.split(' ')

ES.exe -instance 1.5a -n 5 -p $x



This way, Everything receives the search:
user 1 documents spreadsheet

Without the split, Everything receives:
"user 1 documents spreadsheet"



If you are using Everything 1.5, please use the 1.5a instance name:

ES.exe -instance 1.5a -n 5 -p user 1 documents spreadsheet



Alternatively, disable the Everything 1.5a instance:
  • From the Start menu, search for: Notepad
  • Right click Notepad and click Run as administrator.
  • In Notepad, open your Everything-1.5a.ini in the same location as your Everything.exe
  • Change the following line:
    alpha_instance=1
    to:
    alpha_instance=0
  • Save changes and exit Notepad.
  • Restart Everything 1.5.
NotNull
Posts: 5468
Joined: Wed May 24, 2017 9:22 pm

Re: How to use a variable in place of search query in ES cli tool?

Post by NotNull »

Calling external programs from PowerShell can be cumbersome, especially if the parameters contain spaces.

There are several more options, like the call operator (&) or Start-Process, but I end up using Invoke-Expression the most as it is simple.
In this case:

Code: Select all

$string = 'user 1 documents spreadsheet'
Invoke-Expression "es.exe /n 5 -p $string"
Invoke-Expression can be abbreviated to iex


$string might contain toxic characters that have special meaning in PowerShell, like "(".
In that case:

Code: Select all

$string = 'user 1 documents spreadsheet'
Invoke-Expression "es.exe /n 5 -p ""$string"""
Note the extra "" around $string.
gaberiel__
Posts: 3
Joined: Sun Jan 08, 2023 7:54 pm

Re: How to use a variable in place of search query in ES cli tool?

Post by gaberiel__ »

NotNull wrote: Fri Jan 13, 2023 11:34 am Calling external programs from PowerShell can be cumbersome, especially if the parameters contain spaces.

There are several more options, like the call operator (&) or Start-Process, but I end up using Invoke-Expression the most as it is simple.
In this case:

Code: Select all

$string = 'user 1 documents spreadsheet'
Invoke-Expression "es.exe /n 5 -p $string"
Invoke-Expression can be abbreviated to iex


$string might contain toxic characters that have special meaning in PowerShell, like "(".
In that case:

Code: Select all

$string = 'user 1 documents spreadsheet'
Invoke-Expression "es.exe /n 5 -p ""$string"""
Note the extra "" around $string.
Just what I needed! Its working now, also thanks for the tip on escaping powershell symbols as well, I am certain I would have arrived at that cross road too.


Thanks to everyone else as well for helping!
void
Developer
Posts: 16897
Joined: Fri Oct 16, 2009 11:31 pm

Re: How to use a variable in place of search query in ES cli tool?

Post by void »

If you are looking to use content: or other search functions, please use octuple " to escape a single "

For example:

$x="abc 123"
Invoke-Expression "es.exe /n 5 *.docx content:""""""""$x"""""""""

This will call:
es.exe /n 5 *.docx content:"abc 123"
NotNull
Posts: 5468
Joined: Wed May 24, 2017 9:22 pm

Re: How to use a variable in place of search query in ES cli tool?

Post by NotNull »

Powershell is somewhat flexible when mixing ' and " quotes and will try to figure out what was meant.
(Variables like $x need to be enclosed in " " to get expanded to their value)

This will work too:

Code: Select all

$x="abc 123"
Invoke-Expression '  es.exe   count:5   *.docx   content:"""$x"""  '
(extra spaces added for clarity)
Post Reply