Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to express an unusual query using ElasticSearch and/or Apache Solr?
    primarykey
    data
    text
    <p>How to query index given the following scenario.</p> <p>Docs have the following attributes:</p> <pre><code>body: # text shares: # array of share(s) share: orgid: # text role: # text </code></pre> <p>Here are a few sample docs:</p> <pre><code>docs = [ {'body':'hello', 'shares':[{'orgid':'abc.de.1', 'role':'11'}, {'orgid':'abc', 'role':'1'}]}, {'body':'world', 'shares':[{'orgid':'abc.de', 'role':'111'}]}, {'body':'today', 'shares':[{'orgid':'abc', 'role':'111'}]} ] </code></pre> <p>Using ElasticSearch syntax, I want to do the following queries:</p> <pre><code>Give me all docs where prefix of 'shares.orgid' = 'abc' and prefix of shares.role = '11' Should return: all of them Give me all docs where prefix of 'shares.orgid' = 'abc.de' and prefix of shares.role = '111' Should return: world Give me all docs where prefix of 'shares.orgid' = 'abc.de.1' and prefix of shares.role = '11' Should return: nothing Give me all docs where prefix of 'shares.orgid' = 'abc' and prefix of shares.role = '111' Should return: world, today </code></pre> <p>As I am researching this I found the following <a href="http://blog.sematext.com/2012/10/01/solr-vs-elasticsearch-part-3-searching/" rel="nofollow">info</a> of interest:</p> <p>In particular: <strong>ElasticSearch</strong></p> <blockquote> <p>ElasticSearch lets you use two type of queries – has_children and top_children queries to operate on child documents. The first query accepts a query expressed in ElasticSearch Query DSL as well as the child type and it results in all parent documents that have children matching the given query. The second type of query is run against a set number of children documents and then they are aggregated into parent documents. We are also allowed to choose score calculation for the second query type.</p> </blockquote> <p>Also of interest: <a href="http://www.elasticsearch.org/guide/reference/query-dsl/has-child-query.html" rel="nofollow">http://www.elasticsearch.org/guide/reference/query-dsl/has-child-query.html</a></p> <p>More re indexing: <a href="http://www.elasticsearch.org/guide/reference/api/index_.html" rel="nofollow">http://www.elasticsearch.org/guide/reference/api/index_.html</a> Parents &amp; Children</p> <p>A child document can be indexed by specifying it’s parent when indexing. For example:</p> <pre><code>$ curl -XPUT localhost:9200/blogs/blog_tag/1122?parent=1111 -d '{ "tag" : "something" }' </code></pre> <p>When indexing a child document, the routing value is automatically set to be the same as it’s parent, unless the routing value is explicitly specified using the routing parameter.</p> <p>EDIT:</p> <p>I'm getting closer. Here is a test using a wrapper that I am trying out that demonstrates an approach. I will write a test to demonstrate a solution to the question/problem. <a href="https://github.com/aparo/pyes/blob/master/tests/test_nested.py" rel="nofollow">https://github.com/aparo/pyes/blob/master/tests/test_nested.py</a>. I ran that test and tweaked it to use PrefixQuery and it works.</p> <p>Per commenter javanna, child query is not the direction to go. Here is example excerpts using pyes:</p> <pre><code>from pyes.filters import TermFilter, NestedFilter from pyes.query import FilteredQuery, MatchAllQuery, BoolQuery, TermQuery, PrefixQuery # from their unit test setup self.conn.index({"field1": "value1", "nested1": [{"n_field1": "n_value1_1", "n_field2": "n_value2_1"}, {"n_field1": "n_value1_2", "n_field2": "n_value2_2"}]}, self.index_name, self.document_type, 1) self.conn.index({"field1": "value1", "nested1": [{"n_field1": "n_value1_1", "n_field2": "n_value2_2"}, {"n_field1": "n_value1_2", "n_field2": "n_value2_1"}]}, self.index_name, self.document_type, 2) self.conn.index({"field1": "value1", "nested1": [{"n_field1": "ni", "n_field2": "11"}, {"n_field1": "n_value1_2", "n_field2": "n_value2_1"}]}, self.index_name, self.document_type, 3) self.conn.index({"field1": "value1", "nested1": [{"n_field1": "ni.kirmse.104", "n_field2": "111"}, {"n_field1": "n_value1_2", "n_field2": "n_value2_1"}]}, self.index_name, self.document_type, 4) self.conn.refresh(self.index_name) # just hacked their test to quickly see if I could do a prefix query on beginning of nested array/list values. q = FilteredQuery(MatchAllQuery(), NestedFilter('nested1', BoolQuery(must=[PrefixQuery('nested1.n_field1', 'ni'), PrefixQuery('nested1.n_field2', '111')]))) resultset = self.conn.search(query=q, indices=self.index_name, doc_types=[self.document_type]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload