rs_es/query/
specialized.rs

1/*
2 * Copyright 2016-2018 Ben Ashford
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Specialised queries
18
19use serde::Serialize;
20use serde_json::Value;
21
22use crate::json::ShouldSkip;
23
24use super::{MinimumShouldMatch, Query};
25
26/// More like this query
27#[derive(Debug, Default, Serialize)]
28pub struct MoreLikeThisQuery {
29    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
30    fields: Option<Vec<String>>,
31    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
32    like_text: Option<String>,
33    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
34    ids: Option<Vec<String>>,
35    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
36    docs: Option<Vec<Doc>>,
37    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
38    max_query_terms: Option<u64>,
39    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
40    min_term_freq: Option<u64>,
41    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
42    min_doc_freq: Option<u64>,
43    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
44    max_doc_freq: Option<u64>,
45    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
46    min_word_length: Option<u64>,
47    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
48    max_word_length: Option<u64>,
49    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
50    stop_words: Option<Vec<String>>,
51    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
52    analyzer: Option<String>,
53    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
54    minimum_should_match: Option<MinimumShouldMatch>,
55    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
56    boost_terms: Option<f64>,
57    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
58    include: Option<bool>,
59    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
60    boost: Option<f64>,
61}
62
63impl Query {
64    pub fn build_more_like_this() -> MoreLikeThisQuery {
65        Default::default()
66    }
67}
68
69impl MoreLikeThisQuery {
70    add_field!(with_fields, fields, Vec<String>);
71    add_field!(with_like_text, like_text, String);
72    add_field!(with_ids, ids, Vec<String>);
73    add_field!(with_docs, docs, Vec<Doc>);
74    add_field!(with_max_query_terms, max_query_terms, u64);
75    add_field!(with_min_term_freq, min_term_freq, u64);
76    add_field!(with_min_doc_freq, min_doc_freq, u64);
77    add_field!(with_max_doc_freq, max_doc_freq, u64);
78    add_field!(with_min_word_length, min_word_length, u64);
79    add_field!(with_max_word_length, max_word_length, u64);
80    add_field!(with_stop_words, stop_words, Vec<String>);
81    add_field!(with_analyzer, analyzer, String);
82    add_field!(
83        with_minimum_should_match,
84        minimum_should_match,
85        MinimumShouldMatch
86    );
87    add_field!(with_boost_terms, boost_terms, f64);
88    add_field!(with_include, include, bool);
89    add_field!(with_boost, boost, f64);
90
91    build!(MoreLikeThis);
92}
93
94// A document can be provided as an example
95#[derive(Debug, Serialize)]
96pub struct Doc {
97    #[serde(rename = "_index")]
98    index: String,
99    #[serde(rename = "_type")]
100    doc_type: String,
101    // TODO - consider generifying this option
102    #[serde(skip_serializing_if = "ShouldSkip::should_skip", rename = "doc")]
103    doc: Option<Value>,
104    #[serde(skip_serializing_if = "ShouldSkip::should_skip", rename = "_id")]
105    id: Option<String>,
106}
107
108impl Doc {
109    pub fn from_doc<A, B>(index: A, doc_type: B, doc: Value) -> Doc
110    where
111        A: Into<String>,
112        B: Into<String>,
113    {
114        Doc {
115            index: index.into(),
116            doc_type: doc_type.into(),
117            doc: Some(doc),
118            id: None,
119        }
120    }
121
122    pub fn id<A, B, C>(index: A, doc_type: B, id: C) -> Doc
123    where
124        A: Into<String>,
125        B: Into<String>,
126        C: Into<String>,
127    {
128        Doc {
129            index: index.into(),
130            doc_type: doc_type.into(),
131            doc: None,
132            id: Some(id.into()),
133        }
134    }
135}