Skip to content

Instantly share code, notes, and snippets.

@malteos
Created January 9, 2017 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malteos/7a479580fdcb78d180a78cab9c1e69fd to your computer and use it in GitHub Desktop.
Save malteos/7a479580fdcb78d180a78cab9c1e69fd to your computer and use it in GitHub Desktop.

Testing Queries for CirrusSearch extension

As produced by SimpleKeywordFeature.doApply() (boosting has no effect)

curl -XPOST localhost:9200/wiki_content/_search?pretty -d '
{
  "_source": [
    "title"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "title.keyword": {
                    "value": "Main Page",
                    "boost": 9
                  }
                }
              },
              {
                "term": {
                  "title.keyword": {
                    "value": "Top Page",
                    "boost": 999
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
'

# from CitolyticsFeature.php
	protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) {
		$relatedPages = $this->getRelatedPages($context, $value);
	
		// Build bool filter query with related pages (boost by page score)
		$filter = new \Elastica\Query\BoolQuery();

		foreach($relatedPages as $page) {
			$pageFilter =  new \Elastica\Query\Term();
			$pageFilter->setTerm('title.keyword', $page['title'], $page['score']);

			$filter->addShould($pageFilter);
		}
		return [ $filter, false ];
	}

With rewriten MainQuery (boosting works, still can be combined with other filters, e.g. namespace)

curl -XPOST localhost:9200/wiki_content/_search?pretty -d '
{
  "_source": [
    "title"
  ],
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title.keyword": {
              "boost": 1,
              "value": "Top Page"
            }
          }
        },
        {
          "term": {
            "title.keyword": {
              "boost": 100,
              "value": "Main Page"
            }
          }
        },
        {
          "term": {
            "title.keyword": {
              "boost": 10,
              "value": "Some Page"
            }
          }
        }
      ],
      "minimum_number_should_match": 1,
      "filter": [
        {
          "bool": {
            "must": [
              {
                "terms": {
                  "namespace": [
                    0
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}'```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment