Elasticsearch Multiple Prefix Keywords

Kousha

I need to use the prefix filter, but allow multiple different prefixes, i.e.

{"prefix": {"myColumn": ["This", "orThis", "orEvenThis"]}}

This does not work. And if I add each as a separate prefix is also obviously doesn't work.

Help is appreciated.

Update

I tried should but without any luck:

$this->dsl['body']['query']['bool']['should'] = [
    ["prefix" => ["myColumn" =>  "This"]],
    ["prefix" => ["myColumn" =>  "orThis"]]
];

When I add those two constraints, I get ALL responses (as though filter is not working). But if I use must with either of those clauses, then I do get a response back with the correct prefix.

pickypg

Based on your comments, it sounds like it may just be an issue with the syntax. With all ES queries (just like SQL ones), I suggest starting simple and just submitting them to ES as the raw DSL outside of code (although in your case this wasn't easily doable). For the request, it's a pretty straight forward one:

{
  "query" : {
    "bool" : {
      "must" : [ ... ],
      "filter" : [
        {
          "bool" : {
            "should" : [
              {
                "prefix" : {
                  "myColumn" : "This"
                }
              },
              {
                "prefix" : {
                  "myColumn" : "orThis"
                }
              },
              {
                "prefix" : {
                  "myColumn" : "orEvenThis"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

I added it as a filter because the optional nature of your prefixing is not improving relevancy: it's literally asking that one of them must match. In such cases where the question is "does this match? yes / no", then you should use a filter (with the added bonus that that's cacheable!). If you're asking "does this match, and which matches better?" then you want a query (because that's relevancy / scoring).

Note: The initial issue appeared to be that the bool / must was unmentioned and the suggestion was to just use a bool / should.

{
  "bool" : {
    "should" : [
      {
        "prefix" : {
          "myColumn" : "This"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orThis"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orEvenThis"
        }
      }
    ]
  }
}

behaves differently than

{
  "bool" : {
    "must" : [ ... ],
    "should" : [
      {
        "prefix" : {
          "myColumn" : "This"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orThis"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orEvenThis"
        }
      }
    ]
  }
}

because the must impacts the required nature of should. Without must, should behaves like a boolean OR. However, with must, it behaves as a completely optional function to improve relevancy (score). To make it go back to the boolean OR behavior with must, you must add minimum_should_match to the bool compound query.

{
  "bool" : {
    "must" : [ ... ],
    "should" : [
      {
        "prefix" : {
          "myColumn" : "This"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orThis"
        }
      },
      {
        "prefix" : {
          "myColumn" : "orEvenThis"
        }
      }
    ],
    "minimum_should_match" : 1
  }
}

Notice that it's a component of the bool query, and not of either should or must!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Elasticsearch Multiple Prefix query OR Matching

From Dev

Elasticsearch phrase prefix query on multiple fields

From Dev

Elasticsearch: query for multiple words across multiple fields (with prefix)

From Dev

Multiple Fields for Prefix in ElasticSearch and auto From and Size limit

From Dev

Multiple Fields for Prefix in ElasticSearch and auto From and Size limit

From Dev

ElasticSearch - prefix with space and filtering

From Dev

Elasticsearch phrase prefix results

From Dev

Elasticsearch sorting the data by using keywords

From Dev

Multiple keywords search

From Dev

Filter multiple keywords

From Dev

Search a text for multiple keywords

From Dev

merge multiple keywords into a string

From Dev

MySQL Searching For Multiple Keywords

From Dev

Regexp for multiple keywords matching

From Dev

WHERE LIKE with multiple keywords

From Dev

Searching with multiple keywords

From Dev

Boosting field prefix match in Elasticsearch

From Dev

Elasticsearch match prefix, no matches found

From Dev

elasticsearch phrase prefix stops finding

From Dev

Prefix properties of multiple PropertyPlaceholderConfigurers

From Dev

Recognizing multiple keywords using PocketSphinx

From Dev

SQL Server - Multiple FROM keywords?

From Dev

Split using multiple keywords as demiliters

From Dev

multiple condition if statement string keywords: in, not in

From Dev

Searching a list of strings for multiple keywords

From Dev

Write a test template with multiple keywords

From Dev

Unix command for searching multiple keywords

From Dev

Checking SMS for Multiple Keywords in Android

From Dev

Searching Multiple Keywords with foreach in Cakephp

Related Related

HotTag

Archive