Showing posts with label search. Show all posts
Showing posts with label search. Show all posts

Sunday, June 30, 2013

Cloudant create view and used indexes to search values

Create a document using : "_design/views103" (After selecting a database).
Enter "indexes" as key.
Enter following content after converting as JSON to value field:

{
    "search_name": {
        "index": 
          "function(doc) {
            index("default", doc._id);
            if (doc['customerID']) {
                index("customerID", doc['customerID'], {"store": "yes"});
            }
        }"
    }
}

_id automatically used as index.

https://{name}.cloudant.com/animaldb/_design/views103/_search/animals?q={id}
Wild card searches are supported, for both single (?) and multiple (*)
character searches. dat? would match date and data, dat* would match date,
data, database, dates etc. Wildcards must come after a search term, you cannot
do a query like *base.



customerID is a key field in data object.
https://{name}.cloudant.com/transactions/_design/views103/_search/animals?q=customerID:%228547963512%22

https://cloudant.com/for-developers/views/
https://cloudant.com/for-developers/search/

With this index you can run any of these queries.
Desired resultQuery
Birds class:bird
Animals that begin with the letter "l" l*
Carnivorous birds class:bird AND diet:carnivore
Herbivores that start with letter "l" l* AND diet:herbivore
Medium-sized herbivores min_length:[1 TO 3] AND diet:herbivore
Herbivores that are 2m long or less diet:herbivore AND min_length:[-Infinity TO 2]
Mammals that are at least 1.5m long class:mammal AND min_length:[1.5 TO Infinity]
Find "Meles meles" latin_name:"Meles meles"
Mammals who are herbivore or carnivore diet:(herbivore OR omnivore) AND class:mammal

Monday, April 29, 2013

Php check a value exists in multi level array and return path

Search in array using php by value if the array is any depth data.
<?php
function array_value_exists($search = "", $searchArray = array(), $returnKey = false, &$returnKeyArray = array(), $level = 0) {
    $returnValue = false;
    $search = trim(strval($search));
    if(strlen($search) == 0) {
        return false;
    }
    if(is_null($searchArray)) {
        return false;
    }
    if(!is_array($searchArray)) {
        return false;
    }
    foreach($searchArray as $key => $value) {
        array_push($returnKeyArray, $key);
        if(is_string($value)) {
            if($search == trim($value)) {
                $returnValue = true;
                break;
            }
        } else if(is_array($value)) {
            $returnValue = array_value_exists($search, $value, false, $returnKeyArray);
            if($returnValue == true) {
                break;
            }
        }
    }
    if($returnKey == true) {
        return $returnKeyArray;
    }
    return $returnValue;
}
?>

Use:<?php
$ary = array(
            "bm" => array(
                "dev" => array(
                    "pritom" => array(
                        "email" => "pritomkucse@yahoo.com"
                    ),
                    "touhid" => array(
                        "email" => "touhid@yahoo.com"
                    )
                ),
                "designer" => array(
                    "dipu" => array(
                        "email" => "dipu@yahoo.com"
                    )
                )
            ),
            "gpit" => array(
                "dev" => array(
                    "sanat" => array(
                        "email" => "sanat@gpit.com"
                    )
                )
            )
        );
        $has = array_value_exists("pritomkucse@yahoo.com", $ary);
        echo $has == true ? "True" : "False";
        print_r(array_value_exists("pritomkucse@yahoo.com", $ary, true));
?>

Output:
1. True (Find or not)
2. Path to this value.
Array
(
    [0] => bm
    [1] => dev
    [2] => pritom
    [3] => email
)